微信小程序多点路线规划
参考文档:
https://developers.weixin.qq.com/miniprogram/dev/component/map.html
完整的页面:
XML/HTML Code复制内容到剪贴板
- <template>
- <view class="detail-map">
- <view class="map-wrap">
- <map id="navi_map" style='width: 750rpx; height:100vh;' :longitude="longitude" :latitude="latitude" :include-points="points" :markers="markers" :polyline="polyline" :segmentTexts="segmentTexts" @markertap="tapMap" @callouttap="tapMap"></map>
- </view>
- </view>
- </template>
- <script>
- export default {
- data () {
- return {
- mainHeight: 300,
- status: 'car',
- road_id: 0,
- longitude: '', // 经度
- latitude: '' ,// 纬度
- points: [], // 缩放视野以包含所有给定的坐标点
- markers: [], // 标记点
- polyline: [],
- segmentTexts: []
- }
- },
- onLoad(options) {
- this.road_id = options.road_id
- var system = this.tool.system();
- this.mainHeight = system.windowHeight
- this.init()
- },
- methods: {
- async init() {
- // #ifdef MP-WEIXIN
- if(!this.longitude || !this.latitude){
- const location = await this.$store.dispatch('getLocation')
- this.longitude = parseFloat(location.longitude)
- this.latitude = parseFloat(location.latitude)
- }
- // #endif
- this.gRequest.get('road/detail',{
- "data": {
- "id": this.road_id
- }
- },(res) => {
- const data = res.data
- const itineraries = data.itineraries
- // 拎出所有行程点的经纬度值
- let segmentTexts = []
- let points = []
- let markers = []
- itineraries.forEach((i,idx) => {
- i.points.forEach((e,ii) => {
- points.push({
- "latitude": parseFloat(e.latitude),
- "longitude": parseFloat(e.longitude),
- })
- segmentTexts.push({
- "name": e.name
- })
- markers.push({
- "id":e.id,
- // "iconPath": "", // 显示的图标
- "callout": {
- "display": "ALWAYS",
- "content":e.name,
- "textAlign":"center",
- "color":"#fff",
- "borderWidth":0,
- "bgColor":'#000',//背景颜色,可使用rgba
- // "anchorY":-3,
- "fontSize":13,
- "borderRadius": 5,
- "padding": 10
- },
- "latitude":parseFloat(e.latitude),
- "longitude":parseFloat(e.longitude),
- "title":e.name
- })
- });
- });
- this.points = points
- this.polyline = [{
- "points": points,
- "color": "#426666",
- // "color": "#0091ff",
- "width": 2,
- "borderWidth": 0,
- "arrowLine": true
- }]
- this.segmentTexts = segmentTexts
- this.markers = markers
- console.log(this.polyline)
- console.log(markers)
- })
- },
- tapMap (e) {
- const marker = this.markers.filter(item => item.id == e.markerId)
- // const marker = this.markers[e.]
- if (marker.length) {
- // uni.showModal({
- // title: '询问',
- // content: '是否打开地图',
- // showCancel: true,
- // success: res => {
- const location = marker[0]
- // console.log(location)
- uni.openLocation({
- latitude: location.latitude, // 要去的地址经度,浮点数
- longitude: location.longitude, // 要去的地址纬度,浮点数
- name: location.title, // 位置名
- address: location.title, // 要去的地址详情说明
- scale: 1, // 地图缩放级别,整形值,范围从1~28。默认为最大
- });
- // },
- // fail: () => {},
- // complete: () => {}
- // });
- }
- },
- }
- }
- </script>
- <style lang="scss" scoped>
- .detail-map {
- .control-wrap {
- width: 750rpx;
- overflow: hidden;
- .item {
- background: $primary-color;
- color: #fff;
- height: 60rpx;
- line-height: 60rpx;
- // padding: 20rpx 0;
- }
- }
- .map-wrap {
- // height: calc(100vh - (60rpx + env(safe-area-inset-bottom)));
- height: 100vh;
- }
- }
- </style>
上一篇 轮播图自动渐隐渐现,最后进入首页
下一篇 微信小程序保存到相册