2009年10月24日

Google Map API 地理編碼


<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript"
src="http://www.google.com/jsapi?key=ABQIAAAAzr2EBOXUKnm_jVnk0OJI7xSosDVG8KKPE1-m51RBrvYughuyMxQ-i1QfUnH94QxWIa6N4U6MouMmBA"></script>
<script type="text/javascript">
google.load("maps", "2.x");

var map = null;
var geocoder = null;
var marker = null;

function updateLatLng(point) {
document.getElementById("Lat").value = point.y;
document.getElementById("Lng").value = point.x;
}

function initialize() {
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map_canvas"));
map.addControl(new GSmallMapControl());
map.addControl(new GMenuMapTypeControl());
map.setMapType(G_HYBRID_MAP);

var point = new GLatLng(25.033668, 121.519913);
map.setCenter(point, 7);
marker = new GMarker(point, {
draggable : true
});
map.addOverlay(marker);

GEvent.addListener(marker, "dragstart", function() {
map.closeInfoWindow();
});
GEvent.addListener(marker, "dragend", function() {
updateLatLng(marker.getLatLng());
});

geocoder = new GClientGeocoder();
}
}

function showAddress(address) {
if (geocoder) {
geocoder.getLatLng(address, function(point) {
if (!point) {
alert(address + " not found");
} else {
map.setCenter(point, 13);
marker.setLatLng(point);
marker.openInfoWindowHtml(address);
updateLatLng(point);
}
});
}
}
</script>
<title></title>
</head>
<body onload="initialize()" onunload="GUnload()">
<div id="map_canvas" style="width: 500px; height: 300px">
</div>
<input type="text" name="address" id="addressId" value="台北市中正紀念堂"/>
<input type="button" value="查詢經緯度" onclick="showAddress(document.getElementById('addressId').value);" /> <br />
緯度<input type="text" name="Lat" id="Lat" />
經度<input type="text" name="Lng" id="Lng" />
</body>
</html>

沒有留言:

網誌存檔