目录

Location

API Declaration

Add the following configuration to the features attribute in the manifest.json file:

{"name": "system.geolocation"}

Module Import

Add the following configuration to the script element on the page where the API will be called:

import geolocation from '@system.geolocation'

Or

var geolocation = require("@system.geolocation")

Application Scope

Item

Description

Applicable devices

Mobile phones, tablets, and telematics devices

Applicable regions

Global

Definition

API

Description

geolocation.getLocation(OBJECT)

Obtains a location.

geolocation.subscribe(OBJECT)

Listens to the location. If this API is called multiple times, only the last call takes effect.

geolocation.unsubscribe()

Cancels listening to the location.

geolocation.getLocationType(OBJECT)

Obtains the location type supported by the system.

geolocation.getSupportedCoordTypes()

Obtains the supported coordinate types.

geolocation.chooseLocation(OBJECT)

Opens the built-in map of the quick app and selects a location.

geolocation.openLocation(OBJECT)

Views a location in the built-in map of the quick app.

geolocation.geocodeQuery(OBJECT)

Obtains the longitude and latitude of a location.

geolocation.reverseGeocodeQuery(OBJECT)

Obtains detailed address information including province and city for a location.

[h2]geolocation.getLocation(OBJECT)

Description

Obtains a location.

OBJECT parameters

Parameter

Type

Mandatory

Description

coordType

string

No

  • wgs84: The location will be returned using the location capability of the system. This is the default value.
  • gcj02: Chinese State Bureau of Surveying and Mapping coordinate system GCJ-02. In this case, the location will be returned by integrating the location ability of a third-party SDK. This value applies only to the Chinese mainland.
CAUTION:

This parameter is defined by Huawei, but not a standard parameter defined by the vendor alliance. You can determine whether your quick app is running on a Huawei device by checking whether the engineProvider parameter returned by device.getInfo() is set to huawei. To improve the location success rate, the GCJ-02 coordinate system is recommended in the Chinese mainland. However, there is an exception. WGS 84 is recommended for head units regardless of the country or region.

timeout

long

No

Timeout interval, in milliseconds. The default value is 30000. When the request of obtaining a location is rejected by the system or the location settings are incorrect, it is possible that no result can be returned. Therefore, a timeout interval needs to be specified. Upon timeout, the fail callback is triggered.

success

function

No

Callback upon success.

fail

function

No

Callback upon failure. A possible cause is that the request is rejected by the user.

complete

function

No

Callback upon completion.

Parameters returned by success

Parameter

Type

Description

longitude

float

Longitude.

latitude

float

Latitude.

accuracy (1035+)

float

Accuracy of the longitude and latitude.

time (1035+)

long

Time.

Result codes returned by fail

Result Code

Description

201

The user has rejected the request of obtaining a location.

202

Invalid parameter.

203

Service unavailable.

204

Timeout occurs.

205

The country or region is not within the service scope.

1000

The location feature is disabled in the system settings of the device.

Sample code

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);
  }
})

[h2]geolocation.subscribe(OBJECT)

Description

Listens to the location. If this API is called multiple times, only the last call takes effect.

If the system location ability (coordType set to wgs84) is used, do not call the getLocation API after you have called this API. Otherwise, location update will take a long time and timeout will occur.

OBJECT parameters

Parameter

Type

Mandatory

Description

coordType

string

No

  • wgs84: The location will be returned using the location capability of the system. This is the default value.
  • gcj02: Chinese State Bureau of Surveying and Mapping coordinate system GCJ-02. In this case, the location will be returned by integrating the location ability of a third-party SDK. This value applies only to the Chinese mainland.
CAUTION:

This parameter is defined by Huawei, but not a standard parameter defined by the vendor alliance. You can determine whether your quick app is running on a Huawei device by checking whether the engineProvider parameter returned by device.getInfo() is set to huawei. To improve the location success rate, the GCJ-02 coordinate system is recommended in the Chinese mainland. However, there is an exception. WGS 84 is recommended for head units regardless of the country or region.

callback

function

Yes

Function called back whenever the location information changes.

fail

function

No

Callback upon failure. A possible cause is that the request is rejected by the user.

Returns

Parameter

Type

Description

longitude

float

Longitude.

latitude

