跳到主要内容

位置信息

获取当前坐标地址。需要位置权限。

获取坐标

const loc = await Location.getLocation()
console.log(loc.latitude, loc.longitude)
// 34.75936567 113.69243507

也可以传一个分钟数,表示你希望位置足够新:

const loc = await Location.getLocation(5)

返回:

{
latitude: 34.75936567,
longitude: 113.69243507,
altitude: 104.13
}
字段类型说明
latitudenumber纬度
longitudenumber经度
altitudenumber海拔,单位米

获取地址

不传参数时,使用当前位置:

const mark = await Location.getPlacemark()
console.log(mark.city) // 某某市
console.log(mark.street) // 大河路
console.log(mark.country) // 中国

也可以传指定坐标:

const mark = await Location.getPlacemark({
latitude: 34.75936567,
longitude: 113.69243507
})

console.log(mark.city) // 某某市

返回字段:

字段说明
name地点名
street街道
streetNumber门牌号
district
city
state
country
countryCode国家码,如 CN
postalCode邮编

例:显示当前城市

const loc = await Location.getLocation()
const mark = await Location.getPlacemark(loc)

const city = mark.city || mark.name || "未知城市"
const position = `${loc.latitude}, ${loc.longitude}`

console.log("city", city)
console.log("position", position)

组件字段里可以绑定:

组件字段填什么
Textcontent${city}
Textcontent${position}

速查

await Location.getLocation()
await Location.getLocation(5)

await Location.getPlacemark()
await Location.getPlacemark({ latitude, longitude })