Geographic Location

icon-note.gif Note:

  • The APIs of this module are no longer maintained since API version 7. It is recommended that you use @ohos.geolocation instead.

  • The initial APIs of this module are supported since API version 3. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Modules to Import

import geolocation from '@system.geolocation';

Required Permissions

ohos.permission.LOCATION

geolocation.getLocation

getLocation(Object): void

Obtains the geographic location.

System capability: SystemCapability.Location.Location.Lite

Parameters

Parameter Type Mandatory Description
timeout number No Timeout duration, in milliseconds. The default value is 30000.
The timeout duration is necessary in case the request to obtain the geographic location is rejected for the lack of the required permission, weak positioning signal, or incorrect location settings. After the timeout duration expires, the fail function will be called.
The value is a 32-digit positive integer. If the value set is less than or equal to 0, the default value will be used.
coordType string No Coordinate system type. Available types can be obtained by getSupportedCoordTypes. The default type is wgs84.
success Function No Called when the operation is successful.
fail Function No Called when the operation fails.
complete Function No Called when the execution is complete

The following values will be returned when the operation is successful.

Parameter Type Description
longitude number Longitude
latitude number Latitude
altitude number Altitude
accuracy number Location accuracy
time number Time when the location is obtained

One of the following error codes will be returned if the operation fails.

Error Code Description
601 Failed to obtain the required permission because the user rejected the request.
602 Permission not declared.
800 Operation times out due to a poor network condition or unavailable GPS.
801 System location disabled.
802 The method is called again while the previous execution result is not returned yet.

Example

export default {    
  getLocation() {        
    geolocation.getLocation({            
      success: function(data) {                
        console.log('success get location data. latitude:' + data.latitude);            
      },            
      fail: function(data, code) {                
        console.log('fail to get location. code:' + code + ', data:' + data);            
      },
    });    
  },
}

geolocation.getLocationType

getLocationType(Object): void

Obtains the supported location types.

System capability: SystemCapability.Location.Location.Lite

Parameters

Parameter Type Mandatory Description
success Function No Called when the execution is successful.
fail Function No Called when the operation fails.
complete Function No Called when the execution is complete

The following values will be returned when the operation is successful.

Parameter Type Description
types Array<string> Available location types, ['gps', 'network']

Example

export default {    
  getLocationType() {        
    geolocation.getLocationType({            
      success: function(data) {                
        console.log('success get location type:' + data.types[0]);            
      },            
      fail: function(data, code) {                
        console.log('fail to get location. code:' + code + ', data:' + data);            
       },        
     });    
  },
}

geolocation.subscribe

subscribe(Object): void

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

System capability: SystemCapability.Location.Location.Lite

Parameters

Parameter Type Mandatory Description
coordType string No Coordinate system type. Available types can be obtained by getSupportedCoordTypes. The default type is wgs84.
success Function Yes Called when the geographical location changes
fail Function No Called when the listening fails

The following values will be returned when the network type is obtained.

Parameter Type Description
longitude number Longitude
latitude number Latitude
altitude number Altitude
accuracy number Location accuracy
time number Time when the location is obtained

One of the following error codes will be returned if the operation fails.

Error Code Description
601 Failed to obtain the required permission because the user rejected the request.
602 Permission not declared.
801 System location disabled.

Example

export default {    
  subscribe() {        
    geolocation.subscribe({            
      success: function(data) {                
        console.log('get location. latitude:' + data.latitude);            
      },            
      fail: function(data, code) {                
        console.log('fail to get location. code:' + code + ', data:' + data);            
      },        
    });    
  },
}

geolocation.unsubscribe

unsubscribe(): void

Cancels listening to the geographical location.

System capability: SystemCapability.Location.Location.Lite

Example

export default {    
  unsubscribe() {        
    geolocation.unsubscribe();    
  },
}

geolocation.getSupportedCoordTypes

getSupportedCoordTypes(): Array<string>

Obtains coordinate system types supported by the device.

System capability: SystemCapability.Location.Location.Lite

Return Value

Type Non-Null Description
Array<string> Yes Coordinate system types, for example, [wgs84, gcj02].

Example

export default {    
  getSupportedCoordTypes() {       
    var types = geolocation.getSupportedCoordTypes();    
  },
}