2017-10-18 162 views
-1

有人提议开发一个页面,在该页面中显示Google Maps的摘录,其中有几个标记以簇的形式组织(完成)。我没有使用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> 
+0

[Google Maps API - 获取最接近的邮政编码]的重复项(https://stackoverflow.com/questions/17280787/google-maps-api-getting-closest-points-to-zipcode) – geocodezip

回答

0

如果你正在谈论的空中距离,用欧几里德式上的所有,蛮快的,考虑到了一切。只需修改一下,不要使用平方根,因为这是一个昂贵的操作。

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

应用一个基本的最小alogirthm,即总是保存最低距离点。

这是简单的方法,一般也是最近的点,如果发生远的地方。



做前面的道路距离的方式会一直做一个接一个,但谷歌谢天谢地认为这种情况和发展https://developers.google.com/maps/documentation/distance-matrix/intro

我不知道使用额度因为你的账户将是,但这取决于你和你的公司根据你想要的计划的类型。