H5纯JS高德API定位
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<title>获取地理位置</title>
<style type="text/css">#iCenter{width:300px; height: 280px; border:1px #000 solid;margin:20px auto;}</style>
<script type="text/javascript" src="http://webapi.amap.com/maps?v=1.4.8&key=高德开发者创建应用申请的key"></script>
</head>
<body>
<div id="iCenter"></div>
<script type="text/javascript">
var mapObj = new AMap.Map('iCenter');
mapObj.plugin('AMap.Geolocation', function () {
geolocation = new AMap.Geolocation({
enableHighAccuracy: true,
timeout: 10000,
maximumAge: 0,
convert: true,
showButton: true,
buttonPosition: 'LB',
buttonOffset: new AMap.Pixel(10, 20),
showMarker: true,
showCircle: true,
panToLocation: true,
zoomToAccuracy:true
});
mapObj.addControl(geolocation);
geolocation.getCurrentPosition();
AMap.event.addListener(geolocation, 'complete', onComplete);
AMap.event.addListener(geolocation, 'error', onError);
});
function onComplete(obj){
var res = '经纬度:' + obj.position +
'\n精度范围:' + obj.accuracy +
'米\n定位结果的来源:' + obj.location_type +
'\n状态信息:' + obj.info +
'\n地址:' + obj.formattedAddress +
'\n地址信息:' + JSON.stringify(obj.addressComponent, null, 4);
alert(res);
}
function onError(obj) {
alert(obj.info + '--' + obj.message);
console.log(obj);
}
</script>
</body>
</html>