zoukankan      html  css  js  c++  java
  • 百度地图定位


    在Vue2.0里,不要用 import BMap from 'BMap' 

    class Geolocation {
    
      constructor() {
        this.cityName = ''
        this.address = ''
      }
    
      /**
       * 浏览器定位
       */
      getGeolocation(callback) {
        try{
          let self = this
          var map = new BMap.Map("allmap")
          var point = new BMap.Point(116.331398,39.897445)
          map.centerAndZoom(point,12)
          var geolocation = new BMap.Geolocation();
          geolocation.getCurrentPosition(function(r){
            if(this.getStatus() == BMAP_STATUS_SUCCESS){
              var mk = new BMap.Marker(r.point)
              map.addOverlay(mk)
              map.panTo(r.point)
              Toast('您的位置:'+r.point.lng+','+ r.point.lat);
              var gc = new BMap.Geocoder();
              let addressReal = gc.getLocation(r.point, (rs)=>{
                var addComp = rs.addressComponents;
                var address =  addComp.province + ' ' + addComp.city + ' ' + addComp.district;
                callback(address)
                return address
              });
            }
            else {
              Toast('浏览器定位失败')
              self.getSDK()
            }
          })
        } catch (e) {
          console.log(e)
        }
      }
      /**
       * 辅助百度SDK定位
       */
      getSDK() {
        var map = new BMap.Map("allmap")
        var point = new BMap.Point(116.331398,39.897445)
        map.centerAndZoom(point,12)
        var geolocation = new BMap.Geolocation()
        // 开启SDK辅助定位
        geolocation.enableSDKLocation()
        geolocation.getCurrentPosition(function(r){
          if(this.getStatus() == BMAP_STATUS_SUCCESS){
            var mk = new BMap.Marker(r.point)
            map.addOverlay(mk)
            map.panTo(r.point)
            Toast('您的位置:'+r.point.lng+','+r.point.lat)
          }
          else {
            Toast('百度地图定位失败')
          }
        });
      }
      /**
       * IP 定位
       */
      getIP() {
        try {
          let promise = new Promise((resolve, reject) => {
            var map = new BMap.Map("allmap")
            var point = new BMap.Point(116.331398,39.897445)
            map.centerAndZoom(point,12)
    
            function myFun(result){
              var cityName = result.name
              map.setCenter(cityName)
              resolve(result)
              // Toast("当前定位城市:"+ cityName)
            }
            var myCity = new BMap.LocalCity()
            myCity.get(myFun)
          })
          return promise
        } catch(e) {
          Toast('IP定位失败')
          return null
        }
      }
    
    }
    
    export default Geolocation
    调用:
         let geolocation = new Geolocation();
         geolocation.getGeolocation((res) => {
                console.log(res)   //拿到地址
         });    
     
     
  • 相关阅读:
    How to import data from Oracle into PostgreSQL(转)
    C++——算法 回溯 八皇后问题
    Python——彩图变线稿
    算法——二叉树的遍历 前序 中序 后序 广度优先 深度优先 (转)
    C++——计数排序 (转)
    C++——位运算相关 (转)
    C++——std::vector相关 (转)
    C++——双指针 (转)
    C++——求三数之和,实操从低效做法逐步提升到高效做法,受益良多
    C++——基础容器
  • 原文地址:https://www.cnblogs.com/init00/p/12762477.html
Copyright © 2011-2022 走看看