從搜索功能6.1.0 版本開始支持測距功能。使用此方法完成距離計算,無需請求行車路線規(guī)劃界面。目前支持直線距離和行駛距離的測量。
第一步,初始化對象
distanceSearch = new DistanceSearch(this);
第二步,設(shè)置數(shù)據(jù)回調(diào)監(jiān)聽
distanceSearch.setDistanceSearchListener(this);
第三步,設(shè)置搜索參數(shù)
通過新的。()
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);
//設(shè)置起點和終點,其中起點支持多個
List latLonPoints = new ArrayList();
latLonPoints.add(start);
latLonPoints.add(start1);
latLonPoints.add(start2);
latLonPoints.add(start3);
distanceQuery.setOrigins(latLonPoints);
distanceQuery.setDestination(dest);
//設(shè)置測量方式,支持直線和駕車
distanceQuery.setType(DistanceSearch.TYPE_DRIVING_DISTANCE);
第四步,發(fā)送請求
使用類的 (.query) 方法進行距離測量計算。
第五步測試距離軟件下載,接收數(shù)據(jù)
在 .ener 接口回調(diào)方法( , int ) 處理距離測量結(jié)果。在返回的信息中,您可以獲得估計的直線或行駛路線距離。
闡明:
1)可以在回調(diào)中解析,測距結(jié)果。
2).() 可以得到一個列表。有關(guān)距離測量結(jié)果的詳細信息,請參閱課程。
3)返回成功或失敗的響應(yīng)碼。1000為成功,其他為失敗(詳見網(wǎng)站開發(fā)指南-實用工具-錯誤碼對照表)
@Override
public void onDistanceSearched(DistanceResult distanceResult, int errorCode) {
//解析result獲取距離測量結(jié)果,可參考官方demo
防范措施:
1、使用上述功能需要下載Map SDK測試距離軟件下載,導(dǎo)入搜索功能的jar包。
2、Map SDK V4.1.從版本3開始,SDK不再提供com.amap.api.maps。,已在官方demo中開源。