【问题标题】:Finding stores at input location and nearby locations and calculating their distance from searched location在输入位置和附近位置查找商店并计算它们与搜索位置的距离
【发布时间】:2014-07-10 09:50:16
【问题描述】:

我使用google map api 3 根据用户输入的地址在google maps 上定位商店。地址到latlong 的转换是使用geocoding 完成的,我的代码运行良好。现在我的客户希望我在搜索区域中绘制商店以及该区域附近的商店(附近 2 英里)并计算它们的距离。我想要的一个例子是this sitethis one。我的工作Javascript如下

var markers = response.d.split('^^');
            var latlng = new google.maps.LatLng(51.474634, -0.195791);
            var mapOptions1 = {
                zoom: 14,
                center: latlng
            }
            var geocoder = new google.maps.Geocoder();
            var infoWindow = new google.maps.InfoWindow();
            var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions1);
            for (i = 0; i < markers.length; i++) {
                var data = markers[i];
                geocoder.geocode({ 'address': data }, 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,
                            title: results[0].formatted_address
                        });


                    } else {
                        alert("Geocode was not successful for the following reason: " + status);
                    }
                });

            }
            (function (marker, data) {

                // Attaching a click event to the current marker
                google.maps.event.addListener(marker, "click", function (e) {
                    infoWindow.setContent(data);
                    infoWindow.open(map, marker);
                });
            })(marker, data);

上面的代码看起来很奇怪,我在主体onload 上进行了初始化,我的markers 数组包含所有地址,geocode 将它们转换为latlongmarker 绘制它们。 我希望 api 给我从搜索位置到每个商店的更近的地址和距离。我怎样才能得到它?

更新

可以使用 MrUpsidown 的答案计算距离,但我无法获得附近的地方。我去了here,发现https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&amp;radius=7500&amp;types=library&amp;sensor=false&amp;key=AIzaSyDRn2crkKMALHkQAOyadm-d2u_bIPeBA1o 给出了数据,但我不知道如何有效地阅读它,按照建议,我尝试使用下面的代码在 jason 中得到它

$.getJSON("https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=7500&types=library&sensor=false&key=AIzaSyDRn2crkKMALHkQAOyadm-d2u_bIPeBA1o", function (data) {
                alert(data);
            });

但没有运气:(我错过了什么?

【问题讨论】:

  • 当你得到一个问题的答案时,你至少可以给出一个反馈是否有效,并最终接受/评价给你的答案。
  • 我自己解决了很多这些问题,由于时间和资源限制,我无法发布我自己的答案,但我认为这不会导致投票失败。
  • 我取消了我的反对票,因为您对上一个问题采取了行动。请记住,给出答案和帮助他人也很耗时,我们免费为您提供时间!请参阅下面的答案。
  • 停止使用alert() 来调试你的代码!使用您的 JavaScript 控制台和console.log()。这样您就可以找出问题所在:Read more here。简单的解决方法:不要使用 AJAX 请求进行地点搜索,而是使用 Places Library

标签: javascript jquery json google-maps geocoding


【解决方案1】:

你必须自己计算距离。您可以使用geometry librarycomputeDistanceBetween 方法。

例如:

var latlng = new google.maps.LatLng(51.474634, -0.195791);

然后在地理编码器中:

var distance = google.maps.geometry.spherical.computeDistanceBetween(latlng, results[0].geometry.location);

如果您希望它们以特定顺序显示,您需要将这些标记及其距离存储在一个数组中并对其进行排序。

【讨论】:

    猜你喜欢
    • 2020-02-12
    • 1970-01-01
    • 2013-06-02
    • 1970-01-01
    • 1970-01-01
    • 2021-07-06
    • 1970-01-01
    • 2017-04-20
    • 1970-01-01
    相关资源
    最近更新 更多