0

我在做什么和我尝试过的

我正在使用谷歌地理编码网络服务来获取我通过的地址的纬度和经度,如下所示:

$.getJSON("http://maps.googleapis.com/maps/api/geocode/json?address=Boston&sensors=true",function(data){
    //Do my programming here
});

出于某种原因,它最初工作得很好,但后来它停止工作,我发现是问题所在,即使用谷歌客户端 API。

我还阅读了关于那个SO 问题的答案,其中说<script>标签有助于克服跨站点脚本问题。

我的问题 :

我正在使用 RequireJs,所以我包含这样的文件:

var Google = require("http://maps.google.com/maps/api/js?sensor=false");

那么,如何让我的 Google API 再次工作以及如何使用 requireJS 来做到这一点

4

1 回答 1

1

requirejs 将异步加载 Maps-API,但您使用的 URL 不能用于异步加载(请参阅async loading javascript with document.write)。

怎么做:将回调参数添加到 URL,以便能够异步加载 Maps-API 并在名称等于提供的callback参数值的函数中执行与 google-maps 相关的代码。

function geocodeaddr(){
  var geocoder = new google.maps.Geocoder();
      geocoder.geocode({ 'address': 'Boston' }, function (results, status) {
         if (status == google.maps.GeocoderStatus.OK) {
            console.log(results[0].geometry.location);
         }
         else {
            console.log("Geocoding failed: " + status);
         }
      });
}
require(["http://maps.google.com/maps/api/js?sensor=false&callback=geocodeaddr"]);
于 2013-02-18T15:50:17.580 回答