2012-09-22 225 views
0

試圖創建一個新版本的地圖功能在這裏看到:http://www.daftlogic.com/projects-google-maps-distance-calculator.htm 但使用v3 api。谷歌地圖v3距離

到目前爲止,我可以在點擊時設置標記並繪製測地折線。 我目前運行到的問題是:

  • 更新上標記拖動折線
  • 我敢肯定,我必須把每個標記在一個陣列和 循環,這樣做我可以繼續點擊並添加點數,將會增加總距離 。
  • 正確顯示距離。

我創建了一個的jsfiddle:http://jsfiddle.net/wyZyS/

編輯:我知道我沒有什麼叫 「更新」 功能。我正在嘗試爲當前每個標記創建數組。你看到的計算是將米轉換爲海里。

回答

0

前段時間我寫了一些這樣的練習。不幸的是,它只能在Chrome瀏覽器中正常工作,在其他瀏覽器中存在奇怪的錯誤。距離函數應該在IE9中工作。

我的想法是將drag監聽器添加到您的標記中,並保留代表路徑的單個多段線,並在拖動標記時更新其路徑。

DEMOhttp://freezoo.alwaysdata.net/freezoomaps/freezoomaps.html

 google.maps.event.addListener(tmpNode, 'drag', function(event) { 
     distance.drawPath(); 
     }); 


    distance.drawPath = function() { 
     distance.countNodes(); 
     var coords = []; 
     for (var i = 0; i < distance.nodes.length; i++) { 
     coords.push(distance.nodes[i].getPosition()); 
     } 
     distance.line.setPath(coords); 

     meters = google.maps.geometry.spherical.computeLength(coords); 
     $("#distance_km").val((meters/1000).toFixed(3)); 
     $("#distance_mi").val((meters/1609.344).toFixed(3)); 
    } 

    distance.countNodes = function() { 
     var count = 0; 
     for (var i = distance.nodes.length - 1; i >= 0; i--) { 
     if (distance.nodes[i].getMap() == null) { 
      distance.nodes.splice(i, 1); 
     } 
     else { 
      count++; 
     } 
     } 
     return count; 
    } 

    distance.clearNodes = function() { 
     for(var i = 0; i < distance.nodes.length; i++) { 
     distance.nodes[i].setMap(null); 
     } 
     distance.drawPath(); 
    } 
+0

謝謝你的幫助。我可能沒有提到我在js上有點小菜。儘管如此,沒有像大多數那樣不太好。我正在尋找與我的腳本的具體幫助,使用我已經在我鏈接到的js小提琴中使用的變量。我瞭解你正在向我展示的內容我現在試圖弄清楚如何實施到我的項目中。 – Shane