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

五、最终展示效果

相关推荐
叫我阿柒啊6 小时前
从Java全栈到前端框架:一次真实面试的深度复盘
java·spring boot·typescript·vue·database·testing·microservices
毕设源码-邱学长7 小时前
【开题答辩全过程】以 基于微信小程序的宠物领养系统为例,包含答辩的问题和答案
微信小程序·小程序·宠物
canglingyue8 小时前
微信小程序日历事件添加实现
微信小程序·小程序
借你耳朵说爱你8 小时前
浅拷贝和深拷贝两种不同的对象复制
vue·web
F2E_Zhangmo11 小时前
基于cornerstone3D的dicom影像浏览器 第四章 鼠标实现翻页、放大、移动、窗宽窗位调节
vue·cornerstone·cornerstone3d·cornerstonejs
毕设源码-邱学长12 小时前
【开题答辩全过程】以 基于微信小程序校园综合服务平台的设计与实现为例,包含答辩的问题和答案
微信小程序·小程序
Thetimezipsby12 小时前
基于Taro4打造的一款最新版微信小程序、H5的多端开发简单模板
前端·javascript·微信小程序·typescript·html5·taro
從南走到北12 小时前
JAVA同城打车小程序APP打车顺风车滴滴车跑腿源码微信小程序打车源码
java·开发语言·微信·微信小程序·小程序
猫七先生14 小时前
微信小程序一键登录可行性方案
前端·微信小程序