微信小程序map 地圖引入配置+騰訊地圖地址座標解析

最終效果

1、要實現的功能

  • 展現地圖。

    • 參考map組件
  • 在地圖上展現多個店鋪。

    • marker標記點用於在地圖上顯示標記的位置。
  • 點擊店鋪放大圖標,展現選擇的店鋪信息。

    • 更改選擇店鋪的標記尺寸大小。
  • 在地圖中心點放一個可視化標記。

    • cover-view標籤放置一個標記圖片,定位到在地圖大小的中心,自行調試。
  • 移動後獲取地圖中心點位置,選擇最近一個店鋪。

    • mapbindregionchange事件,視野發生變化時觸發,從新請求,把最近一家店鋪修改標記尺寸大小(有注意事項)。
  • 移動後根據屏幕中心點座標逆地址解析成中文地址(詳細看第四)。

  • 增長選擇其餘城市頁面(封裝成組件了,點擊這裏查看)。

  • 點擊其餘城市,根據城市名稱地址解析成座標,更新視圖(詳細看第四)。

2、注意事項

  • 每次更新視圖都會觸發bindregionchange事件,若是在裏面獲取到地圖視圖中心點座標從新賦值給地圖座標的話,會形成這個事件不斷的觸發。
  • 每次拖動視圖中心點位置都須要從新請求座標附近的店鋪。注意封裝方法
  • 展現在地圖上的標籤必須用cover-view或者cover-image

3、涉及到的技術及連接

4、騰訊地圖的微信小程序javaScript SDK 使用

使用方式直接去看官方文檔,引入也很是詳細了。直接把下載js文件,而後引入。不改爲了es6的expost default/import來導出引入html

export default QQMapWX
複製代碼

而後在使用頁面import,在onLoad裏實例化api核心類。vue

//index.vue
import QQMapWX from "@/utils/qqmap-wx-jssdk"; //騰訊地圖,reverseGeocoder逆地址轉碼
export default {
    data(){
        return {
          qqmapsdk: null, //實例化地圖sdk
        }
    },
    onLoad(){
        // 實例化API核心類
        this.qqmapsdk = new QQMapWX({
          key: "3P2BZ-6G***-C***3-***5G-3VDYH-N5BGH" // 必填
        });
    }
}
複製代碼

根據座標逆解析詳細地址java

//根據座標逆解析詳細地址
getCityinfo() {
      return new Promise((resolved, rejected) => {
        const that = this;
        this.qqmapsdk.reverseGeocoder({
          location: {
            latitude: this.latitude,
            longitude: this.longitude
          },
          success(res) {
            console.log("地址轉碼成功", res);
            const _res = res.result;
            that.cityName = _res.address_component.city;
            that.update({
              cityName: _res.address_component.city,
              nowPlace:
                _res.formatted_addresses.recommend + " - " + _res.address
            });
            that.getShopData();
          },
          fail: function(res) {
            console.log(res);
          }
        });
      });
    },
複製代碼

根據城市/地址解析成座標git

//根據城市/地址解析成座標
cityNameGetPosition() {
      return new Promise((resolved, rejected) => {
        const that = this;
        this.qqmapsdk.geocoder({
          address: this.cityName,
          success(res) {
            console.log("根據地址轉換座標", res);
            const _res = res.result.location;
            that.latitude = _res.lat;
            that.longitude = _res.lng;
            that.update({
              latitude: _res.lat,
              longitude: _res.lng
            });
            that.getCityinfo();
          },
          fail(err) {
            console.log("根據地址轉換座標err", err);
          }
        });
      });
    },
複製代碼

5、實現的部分代碼

使用的請求和功能邏輯應該是這樣的,作了個思惟導圖。 (注意:代碼跟思惟導圖可能有出入,圖是完成代碼後的總結,是比較完善的)

map

這個項目開發使用的是mpvue開發的小程序,mpvue裏bindregionchange事件變成了es6

//不是mpvue開發請無視
    @regionchange="getCenterMap1"
    @end="getCenterMap"
複製代碼

map組件github

<div>
        <!-- 地圖組件 -->
        <map
          id="map"
          :longitude="longitude"
          :latitude="latitude"
          scale="13"
          :markers="markers"
          @markertap="markertap"
          @regionchange="getCenterMap1"
          @end="getCenterMap"
          show-location
          style="width:750rpx; height:99vh;"
        >
        </map>
        <!-- 中心點 -->
        <cover-image  class="centerImg"
        src="/static/images/person.png"
        ></cover-image>
        <!-- 回到個人定位 -->
        <cover-image
          @click="getMyPosition"
          class="backMyPosition"
          src="/static/images/location.png"
        ></cover-image>
    </div>
複製代碼

獲取自身定位wx.getLocation小程序

// 獲取定位
    getMyPosition() {
      return new Promise((resolved, rejected) => {
        wx.getLocation({
          type: "wgs84",
          success: data => {
            // console.log(data,"微信地圖")
            this.latitude = data.latitude;
            this.longitude = data.longitude;
            this.$store.commit("update", {
              latitude: data.latitude,
              longitude: data.longitude
            });
            // 根據座標獲取城市信息
            this.getCityinfo().then(() => {
              resolved();
            });
          },
          fail() {
            //失敗回調
            //若是用戶拒絕受權,默認爲北京
            this.cityName = "北京市";
            this.update({ cityName: "北京市" });
          }
        });
      });
    },
複製代碼

地圖視野更新時觸發微信小程序

// 地圖視野更新時觸發
    getCenterMap() {
      if (this.active === "上門") {
        const that = this;
        console.log("自身位置座標", this.longitude, this.latitude);
        const map = wx.createMapContext("map");
        map.getCenterLocation({
          success(res) {
            // 判斷座標一致,不用重複請求數據
            if (
              that.longitude === res.longitude &&
              that.latitude === res.latitude
            ) {
              return false;
            }
            //  const ress =  transformFromGCJToWGS(res.latitude,res.longitude)
            that.latitude = res.latitude;
            that.longitude = res.longitude;

            that.$store.commit("update", {
              latitude: res.latitude,
              longitude: res.longitude
            });
            console.log("中心位置座標", that.longitude, that.latitude);
            // console.log('轉換後的中心位置座標',ress)
            that.getCityinfo();
          }
        });
      }
    }
複製代碼
相關文章
相關標籤/搜索