uniapp App端使用高德地图

uniapp App端使用高德地图

第一步:

先去高德官网申请keyhttps://console.amap.com/dev/key/app
这里插入图片描述
关于SHA1生成方法如下:https://lbs.amap.com/faq/android/map-sdk/create-project/43112
我使用的是使用 keytool(jdk自带工具)获取SHA1
PackageName包名和你需要云打包的项目Android包名一样
在这里插入图片描述

第二步:

打开项目manifest.json文件,将所需的权限配置好
在这里插入图片描述
在这里插入图片描述

第三步:

下载微信小程序SDK:https://lbs.amap.com/api/wx/download
然后放进uni项目,在需要用的到页面引入

	import amap from "../../../common/amap-wx.130.js";//微信小程序SDK
	export default {
		data() {
			return {
				key: "高德申请的key",
				amapPlugin: null,
			}
		},
		onLoad(options) {
			// #ifdef APP-PLUS
			this.getAppLocation();
			// #endif
		},
}
methods: {
	//高德地图
	getAppLocation() {
		const _this = this;
		this.amapPlugin = new amap.AMapWX({
			key: this.key //该key 是在高德中申请的微信小程序key
		});
		this.amapPlugin.getRegeo({
			// type: 'gcj02', //map 组件使用的经纬度是国测局坐标, type 为 gcj02
			success: function(res) {
				console.log('app端地图')
				console.log(res);
				_this.getCurLocation();
			},
			fail: (res) => {
				console.log(res);
				//检测app端是否开启权限
				_this.openPosition();
			}
		});
	},
	openPosition() {
		const _this = this;
		let system = uni.getSystemInfoSync()
		if (system.platform === 'android') { //判断平台
			var context = plus.android.importClass("android.content.Context")
			var locationManager = plus.android.importClass("android.location.LocationManager")
			var main = plus.android.runtimeMainActivity()
			var mainSvr = main.getSystemService(context.LOCATION_SERVICE)
			console.log('GPS', mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER))
			if (!mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER)) {
				uni.showModal({
					title: '提示',
					content: '请在设置中打开定位服务功能',
					success(res) {
						if (res.confirm) {
							if (!mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER)) {
								let main = plus.android.runtimeMainActivity();
								let Intent = plus.android.importClass("android.content.Intent");
								let mIntent = new Intent('android.settings.ACTION_SETTINGS');
								main.startActivity(mIntent); //打开系统设置GPS服务页面 
							} else {
								uni.showToast({
									title: '定位功能已启动',
									duration: 2000
								})
							}
						} else if (res.cancel) {
							// 拒绝授权返回上一页
							_this.goBack();
						}
					}
				})
			} else {
				//获取当前用户所在位置
				_this.getCurLocation();
			}
		}
	},
	getCurLocation() {
		const _this = this;
		uni.getLocation({
			geocode: true,
			type: 'gcj02',
			success: async (res) => {
				console.log(res);
			}
		})
	},
	//搜索地图
	getLcotion() {
		const _this = this;
		uni.chooseLocation({
			success(list) {
				console.log(list);
			}
		})
	},	
}

最后云打包:

Android包名和申请高德key填写的PackageName一致
选择自有证书,如果使用公共测试证书的话,uni.chooseLocation这个方法会搜索不到位置
别名、证书私钥密码、证书文件请查看图二生成的秘钥
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 7
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
uniapp使用高德地图定位,可以按照以下步骤进行操作。首先,需要在项目中引入高德地图的SDK。可以通过下载高德地图微信小程序版SDK,并将amap-wx.js文件复制到项目的common目录中。然后,在需要使用定位功能的页面中,引入amap-wx.js文件,并在onLoad方法中初始化一个高德地图的实例对象。接下来,可以利用高德地图的SDK来获取当前位置的地址信息。可以使用getRegeo方法来获取当前位置的经纬度和地址名称。具体的代码示例可以参考引用\[2\]和引用\[3\]中的代码。 #### 引用[.reference_title] - *1* [uniapp使用高德地图定位](https://blog.csdn.net/liyu_ya/article/details/126951270)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [uniapp使用高德地图定位(兼容app)](https://blog.csdn.net/jinxi1112/article/details/121906333)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [uniapp开发APP微信小程序——使用高德实现定位](https://blog.csdn.net/qq_42374233/article/details/112857249)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值