2017-10-18 198 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

我不知道使用額度因爲你的賬戶將是,但這取決於你和你的公司根據你想要的計劃的類型。

相關問題