我正在研究基本定位和路线导航(方向)。
我创建了NavigateRoute片段视图之一。
public class RouteNavigateFragmentView {
private MapFragment m_mapFragment = null;
private Activity m_activity;
private Map m_map;
private MapRoute mapRoute;
private double startLangitude, startLatitude, endLatitude = 18.467373, endLangitude = 73.777706;
public RouteNavigateFragmentView(Activity activity, Double startLat, Double startLang) {
m_activity = activity;
/*
* The map fragment is not required for executing search requests. However in this example,
* we will put some markers on the map to visualize the location of the search results.
*/
startLatitude = startLat;
startLangitude = startLang;
initMapFragment();
}
private void initMapFragment() {
/* Locate the mapFragment UI element */
m_mapFragment = (MapFragment) m_activity.getFragmentManager()
.findFragmentById(R.id.mapfragment);
progressBar_cyclic = (ProgressBar) m_activity.findViewById(R.id.progressBar_cyclic);
if (m_mapFragment != null) {
/* Initialize the MapFragment, results will be given via the called back. */
m_mapFragment.init(new OnEngineInitListener() {
@Override
public void onEngineInitializationCompleted(OnEngineInitListener.Error error) {
if (error == Error.NONE) {
m_map = m_mapFragment.getMap();
m_map.setCenter(new GeoCoordinate(18.467518, 73.777694, 0.0),
Map.Animation.NONE);
m_map.setZoomLevel(13.2);
m_map.getPositionIndicator().setVisible(false);
} else {
Toast.makeText(m_activity,
"ERROR: Cannot initialize Map with error " + error,
Toast.LENGTH_LONG).show();
}
}
});
getDirections();
}
}
// Functionality for taps of the "Get Directions" button
public void getDirections() {
if (m_map != null && mapRoute != null) {
m_map.removeMapObject(mapRoute);
mapRoute = null;
}
RouteManager routeManager = new RouteManager();
RoutePlan routePlan = new RoutePlan();
RouteOptions routeOptions = new RouteOptions();
routeOptions.setTransportMode(RouteOptions.TransportMode.CAR);
routeOptions.setRouteType(RouteOptions.Type.FASTEST);
routePlan.setRouteOptions(routeOptions);
routePlan.addWaypoint(new GeoCoordinate(startLatitude, startLangitude));
routePlan.addWaypoint(new GeoCoordinate(endLatitude, endLangitude));
RouteManager.Error error = routeManager.calculateRoute(routePlan, routeManagerListener);
if (error != RouteManager.Error.NONE) {
Toast.makeText(m_activity,
"Route calculation failed with: " + error.toString(), Toast.LENGTH_SHORT)
.show();
}
}
private RouteManager.Listener routeManagerListener = new RouteManager.Listener() {
public void onCalculateRouteFinished(RouteManager.Error errorCode,
List<RouteResult> result) {
if (errorCode == RouteManager.Error.NONE && result.get(0).getRoute() != null) {
// create a map route object and place it on the map
mapRoute = new MapRoute(result.get(0).getRoute());
m_map.addMapObject(mapRoute);
// Get the bounding box containing the route and zoom in (no animation)
GeoBoundingBox gbb = result.get(0).getRoute().getBoundingBox();
m_map.zoomTo(gbb, Map.Animation.NONE, Map.MOVE_PRESERVE_ORIENTATION);
}
}
public void onProgress(int percentage) {
}
};
}
假设我已经两次调用此函数,那么Map将导航两条路线。我也使用了下面的功能,但它不起作用
if (m_map != null && mapRoute != null) {
m_map.removeMapObject(mapRoute);
mapRoute = null;
}
因此,如何清除路线导航或路线管理器。我想一次显示一条路线。
提前谢谢!
答案 0 :(得分:1)
在回调onCalculateRouteFinished
中检查mapRoute
是否不是null
,如果是-删除它。否则,您将失去对它的引用,下一次调用m_map.removeMapObject(mapRoute)
不会删除旧的mapRoute
对象。
因此回调将如下所示:
void onCalculateRouteFinished(Error errorCode, List<RouteResult> result) {
if (errorCode == NONE && !result.isEmpty()) {
if (mapRoute != null) m_map.removeMapObject(mapRoute);
mapRoute = new MapRoute(result.get(0).getRoute());
m_map.addMapObject(mapRoute);
// your code here
}
}