Google 地圖事件
Google 地圖事件
點擊標記縮放地圖- 綁定在谷歌地圖上的事件。
點擊標記縮放地圖
我們仍然使用上一遍文章使用的英國倫敦的地圖。
點使用者點擊標記時實作縮放地圖的功能(點擊標記時綁定地圖縮放事件)。
程式碼如下:
實例
<html> <head> <script src="http://maps.googleapis.com/maps/api/js?key=AIzaSyDY0kkJiTPVd2U7aTOAwhc9ySH6oHxOIYM&sensor=false"> </script> <script> var myCenter=new google.maps.LatLng(51.508742,-0.120850); function initialize() { var mapProp = { center: myCenter, zoom:5, mapTypeId: google.maps.MapTypeId.ROADMAP }; var map = new google.maps.Map(document.getElementById("googleMap"),mapProp); var marker = new google.maps.Marker({ position: myCenter, title:'Click to zoom' }); marker.setMap(map); // Zoom to 9 when clicking on marker google.maps.event.addListener(marker,'click',function() { map.setZoom(9); map.setCenter(marker.getPosition()); }); } google.maps.event.addDomListener(window, 'load', initialize); </script> </head> <body> <div id="googleMap" style="width:500px;height:380px;"></div> </body> </html>
#執行實例»##點擊"執行實例"按鈕查看線上實例
重置標記我們透過在地圖上新增事件處理程序來改變'center' 屬性,以下程式碼使用center_changed 事件在3秒後標記移會中心點:
實例
運行實例»點擊"運行實例" 按鈕查看線上實例
<html> <head> <script src="http://maps.googleapis.com/maps/api/js?key=AIzaSyDY0kkJiTPVd2U7aTOAwhc9ySH6oHxOIYM&sensor=false"> </script> <script> var myCenter=new google.maps.LatLng(51.508742,-0.120850); function initialize() { var mapProp = { center: myCenter, zoom:5, mapTypeId: google.maps.MapTypeId.ROADMAP }; var map = new google.maps.Map(document.getElementById("googleMap"),mapProp); var marker = new google.maps.Marker({ position: myCenter, title:'Click to zoom' }); marker.setMap(map); // Zoom to 9 when clicking on marker google.maps.event.addListener(marker,'click',function() { map.setZoom(9); map.setCenter(marker.getPosition()); }); google.maps.event.addListener(map,'center_changed',function() { // 3 seconds after the center of the map has changed, pan back to the marker window.setTimeout(function() { map.panTo(marker.getPosition()); },3000); }); } google.maps.event.addDomListener(window, 'load', initialize); </script> </head> <body> <div id="googleMap" style="width:500px;height:380px;"></div> </body> </html>
運行實例»點擊"運行實例" 按鈕查看線上實例
點擊標記時開啟訊息視窗。 點擊標記在資訊視窗顯示一些文字訊息:
實例#
執行實例»點擊"運行實例" 按鈕查看線上實例
<html> <head> <script src="http://maps.googleapis.com/maps/api/js?key=AIzaSyDY0kkJiTPVd2U7aTOAwhc9ySH6oHxOIYM&sensor=false"> </script> <script> var myCenter=new google.maps.LatLng(51.508742,-0.120850); function initialize() { var mapProp = { center:myCenter, zoom:5, mapTypeId:google.maps.MapTypeId.ROADMAP }; var map=new google.maps.Map(document.getElementById("googleMap"),mapProp); var marker=new google.maps.Marker({ position:myCenter, }); marker.setMap(map); var infowindow = new google.maps.InfoWindow({ content:"Hello World!" }); google.maps.event.addListener(marker, 'click', function() { infowindow.open(map,marker); }); } google.maps.event.addDomListener(window, 'load', initialize); </script> </head> <body> <div id="googleMap" style="width:500px;height:380px;"></div> </body> </html>
執行實例»點擊"運行實例" 按鈕查看線上實例
設定標記及開啟每個標記的資訊視窗當使用者點擊地圖時執行一個視窗使用者點擊地圖某個位置時使用placeMarker() 函數在指定位置上放置一個標記,並彈出資訊視窗:
實例
執行實例»點擊"執行實例"按鈕查看線上實例
<html> <head> <script src="http://maps.googleapis.com/maps/api/js?key=AIzaSyDY0kkJiTPVd2U7aTOAwhc9ySH6oHxOIYM&sensor=false"> </script> <script> var map; var myCenter=new google.maps.LatLng(51.508742,-0.120850); function initialize() { var mapProp = { center:myCenter, zoom:5, mapTypeId:google.maps.MapTypeId.ROADMAP }; map = new google.maps.Map(document.getElementById("googleMap"),mapProp); google.maps.event.addListener(map, 'click', function(event) { placeMarker(event.latLng); }); } function placeMarker(location) { var marker = new google.maps.Marker({ position: location, map: map, }); var infowindow = new google.maps.InfoWindow({ content: 'Latitude: ' + location.lat() + '<br>Longitude: ' + location.lng() }); infowindow.open(map,marker); } google.maps.event.addDomListener(window, 'load', initialize); </script> </head> <body> <div id="googleMap" style="width:500px;height:380px;"></div> </body> </html>
執行實例»點擊"執行實例"按鈕查看線上實例
Google 地圖- 事件參考手冊Google Maps API 參考手冊。