Google Map javascript v3 移動地圖後抓取坐標
<script type="text/javascript">
var markerCluster = null;
var map;
var listener;
//初始化
function initialize() {
var mapOptions = {
zoom: 10,
center: new google.maps.LatLng(23.56, 120.56247),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('mapcontainer'),mapOptions);
google.maps.event.addListener(map, 'dragstart', onDragstart);
google.maps.event.addListener(map, 'dragend', onDragend);
listener = google.maps.event.addListener(map, 'bounds_changed', onBounds_changed);
}
//使用者開始移動地圖
function onDragstart(){
google.maps.event.removeListener(listener);
}
//使用者結束移動地圖
function onDragend(){
listener = google.maps.event.addListener(map, 'bounds_changed', onBounds_changed);
}
var timeout;
function onBounds_changed(){
//設定一個timeout, 使用者在時間內移動都不觸發, 直到經過該時間後才觸發, 避免一直觸發
window.clearTimeout(timeout);
timeout = window.setTimeout(function() {
var bounds = map.getBounds();
$("#boundinfo").text('坐標:'+bounds.getSouthWest()+','+bounds.getNorthEast());
}, 2000);
}
google.maps.event.addDomListener(window, 'load', initialize);
0 comments: