# uni.getLocation(OBJECT)

Get the current location and speed.

OBJECT parameter description

Parameter Name Type Required Description Platform Difference Description
type String 默认为 wgs84 返回 gps 坐标,gcj02 返回国测局坐标,可用于 uni.openLocation 和 map 组件坐标,App 和 H5 需配置定位 SDK 信息才可支持 gcj02。
altitude Boolean 传入 true 会返回高度信息,由于获取高度需要较高精确度,会减慢接口返回速度 抖音小程序、飞书小程序、支付宝小程序不支持
geocode Boolean No The default is false, whether to resolve the address information Supported only by the App platform (Android needs to specify the type as gcj02 and configure the three-party positioning SDK)
highAccuracyExpireTime Number No High Accuracy Positioning Timeout Time (ms), returns the highest accuracy within the specified time, this value is more than 3000ms for high accuracy positioning to be effective App (3.2.11+), H5 (3.2.11+), WeChat MiniApp(basic library 2.9.0+)
timeout String No The default is 5, the positioning timeout, in seconds Only supported by MiniApp
cacheTimeout Number No Location cache timeout, in seconds; the current location data is cached every time, and the timestamp is recorded. When the next call is within cacheTimeout, the cached data will be returned Only Feishu MiniApp, Alipay MiniApp Support
accuracy String No The default is high, specify the expected accuracy, support high, best. When specifying high, the expected precision value is 100m, and when specifying best, the expected precision value is 20m. When the accuracy obtained from the positioning does not meet the conditions, the positioning will continue before the timeout, and try to obtain the positioning results that meet the requirements Only supported by MiniApp
isHighAccuracy Boolean No Enable high-precision positioning App (3.4.0+), H5 (3.4.0+), WeChat MiniApp(basic library 2.9.0+)
success Function is the callback function for the successful call of the interface. For details, please refer to the return parameter description.
fail Function No Callback function for interface call failure
complete Function No The callback function of the end of the interface call (the call will be executed if the call succeeds or fails)

success return parameter description

Parameters Description
latitude Latitude, a floating-point number, the range is -90~90, a negative number means south latitude
longitude Longitude, a floating point number, the range is -180~180, a negative number means west longitude
speed speed, float, unit m/s
accuracy Accuracy of location
altitude Altitude in m
verticalAccuracy Vertical Accuracy, in m (unavailable for Android, returns 0)
horizontalAccuracy Horizontal Accuracy, in m
address Address information (only supported by the App, you need to configure geocode to true)

address address information description

property type description description
country String Country For example, "China", if this information cannot be obtained, return undefined
province String province name such as "Beijing", if this information cannot be obtained, return undefined
city String City name For example, "Beijing", if this information cannot be obtained, return undefined
district String District (county) name For example, "Chaoyang District", if this information cannot be obtained, return undefined
street String Street information For example, "Jiuxianqiao Road", if this information cannot be obtained, return undefined
streetNum String Get the street number information For example, "No. 3", if this information cannot be obtained, return undefined
poiName String POI information For example, "Electronic City. International Electronic Headquarters", if this information cannot be obtained, return undefined
postalCode String Postal code For example "100016", if this information cannot be obtained, return undefined
cityCode String City Code For example, "010", if this information cannot be obtained, return undefined

Example

uni.getLocation({
	type: 'wgs84',
	success: function (res) {
		console.log('当前位置的经度:' + res.longitude);
		console.log('当前位置的纬度:' + res.latitude);
	}
});

