js 获取地理位置经纬度

1、 加载百度API的核心js,ak表示获取百度地图的开发密钥,免费的需要申请下

  <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=42b8ececa9cd6fe72ae4cddd77c0da5d"></script>

2、代码:

  

    //设置默认地理位置 访问失败时使用默认地理位置

   var defaultPosition="121.48479060028a31.234309729773";

 1 if($.cookie("appPoi")==null||$.cookie("appPoi")=="undefined"||$.cookie("appPoi")==undefined){
 2                 var geolocation = new BMap.Geolocation();
 3                 geolocation.getCurrentPosition(function(r){
 4                     if (r.point != undefined) {
 5                         $lng = r.point.lng;
 6                         $lat = r.point.lat;
 7                     }
 8                     if (r.longitude != undefined) {
 9                         $lng = r.longitude;
10                         $lat = r.latitude;
11                     }
12                     if($lng != 0){    
13                         $.cookie("appPoi",$lng + "a" + $lat,{expires:1});
14                     }
15                     if(this.getStatus() == BMAP_STATUS_SUCCESS){
16                         appPoi = $lng + "a" + $lat;
17                     } else {
18                         appPoi = defaultPosition;
19                     }
20 
21                 },{    enableHighAccuracy: true,
22                     timeout:3000,
23                     maximumAge:5000*60});
24             }else{
25                 appPoi = $.cookie("appPoi");
26             }

在后台 接受到经纬度后裔'a'为分隔符分开并进行地址解析(解析方法见工具类)

原文地址:https://www.cnblogs.com/Wen-yu-jing/p/4081950.html