float

Latitude.

accuracy (1035+)

float

Accuracy of the longitude and latitude.

time (1035+)

long

Time.

Result codes returned by fail

Result Code

Description

201

The user has rejected the request of obtaining a location.

204

Timeout occurs.

1000

The location feature is disabled in the system settings of the device.

Sample code

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);
    }
})

[h2]geolocation.unsubscribe()

Description

Cancels listening to the location.

Sample code

geolocation.unsubscribe()

[h2]geolocation.getLocationType(OBJECT) (1010+)

Description

Obtains the location type supported by the system.

OBJECT parameters

Parameter

Type

Mandatory

Description

success

function

No

Callback upon success.

fail

function

No

Callback upon failure.

complete

function

No

Callback upon completion.

Parameters returned by success

Parameter

Type

Description

types

array

Supported location types, for example: ['gps','network'].

Sample code

geolocation.getLocationType({
    success: function (data) {
        console.log("data - "+data.types)
    },
    fail: function (erromsg, errocode) {
        console.log('getLocationType: ' + errocode + ': ' + erromsg)
    }
})

[h2]geolocation.getSupportedCoordTypes() (1045+)

Description

Obtains the supported coordinate types.

Returns

A string array, which indicates the supported coordinate system types, for example, ['wgs84'].

Sample code

var result = geolocation.getSupportedCoordTypes();
console.log(result);

[h2]geolocation.chooseLocation(OBJECT) (1070+)

Description

Opens the built-in map of the quick app and selects a location. This function is available only in the Chinese mainland. You need to obtain the device location permission.

OBJECT parameters

Parameter

Type

Mandatory

Default Value

Description

latitude

number

No

-

Latitude of the center point. If this parameter is left empty, the current location is displayed.

longitude

number

No

-

Longitude of the center point. If this parameter is left empty, the current location is displayed.

coordType

string

No

wgs84

Coordinate system of the specified center point. The options are wgs84 and gcj02. The default value is wgs84. This parameter is supported only in the Chinese mainland.

success

function

No

-

Callback upon success.

fail

function

No

-

Callback upon failure.

complete

function

No

-

Callback upon completion.

Parameters returned by success

Parameter

Type

Description

name

string

Location name.

address

string

Detailed address.

coordType

string

Coordinate system of the returned longitude and latitude. The coordinate system gcj02 is used.

latitude

string

Latitude. The value is a floating-point number ranging from –90 to 90. A negative value indicates a south latitude.

longitude

string

Longitude. The value is a floating-point number ranging from –180 to 180. A negative value indicates a west longitude.

Sample code

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.');
     },
})

[h2]geolocation.openLocation(OBJECT) (1070+)

Description

Views a location in the built-in map of the quick app. This function is available only in the Chinese mainland. You need to obtain the device location permission.

Call chooseLocation to select or search for a location. After the location is determined, its longitude and latitude will be returned. Then, call openLocation for navigation.

To display Baidu Maps or AutoNavi Map in the navigation quick app, they must be installed on the phone first.

OBJECT parameters

Parameter

Type

Mandatory

Default Value

Description

latitude

number

Yes

-

Latitude. The value ranges from –90 to 90. A negative value indicates a south latitude.

longitude

number

Yes

-

Longitude. The value ranges from –180 to 180. A negative value indicates a west longitude.

coordType

string

No

wgs84

Coordinate system. The options are wgs84 and gcj02. The default value is wgs84.

scale

number

No

18

Zoom ratio, ranging from 5 to 18.

name

string

No

-

Location name.

address

string

No

-

Detailed address.

success

function

No

-

Callback upon success.

fail

function

No

-

Callback upon failure.

complete

function

No

-

Callback upon completion.

Sample code

geolocation.openLocation({
    latitude: 31.94830062319531,
    longitude: 118.84177933970965,
    coordType: "gcj02",
    name: "Software Avenue subway station",
    address: "Software Avenue subway station",
    scale: 18,
    success: function () {
        console.log('openLocation success . ');
    },
    fail: function (erromsg, errocode) {
        console.log('geolocation.openLocation----------' + errocode + ': ' + erromsg)
    },
    complete: function () {
        console.log('openLocation complete.');
    }
})

[h2]geolocation.geocodeQuery(OBJECT) (1075+)

Description

Obtains the longitude and latitude of a location. This function is available only in the Chinese mainland.

OBJECT parameters

Parameter

Type

Mandatory

Default Value

Description

city

string

Yes

-

City where a location resides.

address

string

Yes

-

Address.

success

function

No

-

Callback upon success.

fail

function

No

-

Callback upon failure.

complete

function

No

-

Callback upon completion.

Parameters returned by success

Parameter

Type

Description

longitude

number

Longitude in the GCJ-02 coordinate system.

latitude

number

Latitude in the GCJ-02 coordinate system.

Result codes returned by fail

Result Code

Description

200

API exception.

202

Invalid parameter.

205

Unsupported country or region.

Sample code

geolocation.geocodeQuery({
   city: "Nanjing",
   address: "Nanjing University",
   success: function (ret) {
      console.info('### geolocation.geocodeQuery ### ${ret.latitude}: ${ret.longitude}')
  },
   fail: function (errormsg, errorcode) {
     console.info('### geolocation.geocodeQuery ### ${errorcode}: ${errormsg}')
  }
})

[h2]geolocation.reverseGeocodeQuery(OBJECT) (1075+)

Description

Obtains detailed address information including province and city for a location. This function is available only in the Chinese mainland.

OBJECT parameters

Parameter

Type

Mandatory

Default Value

Description

longitude

number

Yes

-

Longitude.

latitude

number

Yes

-

Latitude.

coordType

string

No

wgs84

Coordinate system. The options are wgs84 and gcj02. The default value is wgs84.

includePoiInfo

boolean

No

false

Indicates whether POIs near a specified location are returned by the success callback. The default value is false, indicating that POIs are not returned.

success

function

No

-

Callback upon success.

fail

function

No

-

Callback upon failure.

complete

function

No

-

Callback upon completion.

Parameters returned by success

Parameter

Type

Description

countryName

string

Country/Region name.

province

string

Province name.

city

string

City name.

district

string

District or county name.

street

string

Street name (street level in the administrative division).

address

string

Brief address information.

poiInfoList

array

POIs near a specified location. For details, please refer to poiInfoList parameters.

poiInfoList parameters

Parameter

Type

Description

poiName

string

POI name.

longitude

number

Longitude.

latitude

number

Latitude.

city

string

City.

address

number

Address.

phone

number

Phone number.

Result codes returned by fail

Result Code

Description

200

API exception.

202

Invalid parameter.

205

Unsupported country or region.

Sample code

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}`)
   }
})

Demo

<template>
    <div>
        <div>
            <input type="button" onclick="getDeviceInfo" value="Obtain device information to determine whether the device is a Huawei device." />
            <input type="button" onclick="getGeolocation" value="Obtain a location." />
            <input type="button" onclick="listenGeolocation" value="Listen to the location." />
            <input type="button" onclick="cancelGeolocation" value="Cancel listening to the location." />
            <input type="button" onclick="getLocationType" value="Obtain the supported location types." />
        </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
    },
    // This parameter is defined by Huawei, but not a standard parameter defined by the vendor alliance. You can determine whether your quick app is running on a Huawei device by checking whether the engineProvider parameter returned by device.getInfo() is set to 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 API
    getGeolocation: function () {
        var that = this;         
        geolocation.getLocation({
            coordType:"wgs84", // Call the location capability of the system.
            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 API
    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 API
    cancelGeolocation: function () {
        geolocation.unsubscribe();
    },
    // getLocationType API
    getLocationType: function () {
        geolocation.getLocationType({
            success: function (data) {
                console.log("ret - " + data.types)
            }
        })
    }
}
</script>

Version Change History

Version

Date

Description

1070

2020-05-19

  • Added the openLocation method to view a location in the built-in map of the quick app.
  • Added the chooseLocation method to open the map and select a location.

1040

2019-06-24

Added the accuracy and time parameters to the success callback of the geolocation.getLocation and geolocation.subscribe APIs.

1010

2018-04-20

Added the geolocation.getLocationType API to obtain the location type supported by the system.

Links

[h2]FAQ

Why is 5e-324 returned for both the longitude and latitude when the geolocation.getLocation API is called to use the GCJ-02 coordinates?