如何清除或删除此处地图上的路由Android Kotlin
我正在进行基本定位和路线导航(方向) 我已经创建了一个NavigateRoute片段视图如何清除或删除此处地图上的路由Android Kotlin,android,api,kotlin,maps,here-api,Android,Api,Kotlin,Maps,Here Api,我正在进行基本定位和路线导航(方向) 我已经创建了一个NavigateRoute片段视图 public class RouteNavigateFragmentView { private MapFragment m_mapFragment = null; private Activity m_activity; private Map m_map; private MapRoute mapRoute; private double startLangitude, startLatitude,
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) {
}
};
}
那么如何清除路线导航或路线管理器。我想一次显示一条路线
提前谢谢你 在回调
onCalculateRouteFinished
中,检查mapRoute
是否为null
,如果为空,则将其删除。否则您将失去对它的引用,下一次调用m\u 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
}
}
void onCalculateRouteFinished(错误代码,列表结果){
if(errorCode==NONE&&!result.isEmpty()){
如果(mapRoute!=null)m_map.removeMapObject(mapRoute);
mapRoute=新的mapRoute(result.get(0.getRoute());
m_map.addMapObject(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
}
}