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();
}
});
}
}
复制代码