2011-08-02 69 views
-3

我有我公司的地區辦事處的多個地點,並有顯示的每個位置,每當用戶點擊在像位置:的Javascript谷歌地圖

LOCATION1
LOCATION2
LOCATION3

當用戶點擊在位置1它會在地圖上顯示位置1。我的地圖中也有這些位置。我之前從未與谷歌地圖合作,所以我只需要一些想法開始。

+1

對不起,我不知道它只是現在要做,謝謝隊友:) – MNet

回答

1

當用戶點擊一個鏈接,運行一段,地圖上呼籲setCenter(latlng:LatLng)到地圖中心到一定位置的JavaScript。

如果你真的不知道從哪裏開始,然後通過讀Google Maps API文檔開始。這很容易閱讀,而且它含有大量的工作examples的。

+0

感謝兄弟,這是有幫助的:) – MNet

0

你可以做這樣的事情:(我敢肯定,它可以用一個優化的循環,其早/晚,現在

<html> 
    <head> 

    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script> 
    <script type="text/javascript"> 

    function initialize() { //Initalize JS after onload 
    var map = new google.maps.Map(document.getElementById('map_canvas'), { 
    mapTypeId: google.maps.MapTypeId.ROADMAP, 
    }); 


    var randomPoint1 = new google.maps.LatLng(44.6479, -63.5744); //First Location 

    var marker1 = new google.maps.Marker({ //Set up marker 
      position: randomPoint1, 
      map: map 
     }); 

    google.maps.event.addDomListener(document.getElementById('locationid1'), 'click', //Set up DOM listener 1 
     function(){ 
      map.setZoom(13); 
     map.setCenter(marker1.getPosition()); 
     }); 


    var randomPoint2 = new google.maps.LatLng(45.5081, -73.5550); //Second Location 

    var marker2 = new google.maps.Marker({ 
      position: randomPoint2, 
      map: map 
     }); 

    google.maps.event.addDomListener(document.getElementById('locationid2'), 'click',//Set up DOM listener 2 
     function(){ 
      map.setZoom(13); 
     map.setCenter(marker2.getPosition()); 
     }); 


    var randomPoint3 = new google.maps.LatLng(43.6481, -79.4042); //Third Location 

    var marker3 = new google.maps.Marker({ 
      position: randomPoint3, 
      map: map 
     }); 

    google.maps.event.addDomListener(document.getElementById('locationid3'), 'click', //Set up DOM listener 3 
     function(){ 
      map.setZoom(13); 
     map.setCenter(marker3.getPosition()); 
     }); 
     map.setCenter(marker2.getPosition()); 
     map.setZoom(5); 
    } 



    </script> 

    </head> 
    <body onload="initialize()"> <!--Initalize JS after onload---> 
     <a href="#" id="locationid1">Halifax</a> 
     <a href="#" id="locationid2">Montreal</a> 
     <a href="#" id="locationid3">Toronto</a> 

     <div id="map_canvas" style="height:100%; width:100%"></div> 

    </body> 

</html> 

而我寫這篇文章後,我才發現帖子是從。去年8月,好吧,可能會幫助別人

+0

你應該不需要明確重複類似的操作。 [下面是一個例子](http://www.geocodezip.com/v3_MW_example_map2.html),其使用用於創建標記(和側邊欄項,這也可能是有用的)的函數。 –