uniapp中进行地图定位

news/2024/7/10 0:50:06 标签: uni-app, 微信小程序, vue

目录

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

五、最终展示效果


http://www.niftyadmin.cn/n/5233894.html

相关文章

Flink入门(一)

整体框架 Flink概述Flink上手部署Flink架构DataStream API(算子)Flink中的时间和窗口&#xff1a;窗口就是范围处理函数&#xff1a;底层函数状态管理&#xff1a;容错机制&#xff1a;报错重启后能够从出错的位置继续执行FlinkSQL&#xff1a;功能逐步完善 基于数据流的有状…

点云从入门到精通技术详解100篇-基于三维点云的工件曲面轮廓检测与机器人打磨轨迹规划(下)

目录 4.3 机器人打磨轨迹规划 4.3.1 机器人运动学建模与分析 4.3.2 机器人轨迹规划算法

c++ (取变量内存地址)的几种用法

1. 声明引用&#xff1a;别名 改变ref a 都修改源数据 源地址数值 int a 10; int& ref a; // 声明一个引用变量ref&#xff0c;它引用了变量a 为什么要用别名: 1 给变量起一个更容易理解的名子 2 给函数/方法 传参 取这个参数的地址的值 修改的是源数据值 更高效 不多用…

运维 | 关于IP网络相关的概念和原理

关注&#xff1a;CodingTechWork IP地址 IP介绍 概述 IP是TCP/IP协议族的核心&#xff0c;IP地址是电脑在网络中的唯一标识&#xff0c;全球唯一&#xff08;G公网IP&#xff09;。IP地址&#xff08;Internet Protocol Address&#xff09;是互联网协议地址&#xff0c;也…

一天一个设计模式---责任链模式

责任链模式 简介 将不同职责的步骤进行串联&#xff0c;前一个执行完成之后才可以执行下一个&#xff0c;即前一个的责任完成之后会将这个责任给到下一个。 组成结构 一共有两个主要的类 抽象的处理类&#xff08;Handle&#xff09;&#xff0c;封装了每一个职责处理请求…

VR 实现 Splash Screen 效果

文章目录 背景官方实现逆向分析 背景 手机 App 在实现 Splash Screen 的时候&#xff0c;目前都有成熟的方案可以参考&#xff0c;但是在做 VR 开发时&#xff0c;要如何实现一个 App 自己的 Splash Screen &#xff0c;下面是我们基于 PICO & OCULUS 进行业务开发时经过探…

Mac卸载、安装Python

卸载 说明 对于删除 Python&#xff0c;我们首先要知道其具体都安装了什么&#xff0c;实际上&#xff0c;在安装 Python 时&#xff0c;其自动生成&#xff1a; Python framework&#xff0c;即 Python 框架&#xff1b;Python 应用目录&#xff1b;指向 Python 的连接。 …

Linux Python ping3库使用教程(ping3命令、ping命令)

文章目录 Linux Python ping3库使用教程1. 环境准备2. ping3库安装3. ping3基本使用4. ping3进阶使用5. 常见问题解答5.1 ping3库可以在Windows系统中使用吗&#xff1f;5.2 如何处理ping操作超时的情况&#xff1f;5.3 ping3库支持IPv6吗&#xff1f; Linux Python ping3库使用…