15

我正在尝试从 google map geocode api 获取 LatLng。我的代码在下面,地址值为“纽约,纽约”

$.ajax({
    url: 'http://maps.googleapis.com/maps/api/geocode/json',
    data: {
        sensor: false,
        address: address
    },
    success: function (data) {
        alert(data)
    }
});

但我没有得到任何警报值。

谢谢你的帮助。

4

6 回答 6

14

您需要就规则联系 API 。

$.getJSON( {
    url  : 'https://maps.googleapis.com/maps/api/geocode/json',
    data : {
        sensor  : false,
        address : address
    },
    success : function( data, textStatus ) {
        console.log( textStatus, data );
    }
} );

编辑:

我真笨,早上不应该在stackoverflow上!问题是跨域请求。出于安全原因,这是不允许的。请参阅:如何对 Google Maps API 进行跨域 AJAX 调用?

请使用谷歌自己的地理编码客户端

于 2011-03-04T08:33:35.060 回答
10

不需要使用 jquery,Google maps javascript API v3 已经内置了自己的函数,这使得 API 易于使用。

https://developers.google.com/maps/documentation/javascript/geocoding https://developers.google.com/maps/documentation/geocoding/intro

var geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': address }, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location
        });
    }
});
于 2015-07-24T12:26:55.080 回答
3

如果有人在这个话题上寻求帮助,我就是这样做的。请注意,这是反向查找,但正向查找应该以相同的方式工作:

`

    function getLocation() {
var latdegrees=40.2298;
var londegrees=-41.88754;
     var url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + latdegrees + "," + londegrees + "YOUR API KEY";
    $.getJSON(url,function (data, textStatus) {
           var streetaddress=data.results[0].formatted_address;
          return streetaddress;
      });
     }`
于 2015-03-22T15:26:50.410 回答
1

我就是这样做的。地理编码器似乎正在创建它自己的回调。

geo.geocode({ 'address': myaddress }, function(data, status) {
    if (status == google.maps.GeocoderStatus.OK) { 
        var myylocation = data[0].geometry.location;
        lat = myylocation.lat();
        lng = myylocation.lng();
    }
});
于 2015-01-20T01:18:07.753 回答
1

V3 地理编码网络服务不支持 JSONP 请求,但在 V2 中仍然可用

请参阅:http ://blog.futtta.be/2010/04/09/no-more-jsonp-for-google-geocoding-webservice/

于 2012-12-27T03:33:59.210 回答
0

或将dataType指定为jsonp?

$.ajax({
    url: 'http://maps.googleapis.com/maps/api/geocode/json',
    data: {
        sensor: false,
        address: address
    },
    dataType:'jsonp',
    success: function (data) {
        alert(data)
    }
});
于 2012-11-23T01:38:20.903 回答