在开发中项目中通过高德封装的获取经纬度的接口
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>高德定位</title>
</head>
<body>
</body>
</html>
<script type="text/javascript" src="http://webapi.amap.com/maps?v=1.3&key='开发者秘钥'&plugin=AMap.Geocoder"></script>
<script type="text/javascript">
window.onload=function(){
eflower_location.error = function(data){
}
//定位初始化
eflower_location.Gdlocation(function(data){
alert(data.position.getLng()+" "+data.position.getLat());
});
}
eflower_location={
success:function(date){},
error:null,
GdMapload:function(){
var Lbody = document.body;
var LO = document.createElement("div");
LO.style.display='none';
LO.setAttribute('id', 'ContentMap');
Lbody.appendChild(LO);
},
Gdlocation:function(_success){
if(_success != undefined && typeof(_success) == "function"){
eflower_location.success=_success;
}
eflower_location.GdMapload();
var lnglatXY=new Array();
map = new AMap.Map('container');
map.plugin('AMap.Geolocation', function() {
geolocation = new AMap.Geolocation({
enableHighAccuracy: true,//是否使用高精度定位,默认:true
timeout: 10000, //超过10秒后停止定位,默认:无穷大
});
map.addControl(geolocation);
geolocation.getCurrentPosition();
AMap.event.addListener(geolocation, 'complete',eflower_location.onComplete);//返回定位信息
AMap.event.addListener(geolocation, 'error', eflower_location.onError); //返回定位出错信息
});
},
onComplete:function(data){
eflower_location.success(data);
},
onError:function(data){
if(eflower_location.error !=null && typeof(eflower_location.error) == "function"){
eflower_location.error(data);
}else{
console.error("error");
return;
}
}
}
</script>