<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>基于浏览器的HTML5查找地理位置</title>
<!-- 百度API -->
<script src="http://api.map.baidu.com/api?v=1.2" type="text/javascript"></script>
<script>
function getLocation(){
var options={
enableHighAccuracy:true,
maximumAge:1000
}
if(navigator.geolocation){
//浏览器支持geolocation
navigator.geolocation.getCurrentPosition(onSuccess,onError,options);
}else{
//浏览器不支持geolocation
}
}
//成功时
function onSuccess(position){
var longitude =position.coords.longitude;
var latitude = position.coords.latitude;
var map =new BMap.Map("container");
//创建一个坐标
var point =new BMap.Point(longitude,latitude);
//地图初始化,设置中心点坐标和地图级别
map.centerAndZoom(point,20);
}
//失败时
function onError(error){
switch(error.code){
case 1:
alert("位置服务被拒绝");
break;
case 2:
alert("暂时获取不到位置信息");
break;
case 3:
alert("获取信息超时");
break;
case 4:
alert("未知错误");
break;
}
}
window.onload=getLocation;
</script>
</head>
<body>
<div id="container" style="width:600px;height:600px"></div>
</body>
</html>