Document Maps SDK for Android Guides Markers and Overlays Route Matching

Route Matching

Note: For track recording and rectification needs, we strongly recommend using the Hunting SDK or Hunting Service API provided by the AMAP Open Platform. The map SDK will gradually discontinue maintenance of the track rectification interface in future versions.

Overview

Track rectification helps you simplify and correct recorded driving track points, matching them to roads for smooth rendering and calculating driving distance (supported in map SDK V3.4.0 and above). It can also record real driving tracks when combined with AMAP positioning (supported in map SDK V5.1.0 and above).

Important: This feature currently only supports correcting driving tracks to roads.

Track Rectification with Positioning (Supported from V5.2.0)

Step 1: Initialize LBSTraceClient

Start recording the track. The SDK records a point every 2 seconds and sends a batch of 5 points for rectification with each request.

LBSTraceClient lbsTraceClient = LBSTraceClient.getInstance(context);

Step 2: Start Track Rectification

lbsTraceClient.startTrace(TraceListener); // Start collecting data. Pass a status callback listener.

Step 3: Parse the Result

void onTraceStatus(List<TraceLocation> locations, List<LatLng> rectifications, String errorInfo);
// locations: Track points from positioning
// rectifications: Corrected track points
// errorInfo: Error information for track rectification

Step 4: Stop Track Rectification

lbsTraceClient.stopTrace(); // Call this method when track rectification is no longer needed (e.g., trip ends)

Track Rectification with Your Own Data

Step 1: Initialize LBSTraceClient

mTraceClient = new LBSTraceClient(this.getApplicationContext());

Step 2: Construct the Track Point List

Build a list of track points using the TraceLocation format. The following table describes the available methods for setting TraceLocation information.

Important:

- Missing required fields will cause rectification to fail. Missing optional fields may affect the final result. Providing as much information as possible is the best way to ensure a smooth track. We recommend using high-accuracy positioning data with speed and bearing values from the Android Location SDK.

- The longitude and latitude points must be within China. Track rectification does not support coordinates outside China.

Method

Parameter Description

Return Value

Effect

setLongitude(double mLongitude)

mLongitude: Longitude, required.

void

Sets the longitude.

setLatitude(double mLatitude)

mLatitude: Latitude, required.

void

Sets the latitude.

setSpeed(float mSpeed)

mSpeed: Speed, required.

void

Sets the speed.

setBearing(float mBearing)

mBearing: Bearing angle, required.

void

Sets the bearing angle.

setTime(long mTime)

mTime: Time, required.

void

Sets the time.

Step 3: Perform Track Rectification

Track rectification supports multiple coordinate systems (AMAP, GPS raw coordinates, and Baidu) and can process multiple tracks simultaneously.

Method

Parameter Description

Return Value

Effect

queryProcessedTrace

lineID: Identifies a track. Supports multi-track rectification. Different tracks must use different line IDs.

locations: A list of points for one track. Recommended to use high-precision GPS positioning data.

type: Coordinate system. Supported values: LBSTraceClient.TYPE_AMAPLBSTraceClient.TYPE_GPSLBSTraceClient.TYPE_BAID.

listener: Callback for track rectification.   

void

Performs track rectification and returns the corrected track data.

mTraceClient.queryProcessedTrace(mSequenceLineID, mTraceList, mCoordinateType, this);

Step 4: Retrieve the Corrected Data

1. Implement the TraceListener Interface

Pass the TraceListener as a parameter to the track rectification method. Its callbacks provide the corrected track points and total distance.

2. Track Rectification Completion Callback

The onFinished callback is triggered when all track points have been successfully rectified.

Method

Parameter Description

Return Value

Effect

onFinished

lineID: Identifies a track. Supports multi-track rectification. Different tracks must use different line IDs.

linepoints: List of corrected longitude/latitude points for the entire track.

distance: Total distance after rectification, in meters.

waitingtime: Total idle time during the track, based on GPS speed, in seconds.   

void

Called when all input track points have been rectified.

Example:

/**
 * Track rectification completion callback
 */
@Override
public void onFinished(int lineID, List<LatLng> linepoints, int distance, int waitingtime) {
    Log.d(TAG, "onFinished");
    Toast.makeText(this.getApplicationContext(), "onFinished", Toast.LENGTH_SHORT).show();
    if (mOverlayList.containsKey(lineID)) {
        TraceOverlay overlay = mOverlayList.get(lineID);
        overlay.setTraceStatus(TraceOverlay.TRACE_STATUS_FINISH);
        overlay.setDistance(distance);
        overlay.setWaitTime(waitingtime);
        setDistanceWaitInfo(overlay);
    }
}

3. Track Rectification Failure Callback

The onRequestFailed callback is triggered when rectification fails due to parameter errors. Common causes include:

- Network is unavailable.

- The input track contains only one point.

Method

Parameter Description

Return Value

Effect

onRequestFailed

lineID: Identifies a track. Supports multi-track rectification. Different tracks must use different line IDs.

errorInfo: Reason for the rectification failure.   

void

Called when an error occurs during track rectification.

Example:

/**
 * Track rectification failure callback
 */
@Override
public void onRequestFailed(int lineID, String errorInfo) {
    Log.d(TAG, "onRequestFailed");
    Toast.makeText(this.getApplicationContext(), errorInfo, Toast.LENGTH_SHORT).show();
    if (mOverlayList.containsKey(lineID)) {
        TraceOverlay overlay = mOverlayList.get(lineID);
        overlay.setTraceStatus(TraceOverlay.TRACE_STATUS_FAILURE);
        setDistanceWaitInfo(overlay);
    }
}

4. Track Rectification Progress Callback

Track data is processed in segments. The onTraceProcessing callback is triggered after each segment is rectified.

Segment-based processing offers two advantages:

- Reduces waiting time for large amounts of track data.

- Provides partial results even if some segments fail rectification.

Method

Parameter Description

Return Value

Effect

onTraceProcessing

lineID: Identifies a track. Supports multi-track rectification. Different tracks must use different line IDs.

index: Index of the current segment in the track.

segments: List of corrected longitude/latitude points for the current segment.   

void

Called for each segment of a track, in index order.

Example:

/**
 * Track rectification progress callback
 */
@Override
public void onTraceProcessing(int lineID, int index, List<LatLng> segments) {
    Log.d(TAG, "onTraceProcessing");
    if (segments == null) {
        return;
    }
    if (mOverlayList.containsKey(lineID)) {
        TraceOverlay overlay = mOverlayList.get(lineID);
        overlay.setTraceStatus(TraceOverlay.TRACE_STATUS_PROCESSING);
        overlay.add(segments);
    }
}

Important Notes

1. As a feature of the map SDK, a valid AMAP Key must be set to use track rectification correctly.

2. If you have doubts about the rectification results (e.g., inaccurate distance calculation, incorrect track), convert your track points to a standard JSON file and submit it to us via a ticket. Note: We only accept track point data in a format we can verify.

a. Use http://tool.oschina.net/codeformat/json to format the JSON file.

b. Verify that the field names match the table below. If they differ, rename them accordingly.

Field

JSON Key

Longitude

lon

Latitude

lat

Time (milliseconds)

loctime

Speed (km/h)

speed

Bearing (degrees)

bearing

c. Submit the file to us via a ticket.