關於定位,分為GPS定位和網絡定位。本文將詳細描述的瀏覽器定位,屬於網絡定位。這是一種通過使用高德JS-API來實現位置定位、城市定位的方法,包含了IP定位,檢索等多種網絡定位方式。如果您的手機支持GPS功能,能夠自動獲取GPS信息,定位將更加准確。
瀏覽器定位
瀏覽器定位插件,封裝了標准的HTML5定位,並含糾正模塊,同時該定位方式僅適用於支持HTML5的瀏覽器上,如Internet Explorer 9、Firefox、Chrome、Safari 以及 Opera等。代碼如下:
/**
* Created by ly-wangweiq on 2015/7/29.
* * support mobile
*/
//用戶位置定位 使用geolocation定位
var mMap=function(){
function rad(d){
return d*Math.PI/180.0;
}
this.map={},
this.geolocation={},
this.k=0,
//加載地圖,調用瀏覽器定位服務
this.initMap=function(mapContainer,completFunc){
if(typeof(AMap)=="object"){
this.map = new AMap.Map(mapContainer, {
resizeEnable: true
});
this.map.plugin('AMap.Geolocation', function () {
this.geolocation = new AMap.Geolocation({
enableHighAccuracy: true,//是否使用高精度定位,默認:true
timeout: 10000, //超過10秒后停止定位,默認:無窮大
maximumAge: 0, //定位結果緩存0毫秒,默認:0
convert: true, //自動偏移坐標,偏移后的坐標為高德坐標,默認:true
showButton: true, //顯示定位按鈕,默認:true
buttonPosition: 'LB', //定位按鈕停靠位置,默認:'LB',左下角
buttonOffset: new AMap.Pixel(10, 20),//定位按鈕與設置的停靠位置的偏移量,默認:Pixel(10, 20)
showMarker: true, //定位成功后在定位到的位置顯示點標記,默認:true
showCircle: true, //定位成功后用圓圈表示定位精度范圍,默認:true
panToLocation: true, //定位成功后將定位到的位置作為地圖中心點,默認:true
zoomToAccuracy:true //定位成功后調整地圖視野范圍使定位位置及精度范圍視野內可見,默認:false
});
this.map.addControl(this.geolocation);
AMap.event.addListener(this.geolocation, 'complete', onComplete);//返回定位信息
AMap.event.addListener(this.geolocation, 'error', onError); //返回定位出錯信息
});
function onComplete(data){
console.log(completFunc)
console.log(data)
if(completFunc){
completFunc(data);
}
}
function onError(){
var str = '定位失敗,';
str += '錯誤信息:'
switch(data.info) {
case 'PERMISSION_DENIED':
str += '瀏覽器阻止了定位操作';
break;
case 'POSITION_UNAVAILBLE':
str += '無法獲得當前位置';
break;
case 'TIMEOUT':
str += '定位超時';
break;
default:
str += '未知錯誤';
break;
}
alert(str)
}
}
},
this.getCurrentPosition=function(callback){
if(typeof(this.geolocation.getCurrentPosition)!='undefined'){
this.geolocation.getCurrentPosition();
}else{
setTimeout(function(){
//將獲得的經緯度信息,放入sessionStorge
this.getSessionLocation(callback)
},200)
}
},
this.distance = function(obj1,obj2){//return:m
var lng=new AMap.LngLat(obj1.lng, obj1.lat);
var lag=new AMap.LngLat(obj2.lng, obj2.lat);
var ss=lng.distance(lag);
return ss;
},
this.getSessionLocation=function(callback){
if(sessionStorage.getItem('location')){
callback();
}else{
this.initMap('',function(data){
sessionStorage.setItem("location",JSON.stringify(data))
callback();
});
this.getCurrentPosition(callback);
}
},
/*
*兩點之間的距離
*(lng1.lat1)地址一的經緯度
*(lng2.lat2)地址一的經緯度
*單位米
*/
this.serverDistance = function(obj1,obj2){//return:m
var radLat1 = rad(obj1.lat);
var radLat2 = rad(obj2.lat);
var a = radLat1 - radLat2;
var b = rad(obj1.lng)- rad(obj2.lng);
var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) + Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
s = s *6378137;
s = Math.round(s * 10000)/10000 ;
return s;
}
return this;
}();
這里將定位獲取的信息存入sessionStorge中,這樣只需要首次訪問時,需要定位,之后都可以從sessionStorge中得到,大大提高了速度。
下面將演示調用定位和兩點距離的實例。
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no"/>
<title></title>
<script language="javascript" src="http://webapi.amap.com/maps?v=1.3&key=e8496e8ac4b0f01100b98da5bde96597"></script>
<script src="mAmaplbs.js"></script>
</head>
<body>
<a id="distance" onclick="getDistance()">獲取距離</a>
<script>
//獲取當前位置(方法名)
mMap.getSessionLocation(locationFunc)
function locationFunc(){
var data = JSON.parse(sessionStorage.getItem("location"));
alert("lng:"+data.position.lng)
alert("lat:"+data.position.lat)
}
// 獲取兩點的距離 (m)
function getDistance(){
var obj1={lng:116.39,lat: 39.98};
var obj2={lng:116.39,lat: 38.98};
alert(mMap.distance(obj1,obj2));
mMap.serverDistance(obj1,obj2);
}
</script>
</body>
</html>
其中”webapi.amap.com/maps?v=1.3&key=e8496e8ac4b0f01100b98da5bde96597這里面的key,需要在高德API網站獲取[http://lbs.amap.com/]。