Distance Matrix
Distance measurement is supported starting from Search SDK version 6.1.0. This feature allows you to calculate distances without making a request to the driving route planning API. Currently, both straight-line distance and driving distance are supported.
Before You Begin
Before using the distance measurement feature, ensure you have:
- Downloaded the Map SDK
- Imported the Search SDK JAR file
Note: Starting from Map SDK V4.1.3, the SDK no longer provides overlays under the com.amap.api.maps.overlay package. These overlays have been open-sourced in the official demo.
Step 1: Initialize the DistanceSearch Object
Create a DistanceSearch instance in your activity or fragment.
distanceSearch = new DistanceSearch(this);Step 2: Set the Data Callback Listener
Register a listener to receive distance measurement results.
distanceSearch.setDistanceSearchListener(this);Step 3: Configure the Search Parameters
Create a DistanceSearch.DistanceQuery object and set the origin points, destination, and measurement type.
LatLonPoint start = new LatLonPoint(39.90403, 116.407525);
LatLonPoint start1 = new LatLonPoint(39.90000, 116.407525);
LatLonPoint start2 = new LatLonPoint(38.540103, 76.978787);
LatLonPoint start3 = new LatLonPoint(10.90000, 116.407525);
LatLonPoint dest = new LatLonPoint(39.90455, 116.407555);
// Set the origin points (multiple origins are supported)
List<LatLonPoint> latLonPoints = new ArrayList<LatLonPoint>();
latLonPoints.add(start);
latLonPoints.add(start1);
latLonPoints.add(start2);
latLonPoints.add(start3);
distanceQuery.setOrigins(latLonPoints);
// Set the destination
distanceQuery.setDestination(dest);
// Set the measurement type: straight-line or driving distance
distanceQuery.setType(DistanceSearch.TYPE_DRIVING_DISTANCE);Step 4: Send the Request
Call the calculateRouteDistanceAsyn() method on the DistanceSearch object to perform the distance calculation.
distanceSearch.calculateRouteDistanceAsyn(distanceQuery);Step 5: Handle the Response
Implement the OnDistanceSearchListener interface and override the onDistanceSearched() callback method to process the distance measurement results.
@Override
public void onDistanceSearched(DistanceResult distanceResult, int errorCode) {
// Parse the result to get distance measurement data
// Refer to the official demo for detailed implementation
}Response Details
- Result parsing: Use distanceResult.getDistanceResults() to obtain a list of DistanceItem objects. Each DistanceItem contains detailed distance measurement information.
- Error codes: A response code of 1000 indicates success. Any other value indicates failure. For a complete list of error codes, refer to the *Error Code Reference* in the Developer Guide.