微信小程序 map地图(轨迹)

allMarkers效果图

 

废话少说直接上马(最后是我遇到的问题)

cover-view是气泡弹窗,可以自定义弹窗,要配合js:customCallout,如果是非自定义的话:callout(可以修改颜色、边框宽度、圆角、边距等)有些属性我没加,还是看官方文档(虽然写的。。)

<!--index.wxml-->
<view class="page-section page-section-gap">
  <map id="myMap" style="width: 100%; height: 450px;" latitude="{{latitude}}" longitude="{{longitude}}" bindmarkertap="markertap" bindcallouttap="callouttap" bindlabeltap="labeltap" markers="{{markers}}" scale="16" polyline="{{polyline}}">
    <cover-view slot="callout">
      <block wx:for="{{customCalloutMarkerIds}}" wx:key="*this">
        <cover-view class="customCallout" marker-id="{{item}}">
          <!-- <cover-image class="icon" src="../img/point.png"></cover-image> -->
          <cover-view class="content">
            <view style="background-color: #fff;">
              <view>设备名称</view>
              <view>设备名称</view>
              <view>设备名称</view>
            </view>
          </cover-view>
        </cover-view>
      </block>
    </cover-view>
  </map>
  <button bindtap="addMarker" class="page-body-button" type="primary">添加marker</button>
</view>

字段说明:

allMarkers:模拟位置点

markers:地图显示位置点

polyline:地图点连线位置点

mapCtx:初始化地图

 customCallout:气泡自定义使用(建议使用方法)

callout:气泡简易使用

const normalCallout = {
  id: 1,
  latitude: 23.098994,
  longitude: 113.322520,
  iconPath: '../img/point.png',
  callout: {
    content: '文本内容',
    color: '#ff0000',
    fontSize: 14,
    borderWidth: 2,
    borderRadius: 10,
    borderColor: '#000000',
    bgColor: '#fff',
    padding: 5,
    display: 'ALWAYS',
    textAlign: 'center'
  },
}

const customCallout1 = {
  id: 2,
  latitude: 23.097994,
  longitude: 113.323520,
  iconPath: '../img/point.png',
  callout: {
    content: '文本内容',
    color: '#ff0000',
    fontSize: 14,
    borderWidth: 2,
    borderRadius: 10,
    borderColor: '#000000',
    bgColor: '#fff',
    padding: 5,
    display: 'ALWAYS',
    textAlign: 'center'
  },
}

const customCallout2 = {
  id: 3,
  latitude: 23.096994,
  longitude: 113.324520,
  iconPath: '../img/point.png',
  customCallout: {
    anchorY: 10,
    anchorX: 0,
    display: 'ALWAYS',
  },
}

const customCallout3 = {
  id: 4,
  latitude: 23.095994,
  longitude: 113.325520,
  iconPath: '../img/point.png',
  customCallout: {
    anchorY: 0,
    anchorX: 20,
    display: 'ALWAYS',
  },
}

const allMarkers = [normalCallout, customCallout1, customCallout2, customCallout3]

Component({
  data: {
    mapCtx: '',
    latitude: 23.098994,
    longitude: 113.322520,
    customCalloutMarkerIds: '',
    markers: [],
    polyline: []
  },
  onReady: function (e) {
    // 使用 wx.createMapContext 获取 map 上下文
    this.mapCtx = wx.createMapContext('myMap')

  },
  methods: {
    addMarker() {
      var list = []
      var that = this
      for (const i in allMarkers) {
        var point = {
          latitude: allMarkers[i].latitude,
          longitude: allMarkers[i].longitude
        }
        list.push(point)
      }
      console.log(this.data.polyline.points)
      this.setData({
        polyline:[{
          points: list,
          color: '#58c16c',
          width: 6,
          borderColor: '#2f693c',
          borderWidth: 1,
          arrowLine: true
        }],
        markers: allMarkers,
        customCalloutMarkerIds: [2, 3, 4],
      })
    },

  },
})

xml和js直接复制就可以运行了没有其他配置

所遇到问题:

1.初始化时我在data直接给markers和polyline赋值了,导致不能显示气泡,解决是加了一个按钮等map初始化结束在添加点就正常了

2.marker-id必须是number

3.我没写,但是我感觉自定义气泡是可以加点击事件的

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
微信小程序中的地图导航功能可以通过调用小程序的API来实现。具体的步骤如下: 1. 在小程序的页面中引入地图组件,使用`map`标签在页面上展示地图。 2. 在小程序的`app.json`文件中配置地图组件的权限,需要在`permission`字段中添加`"scope.userLocation"`权限,以便获取用户的地理位置信息。 3. 在页面的js文件中编写代码,以获取用户地理位置为例,通过`wx.getLocation`方法获取用户当前位置的经纬度信息。 4. 调用微信地图API,使用获取到的经纬度信息显示地图,并根据用户选择的目的地,在地图上添加导航标记。 5. 用户在小程序中选择目的地后,通过`wx.openLocation`方法打开手机系统地图,实现导航功能。 例如,可以在小程序的`index.wxml`文件中添加地图组件的引用: ```html <!-- index.wxml --> <view> <map id="myMap" class="map"></map> </view> ``` 在`index.js`文件中编写获取用户位置的代码: ```javascript // index.js Page({ data: { latitude: 0, longitude: 0, }, onLoad() { wx.getLocation({ type: 'gcj02', success: (res) => { this.setData({ latitude: res.latitude, longitude: res.longitude, }) }, fail: (error) => { console.error('获取位置失败', error) }, }) }, }) ``` 最后,在用户点击选择目的地后,可以使用`wx.openLocation`方法打开手机系统地图进行导航: ```javascript // index.js Page({ // ... navigateToDestination() { wx.openLocation({ latitude: destinationLatitude, longitude: destinationLongitude, name: '目的地名称', address: '目的地地址' }); }, }) ``` 通过以上步骤,便可以在微信小程序中实现地图导航功能。当用户点击选择目的地后,系统便会打开手机系统地图,并提供导航路线信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值