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

时间:2016-04-28 05:34:50

标签: javascript json

我无法加载本地存储在我系统上的JSON文件。每当我执行它时,我使用以下Java Script代码加载我的文件我得到错误" Uncaught InvalidValueError:not a feature or FeatureCollection "

  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存储库中的本地文件加载它

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

jQuery的:

 $.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:

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