有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何清除谷歌地图上显示的路线?

在我的安卓应用程序中,我通过点击“获取方向”按钮显示路线b/w当前位置和用户在剧院附近选择的路线,但当用户选择另一个剧院时,新路线也会显示,但旧路线未清除。我试过几种方法,但都做不到。我试过用地图覆盖。清除();这是我不想要的全部地图。甚至我也尝试了{mapOverlays.remove(ol1);mapView.postInvalidate();}它不是清关路线,请帮助我:以下是代码:

麦弗雷。爪哇

public class MyOverLay extends Overlay 
{ 
    private GeoPoint gp1; 
    private GeoPoint gp2; 
    private int mode=0; 
    Context mContext;

    public MyOverLay(GeoPoint gp1,GeoPoint gp2,int mode) 
    { 
        this.gp1 = gp1; 
        this.gp2 = gp2; 
        this.mode = mode; 
    }  
    public int getMode() 
    { 
        return mode; 
    } 

    @Override 
    public boolean draw (Canvas canvas, MapView mapView, boolean shadow, long when) 
    { 
        Projection projection = mapView.getProjection(); 
        Log.d("shadow", ""+shadow);
        if (shadow== false) 
        { 
            Paint paint = new Paint(); 
            paint.setAntiAlias(true); 
            Point point = new Point(); 
            projection.toPixels(gp1, point); 
            if(mode==1) // start point
            { 
                    paint.setColor(Color.BLUE); 
            } 
            else if(mode==2) // mode=2,path 
            { 
                paint.setColor(Color.BLUE); 
                Point point2 = new Point(); 
                projection.toPixels(gp2, point2); 
                paint.setStrokeWidth(5); 
                paint.setAlpha(120); 
                canvas.drawLine(point.x, point.y, point2.x,point2.y, paint); 
            } 
            else if(mode==3)   /* mode=3:end */ 
            { 
                /* the last path */ 
                Point point2 = new Point(); 
                projection.toPixels(gp2, point2); 
                paint.setStrokeWidth(5); 
                paint.setAlpha(120); 
                canvas.drawLine(point.x, point.y, point2.x,point2.y, paint); 
                /* end point */
            } 
        } 
        return super.draw(canvas, mapView, shadow, when); 
    } 
}

HelloItemizedOverlay。爪哇

public class HelloItemizedOverlay extends ItemizedOverlay<OverlayItem> {
    WebViewDemo viewDemo;
    GeoPoint curret_point;

    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
    Context mContext;

   //some methods

    public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
        super(boundCenterBottom(defaultMarker));
        mContext = context;
        viewDemo=(WebViewDemo)context;
    }

    //When user taps theater icon this alert box pop ups.which contains 'Get directions' Button
    @Override
    protected boolean onTap(int index) { 
         WebViewDemo.MyOverLayItem item = (WebViewDemo.MyOverLayItem) mOverlays.get(index);
        AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
        viewDemo.desc_lat=item.getPoint().getLatitudeE6();
        viewDemo.desc_lng=item.getPoint().getLongitudeE6();
        dialog.setTitle(item.getTitle());
        dialog.setMessage(item.getSnippet());
        dialog.setPositiveButton("Get directions", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                viewDemo.showDrections();
            }
        });
        dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        dialog.show();
        return true;
    }
}

WebViewDemo地图活动的主要部分:

WebViewDemo。爪哇

public class WebViewDemo extends MapActivity {

public double current_latitude, current_langitude;
Overlay ol1;
MapView mapView;
GeoPoint destGeoPoint;
HttpClient httpClient;
HttpPost postRequest;
HttpResponse response;
BufferedReader reader;
String sResponse, result;
StringBuilder s;
JSONObject jsonObject;
public String lat = null, lng = null;
int desc_lat,desc_lng;

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
.....
mapView = (MapView) findViewById(R.id.mapview);
mapOverlays = mapView.getOverlays();
/**Here i am getting current_latitude, current_langitude using Location Manager
and displaying it on map.and making Web Service request to google and displaying near theaters also.*/
..
}//onCreate end

// *For displaying route b/w current location and User selected Theaters when Get directions Button clicked (in HelloItemizedOverlay class). 

public void showDrections() {

        try {
                      //Here i am trying  to clear previous path. 
            if(ol1!=null){
                mapOverlays.remove(ol1);
                mapView.postInvalidate();
                mapView.invalidate();
            }
            double l1 = desc_lat/1E6;
            double l2 = desc_lng/1E6;
            destGeoPoint = new GeoPoint(desc_lat,desc_lng);
            httpClient = new DefaultHttpClient();
            postRequest = new HttpPost(
                    "http://maps.googleapis.com/maps/api/directions/json?origin="
                            + current_latitude
                            + ","
                            + current_langitude
                            + "&destination="
                            + l1
                            + ","
                            + l2
                            + "&sensor=true&optimize=true&alternatives=false");
            response = httpClient.execute(postRequest);
            reader = new BufferedReader(new InputStreamReader(response
                    .getEntity().getContent(), "UTF-8"));// getting response
            sResponse = null;
            s = new StringBuilder();
            while ((sResponse = reader.readLine()) != null) {
                s = s.append(sResponse);
            }
            result = s.toString().trim();
            jsonObject = new JSONObject(result);

            JSONArray jsonRouteArray = jsonObject.getJSONArray("routes");
            JSONObject overview_poly = jsonRouteArray.getJSONObject(0).getJSONObject("overview_polyline");
            String points = overview_poly.getString("points");
            List<GeoPoint> _geopoints = decodePoly(points);
            GeoPoint gp1;
            GeoPoint gp2;
            gp2 = _geopoints.get(0);
            for (int i = 1; i < _geopoints.size(); i++) // the last one  would be crash
            {
                gp1 = gp2;
                gp2 = _geopoints.get(i);
                ol1 = new MyOverLay(gp1, gp2, 2);
                mapOverlays.add(ol1);
            }
            mapView.postInvalidate();
            dialog.cancel();
        } catch (JSONException je) {
            Toast.makeText(this, "Unable to Show The Route", Toast.LENGTH_LONG).show();
            Log.d("showDirectins() JSON ERROR", ""+je);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
private List<GeoPoint> decodePoly(String encoded) {
//this method is used to decode the route points which are coming from web service and convert them into GeoPoints
}

}

如果需要,请建议我更改代码


共 (3) 个答案

  1. # 1 楼答案

    有可能删除地图中的路线,请在创建路线时将所有重叠引用存储到一个列表中,当您要删除迭代列表时,请从mapview中删除所有重叠引用。我希望它能起作用

  2. # 2 楼答案

    向MyOverLay类添加一个函数。“setPathVisibility(布尔值)”

    将bool存储在名为visibility的私有变量中,在执行任何图形逻辑之前,请检查visibility是否为true

    这将允许您在不丢失所有覆盖数据的情况下打开和关闭管线

  3. # 3 楼答案

    我找到了一种更简单的方法。它可能有点脏,但对我有用

    创建一个整数变量,该变量将在单击按钮时增加。 然后在代码的顶部添加以下代码

    if(iterator>0) {
                    int listSize = mapView.getOverlays().size();
                    Log.d("listSize",Integer.toString(listSize));
                    mapView.getOverlays().remove(listSize-1); 
                    iterator=0;
                }
    

    上面的代码将删除地图上绘制的最后一条路径。不需要列表或类似的东西。希望这有帮助