无法加载存储在系统中本地的 JSON 文件

unable to load a JSON file stored locally in the System

本文关键字:JSON 文件 系统 加载 存储      更新时间:2023-09-26

我无法加载本地存储在系统上的 JSON 文件。每当我执行文件时,我都使用以下 Java 脚本代码加载我的文件,我收到错误"未捕获的无效值错误:不是功能或功能集合"

  var map;
  function initMap() {
  map = new google.maps.Map(document.getElementById('map'), {
  center: {lat: -34.397, lng: 150.644},
  zoom: 8
  });
  }
  map.data.addGeoJson("test.geojson"); 

尝试使用以下对我有用的方法

  var map;
  function initMap() {
  map = new google.maps.Map(document.getElementById('map'), {
  center: {lat: -34.397, lng: 150.644},
  zoom: 8
  });
  }
   var JSON ={"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[-83.53021556073881,40.30187345365667],[-83.53021555683246,40.30177280563904],[-83.53006258934602,40.30177280901812],[-83.53006259302536,40.301873457035754],[-83.53021556073881,40.30187345365667]]]},"properties":{"stroke":"#555555","stroke-width":2,"stroke-opacity":1,"fill":"#555555","fill-opacity":0.5,"name":"Shower Test Lab","styleUrl":"#rangecolour","styleHash":"7d00d790","description":"PolygonDescription   <table border='"3'">   </table>"}}]}
   that.map.data.addGeoJSON(JSON);

我之前搜索过它,但没有通过存储在我的 Eclipse SDK 存储库中的本地文件加载它

尝试使用绝对路径而不是相对路径。

这可以通过使用jquery或Ajax轻松完成:

j查询:

 $.getJSON( "C:'Users'nirmal'workspace'com.test'test.geojson", function( data ) {
    var items = [];
  $.each( data, function( key, val ) {
    items.push( "<li id='" + key + "'>" + val + "</li>" );
  });
  $( "<ul/>", {
   "class": "my-new-list",
   html: items.join( "" )
   }).appendTo( "body" );
});

阿贾克斯:

 $.ajax({
 dataType: "json",
 url: getAbsoluteUrl("C:'Users'nirmal'workspace'com.test'test.geojson"),
 data: data,
 success: success
});