目录

Distance Calculation

Use Cases

This tool calculates the straight-line distance in meters between two points based on their longitude and latitude coordinates specified by the user.

API Description

The following table describes APIs for distance calculation, which are mainly provided by the calculateDistance method in the map namespace. For more information about the APIs, please refer to API Reference.

API Description
mapCommon.LatLng Longitude-latitude object.
calculateDistance(from: mapCommon.LatLng, to: mapCommon.LatLng): number Calculates the distance between coordinate points.

Development Procedure

  1. Import required modules.

    import { map, mapCommon } from '@kit.MapKit';
  2. Initialize the coordinates to be calculated and call the calculateDistance method to calculate the distance.

    let fromLatLng: mapCommon.LatLng = {
      latitude: 38,
      longitude: 118
    };
    let toLatLng: mapCommon.LatLng = {
      latitude: 39,
      longitude: 119
    };
    // Calculate the distance between coordinate points.
    let distance = map.calculateDistance(fromLatLng, toLatLng);