map
組件marker
標記點用於在地圖上顯示標記的位置。cover-view
標籤放置一個標記圖片,定位到在地圖大小的中心,自行調試。map
的bindregionchange
事件,視野發生變化時觸發,從新請求,把最近一家店鋪修改標記尺寸大小(有注意事項)。bindregionchange
事件,若是在裏面獲取到地圖視圖中心點座標從新賦值給地圖座標的話,會形成這個事件不斷的觸發。cover-view
或者cover-image
map
組件使用方式直接去看官方文檔,引入也很是詳細了。直接把下載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);
}
});
});
},
複製代碼
這個項目開發使用的是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();
}
});
}
}
複製代碼