在调用接口页面的<script>部分增加如下配置。
import geolocation from '@system.geolocation'
或
var geolocation = require("@system.geolocation")
接口 |
描述 |
|---|---|
获取地理位置。 |
|
监听地理位置。如果多次调用,仅最后一次调用生效。 |
|
取消监听地理位置。 |
|
获取当前系统支持的定位类型。 |
|
获取支持的坐标系类型。 |
|
打开快应用内置地图选择位置。 |
|
使用快应用内置地图查看位置。 |
|
获取地理位置对应的经纬度。 |
|
获取位置对应的省份、城市等信息。 |
描述
获取地理位置。
OBJECT参数
参数 |
类型 |
是否必填 |
说明 |
|---|---|---|---|
coordType |
string |
否 |
注意:
非厂商联盟标准,华为定制化参数,可以通过device.getInfo()的engineProvider是否为”huawei”来判断是否在华为的环境上。为了提高定位成功率,在中国区强烈推荐使用gcj02坐标系,但是车机设备上请使用“wgs84”。 |
timeout |
long |
否 |
设置超时时间,单位是ms,默认值为30000。在权限被系统拒绝或者定位设置不当的情况下,有可能永远不能返回结果,因而需要设置超时。超时后会使用fail回调。 |
success |
function |
否 |
成功回调。 |
fail |
function |
否 |
失败回调,原因可能是用户拒绝。 |
complete |
function |
否 |
执行结束后的回调。 |
success返回值:
参数 |
类型 |
说明 |
|---|---|---|
longitude |
float |
经度。 |
latitude |
float |
纬度。 |
accuracy(1035+) |
float |
精确度。 |
time(1035+) |
long |
时间。 |
fail返回错误码:
错误码 |
说明 |
|---|---|
201 |
用户拒绝,获取定位权限失败。 |
202 |
参数错误。 |
203 |
服务不可用。 |
204 |
超时返回。 |
205 |
国家不在服务范围内。 |
1000 |
系统位置开关关闭。 |
示例代码
geolocation.getLocation({
success:function(data){
console.log("handling success: longitude=" + data.longitude + ", latitude=" + data.latitude);
},
fail: function(data, code) {
console.log("handling fail, code=" + code);
}
})
描述
监听地理位置。如果多次调用,仅最后一次调用生效。
如果使用系统定位能力(coordType为wgs84),调用本接口后,请勿再调用getLocation接口,否则将导致刷新地理位置过慢而超时的问题
OBJECT参数
参数 |
类型 |
是否必填 |
说明 |
|---|---|---|---|
coordType |
string |
否 |
注意:
非厂商联盟标准,华为定制化参数,可以通过device.getInfo()的engineProvider是否为”huawei”来判断是否在华为的环境上。为了提高定位成功率,在中国区强烈推荐使用gcj02坐标系,但是车机设备上请使用“wgs84”。 |
callback |
function |
是 |
每次位置信息发生变化,都会被回调。 |
fail |
function |
否 |
失败回调,原因可能是用户拒绝。 |
callback返回值:
参数 |
类型 |
说明 |
|---|---|---|
longitude |
float |
经度。 |
latitude |
float |
纬度。 |
accuracy(1035+) |
float |
精确度。 |
time(1035+) |
long |
时间。 |
fail返回错误码:
错误码 |
说明 |
|---|---|
201 |
用户拒绝,获取定位权限失败。 |
204 |
超时返回。 |
1000 |
系统位置开关关闭。 |
示例代码
geolocation.subscribe({
callback:function(data) {
console.log("handling success: longitude=" + data.longitude + ", latitude=" + data.latitude);
},
fail: function(data, code) {
console.log("handling fail, code=" + code);
}
})
描述
获取当前系统支持的定位类型。
OBJECT参数
参数 |
类型 |
是否必填 |
说明 |
|---|---|---|---|
success |
function |
否 |
成功回调。 |
fail |
function |
否 |
失败回调 |
complete |
function |
否 |
执行结束后的回调。 |
success返回值:
参数 |
类型 |
说明 |
|---|---|---|
types |
array |
支持的类型['gps','network'] |
示例代码
geolocation.getLocationType({
success: function (data) {
console.log("data - "+data.types)
},
fail: function (erromsg, errocode) {
console.log('getLocationType: ' + errocode + ': ' + erromsg)
}
})
描述
获取支持的坐标系类型。
返回值
字符串数组。当前支持的坐标系类型,如['wgs84']。
示例代码
var result = geolocation.getSupportedCoordTypes(); console.log(result);
描述
打开快应用内置地图选择位置。仅限中国大陆使用,需要获取设备定位权限。
Object参数
参数 |
类型 |
是否必填 |
默认值 |
说明 |
|---|---|---|---|---|
latitude |
number |
否 |
- |
指定中心点纬度,如果为空则显示当前位置。 |
longitude |
number |
否 |
- |
指定中心点经度,如果为空则显示当前位置。 |
coordType |
string |
否 |
wgs84 |
坐标系,支持wgs84和gcj02,默认使用wgs84。仅作用于指定中心点的经纬度坐标系。仅支持国内。 |
success |
function |
否 |
- |
成功回调。 |
fail |
function |
否 |
- |
失败回调 |
complete |
function |
否 |
- |
执行结束后的回调。 |
success返回值:
参数 |
类型 |
说明 |
|---|---|---|
name |
string |
位置名称。 |
address |
string |
详细地址。 |
coordType |
string |
返回值经纬度的坐标系,坐标固定使用gcj02坐标系。 |
latitude |
string |
纬度,浮点数,范围为-90 ~ 90,负数表示南纬。 |
longitude |
string |
经度,浮点数,范围为-180 ~ 180,负数表示西经。 |
示例代码
geolocation.chooseLocation({
latitude: 31.94830062319531,
longitude: 118.84177721902,
coordType: "gcj02",
success: function (data) {
console.log('chooseLocation success : ' + JSON.stringify(data));
},
fail: function (erromsg, errocode) {
console.log('chooseLocation fail : '+ errocode + ': ' + erromsg);
},
complete: function () {
console.log('chooseLocation complete.');
},
})
描述
使用快应用内置地图查看位置。仅限中国大陆使用,需要获取设备定位权限。
chooseLocation 可以选择地理位置,也可以搜索位置,确定选择的位置后会返回一个经纬度坐标,然后使用 openLocation 进行导航。
如果手机安装了百度地图和高德地图,导航app中才会显示。
OBJECT参数
参数 |
类型 |
是否必填 |
默认值 |
说明 |
|---|---|---|---|---|
latitude |
number |
是 |
- |
纬度,范围为-90 ~ 90,负数表示南纬。 |
longitude |
number |
是 |
- |
经度,范围为-180 ~ 180,负数表示西经。 |
coordType |
string |
否 |
wgs84 |
坐标系,支持wgs84和gcj02,默认使用wgs84。 |
scale |
number |
否 |
18 |
缩放比例,范围5 ~ 18。 |
name |
string |
否 |
- |
位置名。 |
address |
string |
否 |
- |
地址的详细说明。 |
success |
function |
否 |
- |
成功回调。 |
fail |
function |
否 |
- |
失败回调。 |
complete |
function |
否 |
- |
执行结束后的回调。 |
示例代码
geolocation.openLocation({
latitude: 31.94830062319531,
longitude: 118.84177933970965,
coordType: "gcj02",
name: "竹山路地铁站",
address: "竹山路地铁站",
scale: 18,
success: function () {
console.log('openLocation success . ');
},
fail: function (erromsg, errocode) {
console.log('geolocation.openLocation----------' + errocode + ': ' + erromsg)
},
complete: function () {
console.log('openLocation complete.');
}
})
描述
获取地理位置对应的经纬度。只支持国内地理位置 ,仅限中国大陆使用。
OBJECT参数
参数 |
类型 |
是否必填 |
默认值 |
说明 |
|---|---|---|---|---|
city |
string |
是 |
- |
地址所在城市。 |
address |
string |
是 |
- |
地址。 |
success |
function |
否 |
- |
成功回调。 |
fail |
function |
否 |
- |
失败回调。 |
complete |
function |
否 |
- |
执行结束回调。 |
success返回值:
参数 |
类型 |
说明 |
|---|---|---|
longitude |
number |
gcj02坐标系的经度。 |
latitude |
number |
gcj02坐标系的纬度。 |
fail返回错误码
错误码 |
说明 |
|---|---|
200 |
接口功能异常。 |
202 |
参数错误。 |
205 |
不支持的国家。 |
示例代码
geolocation.geocodeQuery({
city: "南京",
address: "南京大学",
success: function (ret) {
console.info('### geolocation.geocodeQuery ### ${ret.latitude}: ${ret.longitude}')
},
fail: function (errormsg, errorcode) {
console.info('### geolocation.geocodeQuery ### ${errorcode}: ${errormsg}')
}
})
描述
获取位置对应的省份、城市等信息。只支持国内地理位置,仅限中国大陆使用。
OBJECT参数
参数 |
类型 |
是否必填 |
默认值 |
说明 |
|---|---|---|---|---|
longitude |
number |
是 |
- |
经度。 |
latitude |
number |
是 |
- |
纬度。 |
coordType |
string |
否 |
wgs84 |
坐标系,支持 wgs84 和 gcj02,默认使用 wgs84。 |
includePoiInfo |
boolean |
否 |
false |
success 回调的返回值是否需要包含设置位置附近的 POI 信息。默认 false ,即 success 回调中不返回。 |
success |
function |
否 |
- |
成功回调。 |
fail |
function |
否 |
- |
失败回调。 |
complete |
function |
否 |
- |
执行结束回调。 |
success返回值:
参数 |
类型 |
说明 |
|---|---|---|
countryName |
string |
国家名称。 |
province |
string |
省份名称。 |
city |
string |
城市名称。 |
district |
string |
区县名称。 |
street |
string |
街道名称(行政区划中的街道层级)。 |
address |
string |
简要地址信息。 |
poiInfoList |
array |
设置位置附近的POI信息,详细参见“poiInfo参数说明”。 |
poiInfoList参数说明
参数 |
类型 |
说明 |
|---|---|---|
poiName |
string |
poi名字。 |
longitude |
number |
经度。 |
latitude |
number |
纬度。 |
city |
string |
所在城市。 |
address |
number |
地址。 |
phone |
number |
电话。 |
fail返回错误码
错误码 |
说明 |
|---|---|
200 |
接口功能异常。 |
202 |
参数错误。 |
205 |
不支持的国家。 |
示例代码
geolocation.reverseGeocodeQuery({
latitude: 30.513070973744515,
longitude: 114.41322124959942,
coordType: "gcj02",
includePoiInfo:true,
success: function (ret) {
console.info(`### geolocation.reverseGeocodeQuery ###` + JSON.stringify(ret))
},
fail: function (errormsg, errorcode) {
console.info(`### geolocation.reverseGeocodeQuery ### ${errorcode}: ${errormsg}`)
}
})
<template>
<div>
<div>
<input type="button" onclick="getDeviceInfo" value="通过获取设备信息判断是否是华为设备" />
<input type="button" onclick="getGeolocation" value="获取地理位置" />
<input type="button" onclick="listenGeolocation" value="监听地理位置" />
<input type="button" onclick="cancelGeolocation" value="取消地理位置监听" />
<input type="button" onclick="getLocationType" value="获取当前支持的定位类型" />
</div>
</div>
</template>
<style>
.item-container {
margin-bottom: 50px;
margin-right: 60px;
margin-left: 60px;
flex-direction: column;
}
.item-content {
flex-direction: column;
background-color: #ffffff;
padding: 30px;
margin-bottom: 100px;
align-items: flex-start;
}
.btn {
height: 80px;
text-align: center;
border-radius: 5px;
margin-right: 60px;
margin-left: 60px;
margin-bottom: 50px;
color: #ffffff;
font-size: 30px;
background-color: #0faeff;
}
</style>
<script>
import device from '@system.device'
import geolocation from '@system.geolocation'
export default {
data: {
isHuawei: false
},
//非厂商联盟标准,华为定制化参数,可以通过device.getInfo()的engineProvider是否为”huawei”来判断是否在华为的环境上。
getDeviceInfo: function () {
var that = this;
device.getInfo({
success: function (ret) {
var deviceInfo = JSON.stringify(ret);
if (deviceInfo.indexOf("engineProvider") >= 0 && deviceInfo.indexOf("huawei") >= 0) {
that.isHuawei = true;
console.log('this device is huawei')
} else {
that.isHuawei = false;
console.log('this device is not huawei')
}
},
fail: function (erromsg, errocode) {
console.log('device.getInfo----------' + errocode + ': ' + erromsg)
}
})
},
//getLocation接口
getGeolocation: function () {
var that = this;
geolocation.getLocation({
coordType:"wgs84", //调用系统定位能力
timeout: 2000,
success: function (ret) {
console.log('geolocation success---------' + JSON.stringify(ret));
},
fail: function (erromsg, errocode) {
console.log('geolocation.getLocation----------' + errocode + ': ' + erromsg)
},
complete: function () {
console.log('geolocation complete----------')
}
})
},
//subscribe接口
listenGeolocation: function () {
geolocation.subscribe({
callback: function (ret) {
console.log('geolocation.subscribe success---------' + JSON.stringify(ret));
},
fail: function (erromsg, errocode) {
console.log('geolocation.subscribe----------' + errocode + ': ' + erromsg)
}
})
},
//unsubscribe接口
cancelGeolocation: function () {
geolocation.unsubscribe();
},
//getLocationType接口
getLocationType: function () {
geolocation.getLocationType({
success: function (data) {
console.log("ret - " + data.types)
}
})
}
}
</script>