1# 地理位置 2 3>  **说明:** 4> - 从API Version 7 开始,该接口不再维护,推荐使用新接口[`@ohos.geolocation`](js-apis-geolocation.md)。 5> 6> - 本模块首批接口从API version 3开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 7 8 9## 导入模块 10 11 12``` 13import geolocation from '@system.geolocation'; 14``` 15 16 17## 权限列表 18 19ohos.permission.LOCATION 20 21 22## geolocation.getLocation 23 24getLocation(Object): void 25 26获取设备的地理位置。 27 28**系统能力:** SystemCapability.Location.Location.Lite 29 30**参数:** 31 32| 参数名 | 类型 | 必填 | 说明 | 33| -------- | -------- | -------- | -------- | 34| timeout | number | 否 | 超时时间,单位为ms,默认值为30000。<br/>设置超时,是为了防止出现权限被系统拒绝、定位信号弱或者定位设置不当,导致请求阻塞的情况。超时后会使用fail回调函数。<br/>取值范围为32位正整数。如果设置值小于等于0,系统按默认值处理。 | 35| coordType | string | 否 | 坐标系的类型,可通过getSupportedCoordTypes获取可选值,缺省值为wgs84。 | 36| success | Function | 否 | 接口调用成功的回调函数。 | 37| fail | Function | 否 | 接口调用失败的回调函数。 | 38| complete | Function | 否 | 接口调用结束的回调函数。 | 39 40success返回值: 41 42| 参数名 | 类型 | 说明 | 43| -------- | -------- | -------- | 44| longitude | number | 设备位置信息:经度。 | 45| latitude | number | 设备位置信息:纬度。 | 46| altitude | number | 设备位置信息:海拔。 | 47| accuracy | number | 设备位置信息:精确度。 | 48| time | number | 设备位置信息:时间。 | 49 50fail返回错误代码: 51 52| 错误码 | 说明 | 53| -------- | -------- | 54| 601 | 获取定位权限失败,失败原因:用户拒绝。 | 55| 602 | 权限未声明。 | 56| 800 | 超时,失败原因:网络状况不佳或GPS不可用。 | 57| 801 | 系统位置开关未打开。 | 58| 802 | 该次调用结果未返回前接口又被重新调用,该次调用失败返回错误码。 | 59 60**示例:** 61 62``` 63export default { 64 getLocation() { 65 geolocation.getLocation({ 66 success: function(data) { 67 console.log('success get location data. latitude:' + data.latitude); 68 }, 69 fail: function(data, code) { 70 console.log('fail to get location. code:' + code + ', data:' + data); 71 }, 72 }); 73 }, 74} 75``` 76 77 78## geolocation.getLocationType 79 80getLocationType(Object): void 81 82获取当前设备支持的定位类型。 83 84**系统能力:** SystemCapability.Location.Location.Lite 85 86**参数:** 87 88| 参数名 | 类型 | 必填 | 说明 | 89| -------- | -------- | -------- | -------- | 90| success | Function | 否 | 接口调用成功的回调函数。 | 91| fail | Function | 否 | 接口调用失败的回调函数。 | 92| complete | Function | 否 | 接口调用结束的回调函数。 | 93 94success返回值: 95 96| 参数名 | 类型 | 说明 | 97| -------- | -------- | -------- | 98| types | Array<string> | 可选的定位类型['gps', 'network']。 | 99 100**示例:** 101 102``` 103export default { 104 getLocationType() { 105 geolocation.getLocationType({ 106 success: function(data) { 107 console.log('success get location type:' + data.types[0]); 108 }, 109 fail: function(data, code) { 110 console.log('fail to get location. code:' + code + ', data:' + data); 111 }, 112 }); 113 }, 114} 115``` 116 117 118## geolocation.subscribe 119 120subscribe(Object): void 121 122订阅设备的地理位置信息。多次调用的话,只有最后一次的调用生效。 123 124**系统能力:** SystemCapability.Location.Location.Lite 125 126**参数:** 127 128| 参数名 | 类型 | 必填 | 说明 | 129| -------- | -------- | -------- | -------- | 130| coordType | string | 否 | 坐标系的类型,可通过getSupportedCoordTypes获取可选值,默认值为wgs84。 | 131| success | Function | 是 | 位置信息发生变化的回调函数。 | 132| fail | Function | 否 | 接口调用失败的回调函数。 | 133 134success返回值: 135 136| 参数名 | 类型 | 说明 | 137| -------- | -------- | -------- | 138| longitude | number | 设备位置信息:经度。 | 139| latitude | number | 设备位置信息:纬度。 | 140| altitude | number | 设备位置信息:海拔。 | 141| accuracy | number | 设备位置信息:精确度。 | 142| time | number | 设备位置信息:时间。 | 143 144fail返回错误代码: 145 146| 错误码 | 说明 | 147| -------- | -------- | 148| 601 | 获取定位权限失败,失败原因:用户拒绝。 | 149| 602 | 权限未声明。 | 150| 801 | 系统位置开关未打开。 | 151 152**示例:** 153 154``` 155export default { 156 subscribe() { 157 geolocation.subscribe({ 158 success: function(data) { 159 console.log('get location. latitude:' + data.latitude); 160 }, 161 fail: function(data, code) { 162 console.log('fail to get location. code:' + code + ', data:' + data); 163 }, 164 }); 165 }, 166} 167``` 168 169 170## geolocation.unsubscribe 171 172unsubscribe(): void 173 174取消订阅设备的地理位置信息。 175 176**系统能力:** SystemCapability.Location.Location.Lite 177 178**示例:** 179 180``` 181export default { 182 unsubscribe() { 183 geolocation.unsubscribe(); 184 }, 185} 186``` 187 188 189## geolocation.getSupportedCoordTypes 190 191getSupportedCoordTypes(): Array<string> 192 193获取设备支持的坐标系类型。 194 195**系统能力:** SystemCapability.Location.Location.Lite 196 197**返回值:** 198 199| 类型 | 非空 | 说明 | 200| -------- | -------- | -------- | 201| Array<string> | 是 | 表示坐标系类型,如[wgs84, gcj02]。 | 202 203**示例:** 204 205``` 206export default { 207 getSupportedCoordTypes() { 208 var types = geolocation.getSupportedCoordTypes(); 209 }, 210} 211```