uniapp中进行地图定位

目录

一、创建map

二、data中声明变量

三、获取当前位置信息,进行定位

四、在methods中写移动图标获取地名地址的方法

五、最终展示效果


一、创建map

	<!-- 地图展示 -->
	<view class="mymap">
		<!-- <view class="mymap__map"> -->
			<map class="mymap__map" id="map" :latitude="mapxx.latitude" :longitude="mapxx.longitude"
			:scale="mapxx.scale" :markers="mapxx.markers"
			@regionchange="mapPoint"
			></map>
		<!-- </view> -->
	</view>

二、data中声明变量

let infowidth = 32,infoheight = 42;
let infoiconPath = '/static/images/map/loaction-red.png';
data(){
    return{
           key:'自己的key',
            mapxx:{
              latitude:35.931616,
              longitude:120.008822,
              scale:16,
              markers:{
                id:0,
                latitude:35.931616,
                longitude:120.008822,
                iconPath:'/static/images/map/loaction-red.png'
              }
          },     
      }
 }

三、获取当前位置信息,进行定位

onLoad() {
	//获取当前的地理位置
	let vthis = this;
	uni.getLocation({
	    type: 'gcj02',
	    success: function (res) {
			vthis.mapxx.latitude = res.latitude;
			vthis.mapxx.longitude = res.longitude;
			vthis.mapxx.markers = [{
				id:1,
				latitude:res.latitude,
				longitude:res.longitude,
				iconPath:'/static/images/map/loaction-red.png'
			}];
	        console.log('当前位置的经度:' + res.longitude);
	        console.log('当前位置的纬度:' + res.latitude);
	    }
	});
},

四、在methods中写移动图标获取地名地址的方法

	//地图相关操作 -start
	mapPoint(e){
	  // 地图发生变化的时候,获取中间点,也就是用户选择的位置toFixed
	  if (e.type == 'end' && (e.causedBy == 'scale' || e.causedBy == 'drag')) {
	    let that = this;
	    this.mapCtx = wx.createMapContext("map");
	    this.mapCtx.getCenterLocation({
	      type: 'gcj02',
	      success: function(res) {
	        that.mapxx.markers = [{
				  latitude: res.latitude,
				  longitude: res.longitude,
				  iconPath: infoiconPath,
				  width: infowidth,
				  height: infoheight,
				}]
	        that.loadCity(res.longitude,res.latitude);
	      }
	    })
	  }
	},
	loadCity(longitude, latitude) {
	  var _self = this;
	  wx.request({
	    url: 'https://restapi.amap.com/v3/geocode/regeo',
	    data: {
	      key: _self.key,
	      location: longitude + "," + latitude,
	      extensions: "all",
	      s: "rsx",
	      sdkversion: "sdkversion",
	      logversion: "logversion"
	 
	    },
	    success: function (res) {
		  _self.dybd.userInfo.dwhzz = res.data.regeocode.formatted_address;
	    },
	    fail: function (res) {
	      console.log('获取地理位置失败')
	    }
	  })
	},

五、最终展示效果

Uniapp可以使用多种地图定位SDK,其比较常用的有百度地图、高德地图和腾讯地图等。下面以百度地图为例,介绍一下在Uniapp如何使用地图定位SDK。 1. 在百度地图开放平台上,创建应用并获取AK值。AK是用于调用百度地图API的密钥,需要在Uniapp使用。 2. 在Uniapp项目,使用uni-app-plus插件来集成百度地图SDK。具体步骤如下: - 在HBuilderX,右键点击项目,选择“插件市场”。 - 搜索“uni-baidu-map”,点击“安装”按钮,等待安装完成。 - 在manifest.json文件,添加uni-baidu-map插件的引用。具体来说,需要在“uni_modules”字段添加“uni-baidu-map”模块的引用,例如: ``` { "uni_modules": { "uni-baidu-map": "node_modules/@dcloudio/uni-baidu-map" } } ``` 3. 在需要使用地图的页面,引入uni-baidu-map组件。例如: ``` <template> <view> <uni-baidu-map :ak="ak" :scale="scale" :markers="markers" @markertap="onMarkerTap" ></uni-baidu-map> </view> </template> <script> import uniBaiduMap from '@/uni_modules/uni-baidu-map/components/uni-baidu-map.vue' export default { components: { uniBaiduMap }, data() { return { ak: 'your ak', scale: 14, markers: [{ id: 0, latitude: 39.914884, longitude: 116.403883, title: 'Marker 1' }] } }, methods: { onMarkerTap(marker) { console.log(marker) } } } </script> ``` 这里的ak是在百度地图开放平台上获取的密钥,scale是地图缩放比例,markers是地图标记点的信息。 4. 在需要获取地理位置的页面,使用uni.getLocation API来获取当前位置。例如: ``` uni.getLocation({ type: 'gcj02', success: (res) => { console.log(res) }, fail: (err) => { console.log(err) } }) ``` 这里的type是定位方式,gcj02代表国测局坐标系,具体可以根据实际情况选择。 需要注意的是,不同的地图定位SDK使用方式可能有所不同,需要根据具体的SDK文档来进行操作。同时,在使用地图定位SDK时,需要注意保护用户隐私和数据安全。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神仙姐姐QAQ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值