本例採用html5 Geolocation 定位服務 和 google maps api 3 完成,運行時請確保你的瀏覽器支持 html5.
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0px; padding: 0px }
#map_canvas { height: 100% }
</style>
<script type="text/javascript"
src="http://maps.google.com/maps/api/js?v=3.7&sensor=false">
</script>
<script type="text/javascript">
function initialize(position) {
var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
//
position.coords.latitude 經度
//
position.coords.longitude 緯度
var myOptions = {
zoom: 18,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementByIdx_x("map_canvas"),myOptions);
var marker = new google.maps.Marker({
position: latlng,
map: map,
title:"你在這裏(精確到" + position.coords.accuracy + "米)"
});
}
function error(msg){
alert(msg);
}
(function(){
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(initialize,error);
}else{
error("親!很遺憾的告訴您,您的瀏覽器不支持HTML5 請升級版本");
}
})();
</script>
</head>
<body >
<div id="map_canvas"></div>
</body>
</html>