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('获取地理位置失败')
	    }
	  })
	},

五、最终展示效果

相关推荐
Greg_Zhong6 小时前
微信小程序中便捷实现自定义底部tab栏
微信小程序·自定义底部tab
LXXgalaxy6 小时前
微信小程序“记住密码”功能的实现与落地 vue3+ts的细致解析
微信小程序·小程序·notepad++
Greg_Zhong7 小时前
微信小程序中使用【免费商用】字体的下载和初步认识和使用
微信小程序·阿里巴巴、站酷·腾讯云对象存储(cos)
克里斯蒂亚诺更新7 小时前
微信小程序 腾讯地图 点聚合 简单示例
微信小程序·小程序·notepad++
Geek_Vision7 小时前
鸿蒙原生APP接入小程序运行能力:数字园区场景实战复盘
微信小程序·harmonyos
2501_916007478 小时前
iOS逆向工程:详细解析ptrace反调试机制的破解方法与实战步骤
android·macos·ios·小程序·uni-app·cocoa·iphone
00后程序员张10 小时前
前端可视化大屏制作全指南:需求分析、技术选型与性能优化
前端·ios·性能优化·小程序·uni-app·iphone·需求分析
编程迪11 小时前
基于Java和uniapp开发的名片交换分享系统企业名片管理软件个人电子名片小程序源码
java·uni-app·电子名片·名片小程序·名片软件源码
DazedMen12 小时前
前端自定义接口返回,想咋玩就咋玩
前端·vue·接口拦截
qwfy1 天前
我从瑞幸咖啡小程序里,拆出了一套 22 个组件的开源 UI 库
微信小程序·开源