【问题标题】:Calling json file from external sources从外部源调用 json 文件
【发布时间】:2016-10-15 20:06:56
【问题描述】:

我使用此 JavaScript 从外部源调用我的 JSON 数据。 json 数据将从服务器加载。那么,我可以使用外部 JSON 数据从谷歌地图获取标记的脚本是什么。

 var map, infowindow; ////

// The JSON data
var json = "http://www.tripleclickstudio.com/json/file.json"
$.getJSON(json,{
    tags:"location",
    tagmode:"any",
    format:"json"
    
})

function initialize() {
  
  // Giving the map som options
  var mapOptions = {
    ////
  };
  
  // Creating the map
  map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
  
  var bounds = new google.maps.LatLngBounds(); ////
  
  // Looping through all the entries from the JSON data
  var responses = json[0].ResponseData; ////
  for(var i = 0; i < responses.length; i++) { ////
    
    // Current object
    var obj = responses[i]; ////

    // Adding a new marker for the object
    var position =
      new google.maps.LatLng( obj.CoordinateY, obj.CoordinateX ); ////
    bounds.extend( position ); ////
    var marker = new google.maps.Marker({
      position: position, ////
      map: map,
      draggable: true,
      animation: google.maps.Animation.DROP,
      title: obj.BuildingName
    });
    
    // Adding a new info window for the object
    var clicker = addClicker(marker, obj.BuildingName); ////

  } // end loop
  
  map.fitBounds( bounds ); ////
  
  // Adding a new click event listener for the object
  function addClicker(marker, content) {
    google.maps.event.addListener(marker, 'click', function() {
      
      if (infowindow) {infowindow.close();}
      infowindow = new google.maps.InfoWindow({content: content});
      infowindow.open(map, marker);
      
    });
  }
  
}

// Initialize the map
google.maps.event.addDomListener(window, 'load', initialize);
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=true"></script>
   <script src="https:////cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <div id="map-canvas"></div>

但输出只是一个空白页。但如果我将 JSON 数据使用到文件中,则效果很好。

【问题讨论】:

  • var json 是一个 url 字符串。学习一些 ajax 教程并阅读 getJSON 文档。需要先了解 ajax 是如何工作的
  • 正如我在对your original question 的评论中提到的以及下面geocodezip 描述的那样,您需要将回调函数传递给$.getJSON() 调用。该函数将接收一个包含您的 JSON 数据的参数,这就是您需要使用该数据进行地图/标记初始化的地方。 $.getJSONdocumentation中有几个例子;从顶部开始的第二个代码 sn-p 有一个带有回调函数的 $.getJSON() 调用,您可以将其用作示例。
  • 另请注意,您的测试文件中的 JSON 数据与原始问题中的测试 JSON 格式不同。那里的测试数据将整个 JSON 对象包裹在 [] 中,使其成为一个包含单个元素的数组,该元素是实际对象。您服务器上的 JSON 文件没有外部 [](这更有意义没有外部 [])。因此,一旦您下载了数据,我们正在执行json[0].ResponseData 之类的操作,它应该只是json.ResponseData(假设您使用名称json 作为回调函数的参数)。

标签: javascript jquery json google-maps google-maps-api-3


【解决方案1】:

我在您的代码 sn-p 中收到两个 javascript 错误:

  1. XMLHttpRequest cannot load http://www.tripleclickstudio.com/json/file.json?tags=location&amp;tagmode=any&amp;format=json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

  2. Uncaught TypeError: Cannot read property 'length' of undefined 在这一行:

for(var i = 0; i < responses.length; i++) {  

因为在运行循环时响应不存在。 $.getJSON 是异步的,在运行回调函数(您尚未定义)之前不会填充数据。但是,您的第一个问题是有一个安全策略阻止您使用 $.getJSON 下载它。您需要获得权限才能为您的域下载它(标题中的相应 Access-Control-Allow-Origin)或通过 JSONP 下载它。

但是,当我这样做时,我的 JSON 中出现语法错误。

【讨论】:

  • JSON 格式本身似乎没问题 - 我将测试文件粘贴到 JSONLint 并验证没有错误。像往常一样,您与其他 cmets 的钱是对的。 :-) 另请参阅OP's original question 了解更多上下文。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-29
  • 1970-01-01
  • 1970-01-01
  • 2011-01-05
  • 1970-01-01
  • 2017-11-01
相关资源
最近更新 更多