微信小程序 獲取位置、移動選點、逆地址解析


WGS-84 地心坐標系,即GPS原始坐標體系。在中國,任何一個地圖產品都不允許使用GPS坐標,據說是為了保密。GoogleEarth及GPS芯片使用。
1、GCJ-02火星坐標系,國測局02年發布的坐標體系,它是一種對經緯度數據的加密算法,即加入隨機的偏差。高德、騰訊、Google中國地圖使用。國內最廣泛使用的坐標體系;
2、其他坐標體系:一般都是由GCJ-02進過偏移算法得到的。這種體系就根據每個公司的不同,坐標體系都不一樣了。比如,百度的BD-09坐標、搜狗坐標等
 本回答由網友推薦
wxml:
<view class="page-body">
  <view class="page-section page-section-gap">
    <map id="qqMap" style="width: 100%; height: 300px;" latitude="{{latitude}}" longitude="{{longitude}}" markers="{{markers}}" show-location></map>
  </view>
  <view class="btn-area">
    <button bindtap="moveToLocation" class="page-body-button" type="primary">移動位置</button>
    
    選擇的位置:
    <view>位置:{{mobileLocation.address}}</view>
    <view>經度:{{mobileLocation.longitude}}</view>
    <view>緯度:{{mobileLocation.latitude}}</view>

  </view>
</view>

js:

var QQMapWX = require('../../libs/qqmap-wx-jssdk.js');
var qqmapsdk;
Page({
   data: {
      borderRadius : 5,
      latitude : 0,
      longitude: 0,
      markers: [],
      callout : {
         content: '預計還有10分鍾到達',
         bgColor: "#736F6E",
         color: "#ffffff",
         padding: 10,
         display: "ALWAYS",
         borderRadius: 5
      },
      mobileLocation : {//移動選擇位置數據
         longitude : 0,
         latitude: 0,
         address: '',
      }
   },
    onLoad: function () {
       // 實例化API核心類
       qqmapsdk = new QQMapWX({
          key: 'qq-map key'
       });
      var that = this;
      //獲取位置
      wx.getLocation({
         type: 'gcj02',//默認為 wgs84 返回 gps 坐標,gcj02 返回可用於wx.openLocation的坐標
         success: function(res) {
            console.log(res);
            var marker = [{
               id: 0,
               latitude: res.latitude,
               longitude: res.longitude,
               callout: {//窗體
                  content: that.data.callout.content,
                  bgColor: that.data.callout.bgColor,
                  color: that.data.callout.color,
                  padding: that.data.callout.padding,
                  display: that.data.callout.display,
                  borderRadius: that.data.callout.borderRadius
               },
            }];
            var mobileLocation = {
               latitude: res.latitude,
               longitude: res.longitude, 
            };
            that.setData({
               latitude: res.latitude,
               longitude: res.longitude,  
               markers: marker,
            });
            //根據坐標獲取當前位置名稱,顯示在頂部:騰訊地圖逆地址解析
            qqmapsdk.reverseGeocoder({
               location: {
                  latitude: res.latitude,
                  longitude: res.longitude
               },
               success: function (addressRes) {
                  var address = addressRes.result.formatted_addresses.recommend;
                  mobileLocation.address = address;
                  console.log(address)
                  //當前位置信息
                  that.setData({
                     mobileLocation: mobileLocation,
                  });
               }
            });
         }
      });

      this.mapCtx = wx.createMapContext('qqMap');
   },
   
    //移動選點
   moveToLocation: function () {
      var that = this;
      wx.chooseLocation({
         success: function (res) {
            let mobileLocation = {
               longitude: res.longitude,
               latitude: res.latitude,
               address: res.address,
            };
            that.setData({
               mobileLocation: mobileLocation,
            });
         },
         fail: function (err) {
            console.log(err)
         }
      });
   }
});

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM