# uni.openLocation(OBJECT)

Use the built-in map to view locations.

Platform difference description

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

OBJECT parameter description

Parameter name Type Required Instruction Platform difference description
latitude Float Yes Latitude, from -90 to 90. Negative number indicates south latitude, using gcj02 coordinate of State Bureau of Surveying and Mapping of China
longitude Float Yes Longitude, from -180 to 180. Negative number indicates west longitude, using gcj02 coordinate of State Bureau of Surveying and Mapping of China.
scale Int No Scale, the range is 5~18, the default is 18 WeChat applet
name String No Location Name Alipay Required
address String No Detailed description of the address Alipay required
success Function No Callback function for successful interface calling
fail Function No Callback function for failed interface calling
complete Function No Callback function for closed interface calling (available both for successful and failed calling)

Example

uni.getLocation({
	type: 'gcj02', //返回可以用于uni.openLocation的经纬度
	success: function (res) {
		const latitude = res.latitude;
		const longitude = res.longitude;
		uni.openLocation({
			latitude: latitude,
			longitude: longitude,
			success: function () {
				console.log('success');
			}
		});
	}
});

Tips

  • This API is a non-atomic packaging interface. Developers can make their own pages to achieve similar functions for customization.
  • In case of inaccurate localization, refer to Matters needing attention in the uni.getLocation document
  • <map> component is defaulted to the coordinates of the State Bureau of Surveying and Mapping of China. When calling uni.getLocation return result and passing it to the <map> component, the type must be specified as gcj02.
  • App 端使用地图组件需要向高德等三方服务商申请SDK资质,获取AppKey,打包时需要在manifest的SDK配置中填写Appkey。在manifest可视化界面有详细申请指南,详见:https://ask.dcloud.net.cn/article/29
  • H5 端使用地图和定位相关,需要在 manifest.json 内配置腾讯或谷歌等三方地图服务商申请的秘钥(key)。
  • iOS端 如需使用第三方地图进行导航,需要在 manifest.json 文件内增加 urlschemewhitelist 节点,在线打包即可
  • 不同平台打开的地图不一样,App端是高德地图(不支持百度地图)、H5端是腾讯地图或谷歌地图、微信小程序端是腾讯地图、百度小程序是百度地图、阿里小程序是高德地图、抖音小程序是高德地图。
{
    "app-plus": {
        "distribute": {
            "ios": {
                "urlschemewhitelist": [
                    "baidumap",
                    "iosamap",
                    "qqmap"
                ]
            }
        }
    }
}

# 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