随着网络越来越发达,地图在这其中扮演着重要的角色,今天我们就来谈谈如何在小程序中使用地图,首先我们看看效果图:
说明一下,默认显示第一张图,然后点搜索会跳转到第二张图片。下面进入正题:
JS文件如下
const amapFile = require('../../utils/amap-wx.js'); //地图文件,在我的QQ群里有下载(731568857)
Page({
data: {
key: '高德地图KEY',
show: false,
currentLo: null,
currentLa: null,
newCurrentLo: null,
newCurrentLa: null,
distance: 0,
duration: 0,
markers: null,
scale: 16,
polyline: null,
statusType: 'car',
includePoints: []
},
//默认加载数据
onLoad: function() {
var _this = this;
wx.getLocation({
success(res) {
_this.setData({
currentLo: res.longitude,
currentLa: res.latitude,
includePoints: [{
longitude: res.longitude,
latitude: res.latitude
}],
markers: [{
id: 0,
longitude: res.longitude,
latitude: res.latitude,
title: res.address,
iconPath: '../../images/dw2.png',
width: 20,
height: 20
}]
});
}
})
},
//获取地址
getAddress: function(e) {
var _this = this;
wx.chooseLocation({
success(res) {
var markers = _this.data.markers;
markers.push({
id: 0,
longitude: res.longitude,
latitude: res.latitude,
title: res.address,
iconPath: '../../src/images/navi_e.png',
width: 32,
height: 32
});
var points = _this.data.includePoints;
points.push({
longitude: res.longitude,
latitude: res.latitude
});
_this.setData({
newCurrentLo: res.longitude,
newCurrentLa: res.latitude,
includePoints: points,
markers: markers,
show: true
});
_this.getPolyline(_this.data.statusType);
}
});
},
//绘制地图
drawPolyline(self, color) {
return {
origin: this.data.currentLo + ',' + this.data.currentLa,
destination: this.data.newCurrentLo + ',' + this.data.newCurrentLa,
success(data) {
var points = [];
if (data.paths && data.paths[0] && data.paths[0].steps) {
var steps = data.paths[0].steps;
for (var i = 0; i < steps.length; i++) {
var poLen = steps[i].polyline.split(';');
for (var j = 0; j < poLen.length; j++) {
points.push({
longitude: parseFloat(poLen[j].split(',')[0]),
latitude: parseFloat(poLen[j].split(',')[1])
})
}
}
}
self.setData({
distance: data.paths[0].distance,
duration: parseInt(data.paths[0].duration / 60),
polyline: [{
points: points,
color: color,
width: 6,
arrowLine: true
}]
});
}
}
},
//获取定位
getPolyline(_type) {
var amap = new amapFile.AMapWX({ key: this.data.key });
var self = this;
switch (_type) {
case 'car':
amap.getDrivingRoute(this.drawPolyline(this, "#0091ff"));
break;
case 'walk':
amap.getWalkingRoute(this.drawPolyline(this, "#1afa29"));
break;
case 'ride':
amap.getRidingRoute(this.drawPolyline(this, "#1296db"));
break;
default:
return false;
}
},
goTo(e) {
var _type = e.currentTarget.dataset.type;
this.setData({ statusType: _type });
this.getPolyline(_type);
}
})
上面就是核心的JS文件,下面是wxml文件:
<view class="tui-map">
<map id="map" longitude="{{currentLo}}" latitude="{{currentLa}}" scale="{{scale}}" markers="{{markers}}" polyline="{{polyline}}" include-points="{{includePoints}}" show-location style="width: 100%; height: 100%;"></map>
</view>
<view class="tui-map-search" bindtap="getAddress">
<icon size='20' type='search' class='tui-map-search-icon'></icon>
<input class="tui-map-input" placeholder="搜索" focus="{{focusStatus}}"></input>
</view>
<view class="tui-search-bottom {{show ? '' : 'tui-hide'}}">
<view class="page-group">
<view class="page-nav-list {{statusType == 'car' ? 'active' : ''}}" data-type="car" bindtap="goTo">驾车</view>
<view class="page-nav-list {{statusType == 'walk' ? 'active' : ''}}" data-type="walk" bindtap="goTo">步行</view>
<view class="page-nav-list {{statusType == 'ride' ? 'active' : ''}}" data-type="ride" bindtap="goTo">骑行</view>
</view>
<view class="tui-warn">
{{distance}}米
</view>
<view class="tui-warn">
{{duration}}分钟
</view>
</view>
好了,大功告成,就是这么简单。才开始研究的时候感觉蛮难的,研究出来就不觉得了。有需要代码的朋友可以加我的QQ号或者微信公众号,我们一起进步。
【本文由“xiaoyuehan007账号”发布,2018年11月15日】