zoukankan      html  css  js  c++  java
  • 百度地图api定位,并根据经纬度转换为地址

    方法一

     1 //浏览器定位,并根据定位经纬度转换为地址描述。
     2 var positionOptions = {
     3     enableHighAccuracy: true,
     4     timeout: 3000,
     5     maximumAge: 0
     6 };
     7 var geolocation = new BMap.Geolocation();
     8 geolocation.getCurrentPosition(function(geolocationResult){
     9     if(geolocationResult!=null){
    10         alert(geolocationResult.point.lng)
    11         // alert(geolocationResult.accuracy)
    12         console.log(geolocationResult.point)
    13         var myGeo = new BMap.Geocoder();
    14         // 根据坐标得到地址描述
    15         myGeo.getLocation(new BMap.Point(geolocationResult.point.lng,geolocationResult.point.lat), function(result){
    16             console.log(result.addressComponents)
    17             console.log(result)
    18             console.log(geolocationResult.point)
    19             if (result){
    20                 location = result.address
    21                 alert(location);
    22             }
    23         });
    24     }
    25 }, positionOptions);

    方法二

     1 var geolocation = new BMap.Geolocation();
     2 geolocation.getCurrentPosition(function(r){
     3     if(this.getStatus() == BMAP_STATUS_SUCCESS){
     4         //根据定位经纬度添加标记
     5         var mk = new BMap.Marker(r.point);
     6         map.addOverlay(mk);
     7         console.log(r.point)
     8         map.panTo(r.point);
     9         alert('您的位置:'+r.point.lng+','+r.point.lat);
    10         //根据坐标获取地理位置
    11         map.centerAndZoom(new BMap.Point(r.point.lng,r.point.lat), 10);
    12         // 创建地理编码实例
    13         var myGeo = new BMap.Geocoder();
    14         // 根据坐标得到地址描述
    15         myGeo.getLocation(new BMap.Point(r.point.lng,r.point.lat), function(result){
    16             console.log(result.addressComponents);
    17             console.log(result)
    18             console.log(r.point)
    19             if (result){
    20                 location = result.address
    21                 alert(location);
    22             }
    23         });
    24     }
    25     else {
    26         alert('failed'+this.getStatus());
    27     }
    28 });
  • 相关阅读:
    多线程环境下调用 HttpWebRequest 并发连接限制
    i—比 i++ 快?
    文件在不同文件系统间拷贝文件时间改变的问题
    Go websocket 聊天室demo2
    Go websocket 聊天室demo以及k8s 部署
    AcWing 1077. 皇宫看守
    AcWing 1073. 树的中心
    AcWing 1085. 不要62
    AcWing 1081 度的数量
    AcWing 1082. 数字游戏
  • 原文地址:https://www.cnblogs.com/qing0228/p/14386092.html
Copyright © 2011-2022 走看看