获取坐标,测量距离并进行比较

时间:2017-10-18 13:07:04

标签: javascript google-maps geolocation coordinates distance

有人建议开发一个页面,在这个页面中,您可以使用多个标记(已完成)组织Google地图的摘录。我没有使用javascript的经验,我需要的是点击一个按钮,是否可以获取我的位置并自动获知地图上标记的哪个点离我更近。有人能帮我吗?     

      function initMap() {

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

        // Create an array of alphabetical characters used to label the markers.
        var labels = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

        // Add some markers to the map.
        // Note: The code uses the JavaScript Array.prototype.map() method to
        // create an array of markers based on a given "locations" array.
        // The map() method here has nothing to do with the Google Maps API.
        var markers = locations.map(function(location, i) {
          return new google.maps.Marker({
            position: location,
            label: labels[i % labels.length]
          });
        });

        // Add a marker clusterer to manage the markers.
        var markerCluster = new MarkerClusterer(map, markers,
            {imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});
      }
      var locations = [
        {lat:  40.962157, lng: -8.593313},
        {lat:  40.962149, lng: -8.595695},
        {lat:  40.960351, lng: -8.598922},   
        {lat:  40.967305, lng: -8.591450},
        {lat:  40.961682, lng: -8.608136}
      ]

    </script>

1 个答案:

答案 0 :(得分:0)

如果你在谈论空中距离,考虑到一切,使用欧几里德公式,非常快。只需稍微修改它,这样你就不会使用平方根,因为这是一项昂贵的操作。

(Xpos - Xpoint)^2 + (Ypos - Ypoint)^2

应用基本的min alogirthm,即始终保存最低距离点。

这是一种简单的方法,通常也是最近点,如果远离它的地方。



之前做道路距离的方法是逐个做到这一点,但谷歌幸好地想到了这个案例并制定了https://developers.google.com/maps/documentation/distance-matrix/intro

我不确定您的帐户的使用配额是多少,但您和您的公司需要根据您想要的计划类型来确定。