百变鹏仔-专注前端行业精选
当前位置:网站首页 > 最近更新 > 前端开发 > JavaScript > 正文

uniapp调用地图,进行位置查询,标记定位

作者:鹏仔先生日期:2022-07-08 14:11:17浏览:2453分类:JavaScript

这周开会又新增一个小功能,就是需要通过身份证号/手机号在地图查询(后台返回经纬度),标记位置,想着挺麻烦,做的时候发现没啥技术,分享给大家。


1.gif


<map class="map" :longitude="longitude" :latitude="latitude" scale="16" :markers="markers" show-location="true"></map>
data() {
  return {
    longitude: "", // 当前位置经度
    latitude: "", // 当前位置纬度
    markers: [], // 获取位置的标记信息
  }
}
methods: {
  searchBtn(){
    let then = this;
    uni.getLocation({
      type: 'wgs84',
      geocode: true, //设置该参数为true可直接获取经纬度及城市信息
      success: function (res) {
        // res会返回当前经纬度信息之类,如果默认想展示当前位置,下面经纬度可直接使用res的值,如果默认展示指定位置,那就自定义经纬度即可;
        then.longitude = '116.407526';
        then.latitude = '39.904030';
        then.markers = [
          {
            title: '',
            latitude: then.longitude,
            longitude: then.latitude,
            height: 100,
            width: 100,
            iconPath: 'http://cdn.duanqinghua.com/duanqinghua/img/55.jpg'
          }
        ]
      }
    });
  }
}

然后每次查询,通过调后台接口,拿到对应经纬度,赋值给longitude和latitude,那么地图就会展示对应经纬度区域,如果同时想展示定位的图标,那就按上方方式给markers赋值,可标记多个位置。

(获取到数据后,最好不要直接给markers赋值,先let随便定义一个赋值,再将let定义的赋给markers,否则会出现不显示标记点现象)


手机扫码访问

暂无评论,来添加一个吧。

取消回复欢迎 发表评论:

关灯