【问题标题】:Why Google Places API is not executing my request?为什么 Google Places API 没有执行我的请求?
【发布时间】:2015-05-21 14:40:34
【问题描述】:

我正在尝试创建一个简单的应用程序,在其中插入搜索区域、搜索内容和搜索半径。我不明白为什么它只在我第二次执行查询搜索时才能正常工作,我知道问题可能出在我的 codeAddress() 函数中:

function codeAddress(){
                var address = document.getElementById('Zona').value;
                geocoder.geocode( { 'address': address}, function(results, status){
                    if (status == google.maps.GeocoderStatus.OK){
                    map.setCenter(results[0].geometry.location);
                    coordo = results[0].geometry.location;
                    }else{
                        alert('Geocode was not successful for the following reason: ' + status);
                    }
                });
            }

由于某种原因,我不知道它会跳过,并且不会进入 geocoder.geocode( { 'address': address}, function(results, status) 内部,但如果这是我第二次执行该函数,而不是第一次,它会进入内部。

这是应用程序:

Jsfiddle 如果您输入一个城市名称并单击一次按钮,如果您再次按预期工作时单击该按钮,它只会找到该位置,它会丢弃引脚并创建表格..有人知道为什么吗?谢谢。

【问题讨论】:

    标签: javascript html google-places-api


    【解决方案1】:

    我查看了您的代码,正如您所说,函数 codeAddress 第一次跳转。 我在google site developer上找了geocoding service的操作,发现codeAddress函数的行为和你的一样。

    为了解决您的问题,我认为您可能会在地理编码之前和启动功能搜索之后运行。

    为此,我想我会使用 jQuery 更改:

    $ ("input"). change (function () {
            //your code here
         })
    

    你的函数应该是这样的:

        $(document).ready(function(){
            $("#Zona").change(function codeAddress(){
                var address = document.getElementById('Zona').value;
                geocoder = new google.maps.Geocoder();
                geocoder.geocode( { 'address': address}, function(results, status){
                    if (status == google.maps.GeocoderStatus.OK){
                        coordo=(results[0].geometry.location);
                    }else{
                        alert('Geocode was not successful for the following reason: ' + status);
                    }
                });
            });
        });
    

    我用我的更改修改了您的示例,您可以在此处查看: http://jsfiddle.net/ftxgm0cn/

    【讨论】:

      【解决方案2】:

      你可以关注这个plunk。我已经解决了所有问题。

      【讨论】:

      • 我不明白。它对我有用。您能否更具体地说明什么不起作用?
      猜你喜欢
      • 1970-01-01
      • 2012-09-20
      • 1970-01-01
      • 1970-01-01
      • 2020-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多