注意事项

  • H5 Platform
    • On newer browsers, the H5 side obtains location information and requires deployment on the https service, and the local preview (localhost) can still use the http protocol.
    • On a domestic Android phone, if the H5 cannot be positioned, check whether the phone has enabled location services, GPS, whether the ROM has given the browser location permission, and whether the browser has given a positioning query box to the web page pop-up request.
    • Android phone cannot locate when the native app is embedded with H5, and the native app needs to process the Webview.
    • 移动端浏览器 普遍仅支持GPS定位,在GPS信号弱的地方可能定位失败。
    • PC 设备 使用 Chrome 浏览器的时候,位置信息是连接谷歌服务器获取的,国内用户可能获取位置信息失败,推荐使用Edge进行获取位置信息
    • WeChat public account can use WeChat js sdk, see details
    • Version 2.9.9 and above, optimize uni.getLocation to support location by IP. By default, it is obtained through GPS. If the acquisition fails, the alternative is to obtain it through IP positioning, and the secret key of the third-party map service platform needs to be filled in. key configuration: manifest.json ---> H5 configuration ---> positioning and map ---> key.
  • App Platform
    • Android is blocked by Google services, or there is no GMS on the mobile phone. If you want to locate normally, you need to apply for an SDK qualification from a third-party service provider such as AutoNavi and obtain an AppKey. Otherwise, the positioning will be inaccurate after packaging. When cloud packaging, you need to fill in the Appkey in the SDK configuration of the manifest. There are detailed application guidelines on the manifest visual interface, see: https://ask.dcloud.net.cn/article/29. Offline packaging is configured in the native project by itself. Note that package name, appkey, and certificate information must match. The real machine running can be positioned normally because the real machine running base uses the sdk configuration that DCloud applied to AutoNavi, which must be applied by the developer after packaging. If the mobile phone comes with GMS and the network environment can access the google positioning server normally, there is no need to fill in the sdk configuration of Gaode positioning in the manifest.
    • Note that the mobile phone itself must enable positioning, and at the same time give the App the positioning permission. Permission judgment can refer to: https://uniapp.dcloud.net.cn/api/system/getappauthorizesetting.html
    • The <map> component defaults to the coordinate gcj02 of the National Survey Bureau. When calling uni.getLocation to return the result to the <map> component, you need to specify the type as gcj02.
    • Positioning and map are two things. After getting the location coordinates through getLocation, it can be displayed on any map map, such as positioning using Gaode, and the map using google's webview version of the map. If the coordinate system is different, pay attention to converting the coordinate system.
    • If you use web-view to load the map, there is no need to configure the sdk configuration of the map in the manifest.
    • 持续定位方案:iOS端可以申请持续定位权限,参考。Android如果进程被杀,代码无法执行,可以在插件市场搜索保活相关原生语言插件避免App被系统杀死。即使使用了原生语言插件保活,也很容易被杀,此时可以使用unipush ,通过推送消息提示用户激活App
    • 3.3.0 版本以上 优化系统定位模块,可不使用三方定位SDK的进行高精度定位,具体参考:系统定位
    • 鸿蒙系统 不支持系统定位,需要配置三方sdk,比如高德,同时设置坐标系参数为 type: 'gcj02'
    • 如需使用腾讯定位sdk,可下载腾讯定位插件,在插件中配置key打包后生效,腾讯定位是ext api插件引用到工程后,会覆盖uni.getLocation的实现,替换掉系统定位。
  • MiniApp Platform
    • The api does not return the Chinese description of the detailed address by default. There are two ways to need a Chinese address: 1. Using the AutoNavi map MiniApp sdk, you can get a Chinese address on the app and WeChat, Reference. 2. Only consider the app, you can also use plus.geolocation to get the Chinese address. The App SDK configuration in the manifest isonly used for the app, and the MiniApp not need to be configured here.
    • The user authorization API can be used to determine whether the user has granted the application location permission, see details
    • In the WeChat MiniApp Program, when the user leaves the app, this interface cannot be called, and you need to apply for [Background continuous positioning permission](https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability /authorize.html), and in the new version, you need to use [wx.onLocationChange]( https://developers.weixin.qq.com/miniprogram/dev/api/location/wx.onLocationChange.html ) to monitor location information changes; when This interface can continue to be called when the user clicks "Show on top of chat".

# uni.chooseLocation(OBJECT)

Open the map to select a location.

Platform Difference Description

App H5 微信小程序 支付宝小程序 百度小程序 抖音小程序、飞书小程序 QQ小程序 快手小程序 京东小程序
x x

OBJECT parameter description

Parameter Name Type Required Description Platform Difference Description
latitude Number No Destination latitude WeChat MiniApp(2.9.0+), H5-Vue3 (3.2.10+)
longitude Number No Destination longitude WeChat MiniApp(2.9.0+), H5-Vue3 (3.2.10+)
keyword String No Search keywords, only supported by App platform
success Function is the callback function for the successful call of the interface. For details, please refer to the return parameter description.
fail Function No The callback function of the failed API call (triggered when the location fails to be acquired, the user cancels, etc.)
complete Function No The callback function of the end of the interface call (the call will be executed if the call succeeds or fails)

Notice

  • 因平台差异,如果SDK配置百度地图,需要设置 keyword,才能显示相关地点
  • 非 weex 编译模式不支持百度地图

success return parameter description

Parameters Description
name Location Name
address Detailed address
latitude Latitude, a floating-point number, the range is -90~90, a negative number represents the southern latitude, using the gcj02 National Survey Bureau coordinate system.
longitude Longitude, a floating-point number, the range is -180~180, the negative number represents the west longitude, using the gcj02 National Survey Bureau coordinate system.

Example

uni.chooseLocation({
	success: function (res) {
		console.log('位置名称:' + res.name);
		console.log('详细地址:' + res.address);
		console.log('纬度:' + res.latitude);
		console.log('经度:' + res.longitude);
	}
});

Notice

  • Different terminals use different underlying map engines for map selection. For details, see map service provider support for the map map component.
    • Baidu positioning can also be used in app, configured in the manifest, and it will take effect after packaging.
    • Only AutoNavi and Google Maps (3.4+) can be used in app-nvue, but Baidu Maps cannot be used. In addition, the API for selecting a map and viewing the map location only supports AutoNavi and Google Maps (3.4+). Therefore, if there is no special need on the App side, it is recommended to use the Gaode map.
  • H5 terminal uses maps and positioning related, you need to configure the secret key (key) applied by third-party map service providers such as Tencent or Google in manifest.json.
  • WeChat js sdk can be used in WeChat built-in browser, see details
  • chooseLocation is an encapsulated API. If developers feel that it is not flexible enough, they can encapsulate it based on the original map component. The plugin market already has a variety of packaging examples.
  • Android App端 位置不准,见上文 uni.getLocation 的注意事项
  • 微信小程序在2023年10月17日之后,使用API需要配置隐私协议

# Three-party positioning and map service fee description

使用三方定位或者地图服务,需向服务提供商(如:高德地图、百度地图、腾讯地图、谷歌地图)申请商业授权和缴纳费用(5万/年)。

DCloud为开发者争取了福利,可优惠获取高德、腾讯的商业授权。如有需求请发邮件到bd@dcloud.io(注明你的公司名称、应用介绍、HBuilder账户);你也可以直接通过uni-im发起在线咨询,在线咨询地址:DCloud地图服务专员

详见:https://uniapp.dcloud.net.cn/tutorial/app-geolocation.html#lic

# unicloud-city-select 城市选择组件

若想要实现城市选择功能,可以使用 unicloud-city-select 城市选择组件。

运行效果图

下载地址:https://ext.dcloud.net.cn/plugin?name=unicloud-city-select

文档地址:https://doc.dcloud.net.cn/uniCloud/unicloud-city-select.html