【问题标题】:limiting google maps autocomplete to 50 MILES from the location given address only将谷歌地图自动完成限制为仅从给定地址的位置 50 英里
【发布时间】:2017-12-03 23:22:54
【问题描述】:

我已经浏览了下面链接中的示例 https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete?csw=1

我尝试了这个例子,但没有得到预期的输出地址超出了我提到的半径范围,下面我添加了我的自动完成代码请告诉我我出了什么问题

var center22 = new google.maps.LatLng({ lat: 51.448386, lng: -0.199501 });
    service = new google.maps.places.AutocompleteService();


    service.getPlacePredictions({
        input: query, location: center22, radius: 50000, strictbounds: true, componentRestrictions: { country: 'GB' }

    }, function (predictions, status) {
        if (status != google.maps.places.PlacesServiceStatus.OK) {               
            dfd.resolve([]);


        }       


            dfd.resolve(predictions);
            return dfd.promise

        }
    });

【问题讨论】:

  • 您的代码 (Uncaught SyntaxError: missing ) after argument list) 出现语法错误。请提供一个 minimal reproducible example 来证明该问题。
  • @geocodezip ,在将代码添加到其中时,它最后错过了右大括号,但有一个大括号,我正在通过更改它来使用此代码。但是地址超出了半径限制。,请给我建议
  • 它仍然不是minimal reproducible example。这只是您没有测试您发布的代码以验证它是否存在问题这一事实的一个症状。

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


【解决方案1】:

尚未为 AutocompleteService 实现严格的界限。目前您只能将结果偏向某些区域,但不能限制。

https://developers.google.com/maps/documentation/javascript/reference#AutocompletionRequest

在公共问题跟踪器中有一个功能请求:

https://issuetracker.google.com/issues/36219203

欢迎对此功能请求加注星标以表达您的兴趣并订阅通知。

【讨论】:

    【解决方案2】:

    您可以使用缩放。 初始化地图时可以设置缩放大小。

        var map = new google.maps.Map(document.getElementById('map'), {
          center: {lat: -33.8688, lng: 151.2195},
          zoom: 13
        });
        map.setZoom(12);
    

    【讨论】:

    • 我的问题是关于搜索地址的限制自动完成位置预测,而不是地图的缩放,无论如何感谢您的重播
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-23
    • 2017-09-16
    • 2021-04-12
    • 1970-01-01
    • 2017-04-12
    • 2012-02-20
    • 2015-09-16
    相关资源
    最近更新 更多