Javascript 通过ajax()请求获取Google Maps API的地址坐标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5623675/
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-23 18:06:56  来源:igfitidea点击:

Get address coordinates of Google Maps API by ajax() request

javascriptjqueryajaxgoogle-mapsgoogle-maps-api-3

提问by Guido Lemmens 2

I'm trying to get lng and lat coordinates of the Google Maps API by the next example http://jsbin.com/inepo3/7/edit. I expect a 'success' popup, but it keeps showing the 'Error' popup. The google maps-request gives the correct json feedback (checked by firebug).

我正在尝试通过下一个示例http://jsbin.com/inepo3/7/edit获取 Google Maps API 的 lng 和 lat 坐标。我期待“成功”弹出窗口,但它一直显示“错误”弹出窗口。google maps-request 给出了正确的 json 反馈(由 firebug 检查)。

<script type="text/javascript">
    $().ready(function() {

        $.fn.getCoordinates=function(address){

            $.ajax(
            {
                type : "GET",
                url: "http://maps.google.com/maps/api/geocode/json",
                dataType: "jsonp",
                data: {
                    address: address,
                    sensor: "true"
                },
                success: function(data) {
                    set = data;
                    alert(set);
                },
                error : function() {
                    alert("Error.");
                }
            });
        };

        $().getCoordinates("Amsterdam, Netherlands");
    });
</script>

Does anyone know how to fix this issue?

有谁知道如何解决这个问题?

Regards, Guido Lemmens

问候, Guido Lemmens

EDIT

编辑

I found a bether solution using the Google Maps Javascript API combined in jQuery:

我找到了一个使用 Google Maps Javascript API 结合 jQuery 的更好的解决方案:

<script type="text/javascript">
    $().ready(function() {
        var user1Location = "Amsterdam, Netherlands";
        var geocoder = new google.maps.Geocoder();
        //convert location into longitude and latitude
        geocoder.geocode({
            address: user1Location
        }, function(locResult) {
            console.log(locResult);
            var lat1 = locResult[0].geometry.location.lat();
            var lng1 = locResult[0].geometry.location.lng();
            $("#testDiv").html("latitude:" + lat1 + "<p>longitude:" + lng1 + "</p>");
        });
    });
</script>

回答by u476945

Google Map API V3 makes it harder for external libraries to work with JSONP. Here is a blog post about it.

Google Map API V3 使外部库更难使用 JSONP。这是一篇关于它的博客文章。

JSONP and Google Maps API Geocoder Plus A Fix w/ jQuery

JSONP 和 Google Maps API Geocoder Plus A Fix w/ jQuery



An alternative way of getting Geocoding is to use the Google Map V3 API Geocoder Service. Here is an example that i helped a person that was having a similar issue as you to replace his JSONP to use Google Map V3 Geocoder Service. Take a look at this JSFiddle Demo:

另一种获取地理编码的方法是使用Google Map V3 API Geocoder Service。这是一个示例,我帮助一个遇到类似问题的人替换了他的 JSONP 以使用 Google Map V3 Geocoder Service。看看这个JSFiddle 演示:

This is basically the core. We basically use twitter to get the tweet's address (IE. London, Madrid or Georgia etc) and convert the actual address into LatLng using Google Map's Geocoder Service:

这基本上是核心。我们基本上使用 twitter 来获取推文的地址(即伦敦、马德里或乔治亚州等),并使用谷歌地图的地理编码服务将实际地址转换为 LatLng:

$.getJSON(
    url1, function(results) { // get the tweets
        var res1 = results.results[0].text;
        var user1name = results.results[0].from_user;
        var user1Location = results.results[0].location;

        // get the first tweet in the response and place it inside the div
        $("#last-tweet1").html(res1 + "<p>from: " + user1name + " (" + user1Location + ")</p><p>");
        //convert location into longitude and latitude
        geocoder.geocode({
            address: user1Location
        }, function(locResult) {
            console.log(locResult);
            var lat1 = locResult[0].geometry.location.lat();
            var lng1 = locResult[0].geometry.location.lng();
            $("#testDiv").html("latitude:" + lat1 + "<p>longitude:" + lng1 + "</p>");
        });
    });