2011-11-25 160 views

回答

8

您遍歷數組,併爲每對創建一個新的Marker實例。這很簡單:

<script> 
    var map = new google.maps.Map(document.getElementById('map'), { 
     center: new google.maps.LatLng(55.378051, -3.435973), 
     mapTypeId: google.maps.MapTypeId.ROADMAP, 
     zoom: 8 
    }); 

    var locations = [ 
     new google.maps.LatLng(54.97784, -1.612916), 
     new google.maps.LatLng(55.378051, -3.435973]) 
     // and additional coordinates, just add a new item 
    ]; 

    locations.forEach(function (location) { 
     var marker = new google.maps.Marker({ 
      position: location, 
      map: map 
     }); 
    }); 
</script> 

這適用於任何數量的緯度/經度對;只需將一個新項目添加到locations陣列。

+0

好主意!我的情況是,我有一堆需要轉換爲座標的位置名稱。這些位置名稱是從文檔中提取的,這些文檔是搜索引擎的搜索結果。輸入是文檔的自由文本,輸出是帶有顯示這些位置名稱的引腳的地圖。這個JavaScript只需要數組(固定數據庫)作爲輸入,可以在上述情況下完成?非常感謝。 – Jun

+0

您的位置名稱是否是任何特定的格式,或者它們是否以原始文檔中寫入的任何格式從搜索引擎結果中的文本中提取? –

+0

例如,在谷歌的搜索結果中,有可能包含地理位置的文本文檔。首先,我想查找這些位置,然後可能會調用Web服務將名稱轉換爲可能存儲在某處的座標。之後,調用Google地圖API將這些名稱顯示爲地圖中的引腳。 – Jun

0

,你可以這樣做

var map = new GMap2(document.getElementById("map_canvas")); 
    map.addControl(new GSmallMapControl()); 
    map.setCenter(new GLatLng(37.4419, -122.1419), 13); 

    // Create our "tiny" marker icon 
    var blueIcon = new GIcon(G_DEFAULT_ICON); 
    blueIcon.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png"; 

    // Set up our GMarkerOptions object 
    markerOptions = { icon:blueIcon }; 

    // Add 10 markers to the map at random locations 
    var bounds = map.getBounds(); 
    var southWest = bounds.getSouthWest(); 
    var northEast = bounds.getNorthEast(); 
    var lngSpan = northEast.lng() - southWest.lng(); 
    var latSpan = northEast.lat() - southWest.lat(); 
    for (var i = 0; i < 10; i++) { 
     var point = new GLatLng(southWest.lat() + latSpan * Math.random(), 
     southWest.lng() + lngSpan * Math.random()); 
     map.addOverlay(new GMarker(point, markerOptions)); 
    } 

我已經採取了從谷歌API文檔這個例子中,他們表現出瞭如何使用LAT翁展現東西

請參閱本作的細節

Showing markers using Google API

雖然我已經使用v2已被棄用,但使用的概念將大致相同

+0

這是javascript。 – Jun

+0

我目前正嘗試從搜索引擎返回的結果文檔中提取地理位置名稱,並將它們顯示爲地圖中的引腳。所以有我需要翻譯到座標,然後調用谷歌地圖顯示圖像的位置名稱。任何想法? – Jun

+0

您能詳細說明您的要求嗎?因爲在您的問題中,您告訴您有緯度經度,因此將Google地圖服務稱爲「經緯度」並非難事,點不過是根據經度和緯度進行計算。 –

相關問題