本例采用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>
来源:oschina
链接:https://my.oschina.net/u/131183/blog/56583