harmony 鸿蒙@system.geolocation (Geolocation)

2022-08-09 浏览 (926)

@system.geolocation (Geolocation)

The geolocation module provides only basic functions such as GNSS positioning and network positioning.

NOTE

  • 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.
  • The APIs provided by this module are no longer maintained since API version 9. You are advised to use geoLocationManager instead.

Modules to Import

import geolocation from '@system.geolocation';

Required Permissions

ohos.permission.LOCATION

geolocation.getLocation(deprecated)

getLocation(options?: GetLocationOption): void

Obtains the geographic location.

NOTE This API is deprecated since API version 9. You are advised to use geoLocationManager.getCurrentLocation.

Required permissions: ohos.permission.LOCATION

System capability: SystemCapability.Location.Location.Lite

Parameters

NameTypeMandatoryDescription
optionsGetLocationOptionNoOptions of a single location request.

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(deprecated)

getLocationType(options?: GetLocationTypeOption): void

Obtains the supported location types.

NOTE This API is deprecated since API version 9. The location subsystem supports only two location types: GNSS positioning and network positioning. No APIs will be provided to query the supported location types.

System capability: SystemCapability.Location.Location.Lite

Parameters

NameTypeMandatoryDescription
optionsGetLocationTypeOptionNoCallback used to return the result.

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(deprecated)

subscribe(options: SubscribeLocationOption): void

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

NOTE This API is deprecated since API version 9. You are advised to use geoLocationManager.on('locationChange').

Required permissions: ohos.permission.LOCATION

System capability: SystemCapability.Location.Location.Lite

Parameters

NameTypeMandatoryDescription
optionsSubscribeLocationOptionYesOptions for continuous location.

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(deprecated)

unsubscribe(): void

Cancels listening to the geographic location.

NOTE This API is deprecated since API version 9. You are advised to use geoLocationManager.off('locationChange').

Required permissions: ohos.permission.LOCATION

System capability: SystemCapability.Location.Location.Lite

Example

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

geolocation.getSupportedCoordTypes(deprecated)

getSupportedCoordTypes(): Array<string>

Obtains coordinate system types supported by the device.

NOTE This API is deprecated since API version 9. The location subsystem supports only the wgs84 coordinate system. No APIs will be provided to query the supported coordinate system types.

System capability: SystemCapability.Location.Location.Lite

Return value

TypeNot emptyDescription
Array<string>YesCoordinate system types, for example, [wgs84, gcj02].

Example

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

GetLocationOption(deprecated)

Defines the options of a single location request.

NOTE This API is deprecated since API version 9. You are advised to use geoLocationManager.CurrentLocationRequest.

Required permissions: ohos.permission.LOCATION

System capability: SystemCapability.Location.Location.Lite

NameTypeMandatoryDescription
timeoutnumberNoTimeout duration, in ms. 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 specified value is less than or equal to 0, the default value will be used.
coordTypestringNoCoordinate system type. Available types can be obtained by getSupportedCoordTypes. The default type is wgs84.
success(data: GeolocationResponse) => voidNoCalled when API call is successful.
fail(data: string, code: number) => voidNoCalled when API call has failed. data indicates the error information, and code indicates the error code.
complete() => voidNoCalled when API call is complete.

Return value of fail()

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

GeolocationResponse(deprecated)

Defines the location information, including the longitude, latitude, and location precision.

NOTE This API is deprecated since API version 9. You are advised to use geoLocationManager.Location.

System capability: SystemCapability.Location.Location.Lite

NameTypeReadableWritableDescription
longitudenumberYesNoLongitude.
latitudenumberYesNoLatitude.
altitudenumberYesNoAltitude.
accuracynumberYesNoLocation accuracy.
timenumberYesNoTime when the location is obtained.

GetLocationTypeOption(deprecated)

Defines the location type option, which holds the callback function used to return the query result.

NOTE This API is deprecated since API version 9.

System capability: SystemCapability.Location.Location.Lite

NameTypeMandatoryDescription
success(data: GetLocationTypeResponse) => voidNoCalled when API call is successful.
fail(data: string, code: number) => voidNoCalled when API call has failed.
complete() => voidNoCalled when API call is complete.

GetLocationTypeResponse(deprecated)

Defines the list of location types supported by the current device

NOTE This API is deprecated since API version 9.

System capability: SystemCapability.Location.Location.Lite

NameTypeReadableWritableDescription
typesArray<string>YesNoAvailable location types, ['gps', 'network']

SubscribeLocationOption(deprecated)

Defines the options for continuous location.

NOTE This API is deprecated since API version 9. You are advised to use geoLocationManager.CurrentLocationRequest.

Required permissions: ohos.permission.LOCATION

System capability: SystemCapability.Location.Location.Lite

NameTypeMandatoryDescription
coordTypestringNoCoordinate system type. Available types can be obtained by getSupportedCoordTypes. The default type is wgs84.
success(data: GeolocationResponse) => voidYesCalled when the geographic location changes.
fail(data: string, code: number) => voidNoCalled when API call has failed.

Return value of fail()

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

你可能感兴趣的鸿蒙文章

harmony 鸿蒙APIs

harmony 鸿蒙System Common Events (To Be Deprecated Soon)

harmony 鸿蒙System Common Events

harmony 鸿蒙API Reference Document Description

harmony 鸿蒙Enterprise Device Management Overview (for System Applications Only)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager)

harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)

harmony 鸿蒙@ohos.bundle (Bundle)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/1158bef2673e4a63b648748f8a3e9e56