2013-07-02 131 views
0

JS改變:顯示標記的信息窗口時,標記在谷歌地圖

var map; 

function pageLoad() { 
    map = new google.maps.Map(document.getElementById('map'), { 
     zoom: 3, 
     center: new google.maps.LatLng(34.454543, 35.812997), 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }); 
    var infowindow = new google.maps.InfoWindow(); 
} 

function getRandomArbitary(min, max) { 
    return Math.random() * (max - min) + min; 
} 

function SetLocation() { 
    var latitude = getRandomArbitary(1, 30); 
    var longitude = getRandomArbitary(1, 30); 
    var locationlatLng = new google.maps.LatLng(latitude, longitude); 
    var marker = new google.maps.Marker({ 
     'position': locationlatLng, 
     'map': map 
    }); 
    map.panTo(locationlatLng); 
} 

HTML:

<div id="map" style="width: 900px; height: 600px"> 
<a href="javascript:void(0)" onclick="SetLocation();">Set Custom Location</a> 

當創建一個新的標誌物上設定的自定義位置用戶點擊地圖上的不眠不休,以但是如何在創建新標記時動態顯示infowindow?

回答

0

點擊標記(與google.maps.event.trigger)創建之後(當然,你需要點擊監聽器來打開信息窗口,有些內容信息窗口):

var map; 
var infowindow; 

function pageLoad() { 
    map = new google.maps.Map(document.getElementById('map'), { 
     zoom: 3, 
     center: new google.maps.LatLng(34.454543, 35.812997), 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }); 
    infowindow = new google.maps.InfoWindow(); 
} 

function getRandomArbitary(min, max) { 
    return Math.random() * (max - min) + min; 
} 

function SetLocation() { 
    var latitude = getRandomArbitary(1, 30); 
    var longitude = getRandomArbitary(1, 30); 
    var locationlatLng = new google.maps.LatLng(latitude, longitude); 
    var marker = new google.maps.Marker({ 
     'position': locationlatLng, 
     'map': map 
    }); 
    google.maps.event.addListener(marker,'click',function(e){ 
     if (e && e.latLng) { 
      infowindow.setContent("location:" +e.latLng); 
     } else { 
      infowindow.setContent("location:" +marker.getPosition()); 
     } 
     infowindow.open(map,marker); 
    }); 
    google.maps.event.trigger(marker,'click'); 

    map.panTo(locationlatLng); 
} 
google.maps.event.addDomListener(window, 'load', pageLoad); 

working example

相關問題