目录

Geocoding

Use Cases

The forward geocoding and reverse geocoding capabilities are provided.

API Description

The following table describes APIs for geocoding, which are mainly provided by methods in the site namespace. For more information about the APIs, please refer to API Reference.

API Description
geocode(geocodeParams: GeocodeParams): Promise<GeocodeResult> Performs forward geocoding.
geocode(context: common.Context, geocodeParams: GeocodeParams): Promise<GeocodeResult> Performs forward geocoding. The context can be passed.
reverseGeocode(reverseGeocodeParams: ReverseGeocodeParams): Promise<ReverseGeocodeResult> Performs reverse geocoding.
reverseGeocode(context: common.Context, reverseGeocodeParams: ReverseGeocodeParams): Promise<ReverseGeocodeResult> Performs reverse geocoding. The context can be passed.
GeocodeParams Parameters for forward geocoding.
GeocodeResult Forward geocoding result.
ReverseGeocodeParams Parameters for reverse geocoding.
ReverseGeocodeResult Reverse geocoding result.

Development Procedure

Import required modules.

import { site } from '@kit.MapKit';
import { BusinessError } from '@kit.BasicServicesKit';

[h2]Forward Geocoding

This capability obtains spatial coordinates (such as the longitude and latitude) of a place based on its address. A maximum of 10 records can be returned.

let params: site.GeocodeParams = {
  // Address information.
  query: 'Piazzale Dante, 41, 55049 Viareggio',
  language: 'en'
};
try {
  // Call the forward geocoding API to query the address.
  const result = await site.geocode(params);
  console.info(`Succeeded in geocoding. result is ${JSON.stringify(result)}`);
} catch (error) {
  const err: BusinessError = error as BusinessError;
  console.error(`Failed in geocoding. Code is ${err.code}, message is ${err.message}`);
}

[h2]Reverse Geocoding

let params: site.ReverseGeocodeParams = {
  // Longitude and latitude of a place.
  location: {
    latitude: 31.984410259206815,
    longitude: 118.76625379397866
  },
  language: 'en',
  radius: 0,
  isExtension: true,
  isNearbyAoi: true
};
try {
  // Call the reverse geocoding API to query the address based on coordinates.
  const result = await site.reverseGeocode(params);
  console.info(`Succeeded in reversing. result is ${JSON.stringify(result)}`);
} catch (error) {
  const err: BusinessError = error as BusinessError;
  console.error(`Failed in reversing. Code is ${err.code}, message is ${err.message}`);
}