uniapp 定位服务_uniapp使用高德地图定位

本文介绍了如何在uniapp中使用高德地图进行定位服务。首先需要在manifest.json配置高德地图的key,然后检查并引导用户开启手机定位权限。接着,通过uniapp的getLocation API获取坐标,并利用高德API解析获取详细地理位置信息,最终将数据用于渲染展示。
摘要由CSDN通过智能技术生成

首先需要申请高德账号并把开发程序给绑定,这里直接跳过了

高德地图.png

然后在manifest.json中进行配置,这里的key都是图1中配置得到的key

高德地图2.png

首先先检查手机是否打开了定位权限,如果没有就弹框引导用户打开手机权限

export const openPosition = ()=>{

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:'请打开定位服务功能',

showCancel:false,

success() {

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

})

}

}

})

}

}

}

先通过uniapp的api获取当前的地理位置坐标,然后通过高德api进行解析,获取地理位置数据

getCurLocation(){

uni.getLocation({

geocode:true,

type: 'gcj02',

success:async (res)=>{

let { pois } = await posToCity(

{

latitude: res.latitude,

longitude: res.longitude,

},

1

);

this.address = pois[0].title

this.latitude = res.latitude

this.longitude = res.longitude

uni.setStorageSync('latitude',res.latitude)

uni.setStorageSync('longitude',res.longitude)

this.curAddress = store.state.address.curPickAddress.title

}

})

}

// 高德坐标解析

export const posToCity = async (location, extensions = 0) => new Promise((resolve, reject) => {

uni.request({

url: `https://restapi.amap.com/v3/geocode/regeo`,

method: 'GET',

data: {

key: gdMapKey,

location: `${location.longitude},${location.latitude}`,

extensions: extensions ? 'all' : 'base'

},

success: ({data}) => {

const {status, info, regeocode} = data

if (status === '1') {

// console.log(regeocode)

if ('pois' in regeocode) {

regeocode.pois = regeocode.pois.filter(item => typeof item.address === 'string' && typeof item.location === 'string').map(item => ({

id: item.id,

title: item.name,

address: item.address,

location: {

lat: item.location.split(',')[1],

lng: item.location.split(',')[0]

}

}))

}

resolve(regeocode)

} else {

reject(info)

}

},

fail: err => {

reject(err)

}

})

})

最后拿到数据进行渲染,得到成果图

高德地图3.jpg

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 ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值