使用场景
将检索出来的餐馆(或其他类型的POI)作为终点,以当前位置作为起点,快速导航到检索地点,适合找餐馆、找酒店的场景。
用到产品
核心类/接口
类接口说明版本
PoiSearch.QueryQuery(java.lang.String query, java.lang.String ctgr, java.lang.String city)构造函数,构造POI搜索查询参数对象。V2.1.0版本起
setCityLimit(boolean isLimit)返回是否严格按照设定城市搜索。V2.8.0版本起
PoiSearchPoiSearch(Context context, PoiSearch.Query query)根据给定的参数构造一个PoiSearch 的新对象。V2.1.0版本起
setOnPoiSearchListener(PoiSearch.OnPoiSearchListener listener)设置查询监听。V2.1.0版本起
searchPOIAsyn()POI搜索异步接口。V2.1.0版本起
PoiSearch.OnPoiSearchListeneronPoiSearched(PoiResult pageResult, int errorCode)返回POI搜索异步处理的结果。V2.1.0版本起
核心类/接口
类接口说明版本
AMapNavistrategyConvert(boolean congestion, boolean avoidspeed, boolean cost, boolean hightspeed, boolean multipleRoute)进行算路策略转换,将传入的特定规则转换成PathPlanningStrategy的枚举值。V1.9.0
calculateDriveRoute(java.util.List from, java.util.List to, java.util.List wayPoints, int strategy)计算驾车路径(包含起点)。V1.0.0
startNavi(int naviType)开始导航。实时导航GPS未开启时,会自动打开GPS定位功能。模拟导航则不需要使用定位功能。V1.0.0
AMapNaviListeneronCalculateRouteSuccess()路径规划的成功的回调函数。V1.0.0
AMapNaviViewListeneronNaviBackClick()导航页面左下角返回按钮的回调接口 false-由SDK主动弹出『退出导航』对话框,true-SDK不主动弹出『退出导航对话框』,由用户自定义。V1.0.0
核心难点
1、基于当前的位置,查询附近的餐馆。
该场景使用单次定位,在定位的回调中记录当前位置的坐标,并以此为中心点,进行POI的周边检索。
@Override
public void onLocationChanged(AMapLocation aMapLocation) {
if (aMapLocation == null || aMapLocation.getErrorCode() != AMapLocation.LOCATION_SUCCESS) {
return;
}
mCurrentLocation = aMapLocation;
initPoiSearch(aMapLocation.getLatitude(), aMapLocation.getLongitude());
}
private void initPoiSearch(double lat, double lon) {
if (mPoiSearch == null) {
PoiSearch.Query poiQuery = new PoiSearch.Query("", "餐饮服务");
LatLonPoint centerPoint = new LatLonPoint(lat, lon);
PoiSearch.SearchBound searchBound = new PoiSearch.SearchBound(centerPoint, 5000);
mPoiSearch = new PoiSearch(this.getApplicationContext(), poiQuery);
mPoiSearch.setBound(searchBound);
mPoiSearch.setOnPoiSearchListener(this);
mPoiSearch.searchPOIAsyn();
}
}
2、当看到POI的详细信息后,确认此处是您想去的地方,快速进行导航,完成一连串的动作。
弹出的POI的infowindow后触发导航,以当前位置为起点,以POI所在的位置为终点,采用默认的驾车策略,进行导航。
a)在检索页面,在弹出的infowindow中开始导航。
//自定义marker点击弹窗内容
@Override
public View getInfoWindow(final Marker marker) {
View view = getLayoutInflater().inflate(R.layout.poikeywordsearch_uri,
null);
TextView title = (TextView) view.findViewById(R.id.title);
title.setText(marker.getTitle());
TextView snippet = (TextView) view.findViewById(R.id.snippet);
int index = mPoiOverlay.getPoiIndex(marker);
float distance = mPoiOverlay.getDistance(index);
String showDistance = Utils.getFriendlyDistance((int) distance);
snippet.setText("距当前位置" + showDistance);
ImageButton button = (ImageButton) view
.findViewById(R.id.start_amap_app);
// 调起导航
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startAMapNavi(marker);
}
});
return view;
}
//点击一键导航按钮跳转到导航页面
private void startAMapNavi(Marker marker) {
if (mCurrentLocation == null) {
return;
}
Intent intent = new Intent(this, RouteNaviActivity.class);
intent.putExtra("gps", false);
intent.putExtra("start", new NaviLatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude()));
intent.putExtra("end", new NaviLatLng(marker.getPosition().latitude, marker.getPosition().longitude));
startActivity(intent);
}
b)在导航页面,初始化AMapNavi对象、算路,开始导航。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_basic_navi);
mTtsManager = TTSController.getInstance(getApplicationContext());
mTtsManager.init();
mAMapNaviView = (AMapNaviView) findViewById(R.id.navi_view);
mAMapNaviView.onCreate(savedInstanceState);
mAMapNaviView.setAMapNaviViewListener(this);
mAMapNavi = AMapNavi.getInstance(getApplicationContext());
mAMapNavi.addAMapNaviListener(this);
mAMapNavi.addAMapNaviListener(mTtsManager);
mAMapNavi.setEmulatorNaviSpeed(60);
getNaviParam();
}
//实现AMapNaviView的生命周期函数
@Override
protected void onResume() {
super.onResume();
mAMapNaviView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mAMapNaviView.onPause();
// 仅仅是停止你当前在说的这句话,一会到新的路口还是会再说的
mTtsManager.stopSpeaking();
//
// 停止导航之后,会触及底层stop,然后就不会再有回调了,但是讯飞当前还是没有说完的半句话还是会说完
// mAMapNavi.stopNavi();
}
@Override
protected void onDestroy() {
super.onDestroy();
mAMapNaviView.onDestroy();
mAMapNavi.stopNavi();
//mAMapNavi.destroy();
mTtsManager.destroy();
}
//获取intent参数并计算路线
private void getNaviParam() {
Intent intent = getIntent();
if (intent == null) {
return;
}
mIsGps = intent.getBooleanExtra("gps", false);
NaviLatLng start = intent.getParcelableExtra("start");
NaviLatLng end = intent.getParcelableExtra("end");
calculateDriveRoute(start, end);
}
//驾车路径规划计算,计算单条路径
private void calculateDriveRoute(NaviLatLng start, NaviLatLng end) {
int strategyFlag = 0;
List startList = new ArrayList();
/**
* 途径点坐标集合
*/
List wayList = new ArrayList();
/**
* 终点坐标集合[建议就一个终点]
*/
List endList = new ArrayList();
try {
strategyFlag = mAMapNavi.strategyConvert(true, false, false, true, false);
} catch (Exception e) {
e.printStackTrace();
}
startList.add(start);
endList.add(end);
mAMapNavi.calculateDriveRoute(startList, endList, wayList, strategyFlag);
}
//路径规划成功后开始导航
@Override
public void onCalculateRouteSuccess() {
if (mIsGps) {
mAMapNavi.startNavi(AMapNavi.GPSNaviMode);
} else {
mAMapNavi.startNavi(AMapNavi.EmulatorNaviMode);
}
}