device-location-geocoding.md 4.4 KB
Newer Older
Z
zengyawen 已提交
1 2 3 4 5
# Geocoding and Reverse Geocoding Capabilities


## When to Use

S
shawn_he 已提交
6
Describing a location using coordinates is accurate, but neither intuitive nor user-friendly. With the geocoding and reverse geocoding capabilities, you will be able to convert geographic description into specific coordinates and vice versa.
Z
zengyawen 已提交
7

S
shawn_he 已提交
8
The geographic description helps users understand a location easily by providing several key attributes, for example, country, administrative region, street, house number, and address.
Z
zengyawen 已提交
9 10 11 12


## Available APIs

S
shawn_he 已提交
13
The following table describes APIs available for mutual conversion between coordinates and geographic description. For details, see [Geolocation Manager](../reference/apis/js-apis-geoLocationManager.md).
Z
zengyawen 已提交
14 15 16 17 18

  **Table1** APIs for geocoding and reverse geocoding

| API | Description | 
| -------- | -------- |
S
shawn_he 已提交
19 20 21 22 23
| isGeocoderAvailable(): boolean; | Obtains the (reverse) geocoding service status.| 
| getAddressesFromLocation(request: ReverseGeoCodeRequest, callback: AsyncCallback<Array<GeoAddress>>): void | Converts coordinates into geographic description through reverse geocoding. This API uses an asynchronous callback to return the result. | 
| getAddressesFromLocation(request: ReverseGeoCodeRequest): Promise<Array<GeoAddress>> | Converts coordinates into geographic description through reverse geocoding. This API uses a promise to return the result. | 
| getAddressesFromLocationName(request: GeoCodeRequest, callback: AsyncCallback<Array<GeoAddress>>): void | Converts geographic description into coordinates through geocoding. This API uses an asynchronous callback to return the result. | 
| getAddressesFromLocationName(request: GeoCodeRequest): Promise<Array<GeoAddress>> | Converts geographic description into coordinates through geocoding. This API uses a promise to return the result. | 
Z
zengyawen 已提交
24 25 26 27


## How to Develop

S
shawn_he 已提交
28 29
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
> 
Z
zengyawen 已提交
30 31
> The **GeoConvert** instance needs to access backend services to obtain information. Therefore, before performing the following steps, ensure that your device is connected to the network.

S
shawn_he 已提交
32
1. Import the **geoLocationManager** module by which you can implement all APIs related to the geocoding and reverse geocoding conversion capabilities.
Z
zengyawen 已提交
33
   
S
shawn_he 已提交
34 35
   ```ts
   import geoLocationManager from '@ohos.geoLocationManager';
Z
zengyawen 已提交
36 37
   ```

L
liu-binjun 已提交
38 39
2. Query whether geocoder service is available.
   - Call **isGeoServiceAvailable** to query whether the geocoder service is available. If the service is available, continue with step 3.
S
shawn_he 已提交
40 41 42 43 44 45 46
      ```ts
      import geoLocationManager from '@ohos.geoLocationManager';
      try {
          var isAvailable = geoLocationManager.isGeocoderAvailable();
      } catch (err) {
          console.error("errCode:" + err.code + ",errMessage:" + err.message);
      }
L
liu-binjun 已提交
47 48 49
      ```

3. Obtain the conversion result.
Z
zengyawen 已提交
50 51
   - Call **getAddressesFromLocation** to convert coordinates into geographical location information.
     
S
shawn_he 已提交
52
      ```ts
Z
zengyawen 已提交
53
      var reverseGeocodeRequest = {"latitude": 31.12, "longitude": 121.11, "maxItems": 1};
S
shawn_he 已提交
54
      geoLocationManager.getAddressesFromLocation(reverseGeocodeRequest, (err, data) => {
L
liu-binjun 已提交
55 56 57 58 59
          if (err) {
              console.log('getAddressesFromLocation err: ' + JSON.stringify(err));
          } else {
              console.log('getAddressesFromLocation data: ' + JSON.stringify(data));
          }
Z
zengyawen 已提交
60 61 62
      });
      ```

S
shawn_he 已提交
63
      Your application can obtain the **GeoAddress** list that matches the specified coordinates and then read location information from it. For details, see [Geolocation](../reference/apis/js-apis-geoLocationManager.md).
Z
zengyawen 已提交
64 65
   - Call **getAddressesFromLocationName** to convert geographic description into coordinates.
     
S
shawn_he 已提交
66
      ```ts
Z
zengyawen 已提交
67
      var geocodeRequest = {"description": "No. xx, xx Road, Pudong District, Shanghai", "maxItems": 1};
S
shawn_he 已提交
68
      geoLocationManager.getAddressesFromLocationName(geocodeRequest, (err, data) => {
L
liu-binjun 已提交
69 70 71 72 73
          if (err) {
              console.log('getAddressesFromLocationName err: ' + JSON.stringify(err));
          } else {
              console.log('getAddressesFromLocationName data: ' + JSON.stringify(data));
          }
Z
zengyawen 已提交
74 75 76
      });
      ```

S
shawn_he 已提交
77
      Your application can obtain the **GeoAddress** list that matches the specified location information and read coordinates from it. For details, see [Geolocation](../reference/apis/js-apis-geoLocationManager.md).
Z
zengyawen 已提交
78 79

      To improve the accuracy of location results, you can set the longitude and latitude ranges in **GeoCodeRequest**.