【问题标题】:Get address coordinates of Google Maps API by ajax() request通过 ajax() 请求获取 Google Maps API 的地址坐标
【发布时间】:2011-08-03 04:19:10
【问题描述】:

我正在尝试通过下一个示例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>

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

问候, 圭多·莱蒙斯

编辑

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

<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>

【问题讨论】:

  • google map api 的哪个版本?
  • 我不是 shure,我猜是 v2(你能把用过的地图 url 转换成一个版本吗?)
  • 我很确定它现在是 V3。
  • 为什么不在 Google Maps JavaScript API 中使用地理编码器?
  • 我同意 JSONP 的这种变化基本上是谷歌强迫人们使用谷歌地图地理编码器 api 的方式

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


【解决方案1】:

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

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


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

这基本上是核心。我们基本上使用 twitter 来获取推文的地址(IE. London、Madrid 或 Georgia 等),并使用 Google Map 的 Geocoder Service 将实际地址转换为 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>");
        });
    });

【讨论】:

  • 酷,我刚刚制定了您的示例并将其添加到我的原始帖子中。这很完美!
  • @Fincha 我大约一年前回答了这个问题,但我想你可以告诉我你到目前为止在 jsfiddle 上尝试了什么。
  • @Fincha crossdomain error using Google Map v3 geocoder service?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-15
  • 2013-02-23
  • 2011-11-19
  • 1970-01-01
  • 2012-09-23
  • 1970-01-01
相关资源
最近更新 更多