如何使用 jquery ajax 调用谷歌地图地理编码服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5191325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:42:32  来源:igfitidea点击:

How to call google map geocode service with jquery ajax

jqueryajaxgoogle-mapsgeocode

提问by rajakvk

I'm trying to get the LatLng from google map geocode api. my code is below with address values is "New York, NY"

我正在尝试从谷歌地图地理编码 api 获取 LatLng。我的代码在下面,地址值为“纽约,纽约”

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

But I'm not getting any values in alert.

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

Thanks for any help.

谢谢你的帮助。

采纳答案by Fokko Driesprong

You will need to contact the API regarding the rules.

您需要就规则联系 API 。

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

Edit:

编辑:

How dumb of me, shouldn't be on stackoverflow in the morning! The problem is a cross-domain request. For security reasons this is not allowed. See: How to make cross-domain AJAX calls to Google Maps API?

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

Please use Google's own Geocoding client.

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

回答by Melvin

there is no need to make use of jquery, Google maps javascript API v3 has build in its own functions which makes the API easy to use.

无需使用 jquery,Google maps javascript API v3 内置了自己的功能,这使得 API 易于使用。

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

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
        });
    }
});

回答by user2534532

If anyone is seeking help on this topic, this is how I have done it. Note this is a reverse lookup, but a forward lookup should work the same way:

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

`

`

    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;
      });
     }`

回答by Brankodd

V3 geocoding webservice doesn't support JSONP requests, but it's still available in V2

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

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

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

回答by HelpNeeder

This is how I did it. Geocoder seems like is creating it's own callback.

我就是这样做的。Geocoder 似乎正在创建它自己的回调。

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();
    }
});

回答by HelpNeeder

or specify dataType to jsonp?

或指定数据类型为 jsonp?

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