首页 文章

在Google Android Maps API v2中清除 Map 中的折线

提问于
浏览
0

我必须在 Map 中绘制一条路线,并且我必须以两种模式绘制路线,步行或乘车(但不能同时) . 这就是我的方式:

首先,我有一个getUrl(LatLng源,LatLng命运,String routetype)方法:

public String getUrl(LatLng src, LatLng dest, String tipo) {

    Log.i("Location", "Entramos al método getUrl");
    StringBuilder urlString = new StringBuilder();
    urlString.append("http://maps.googleapis.com/maps/api/directions/json");
    urlString.append("?origin=");// from
    urlString.append(Double.toString(src.latitude));
    urlString.append(",");
    urlString.append(Double.toString(src.longitude));
    urlString.append("&destination=");// to
    urlString.append(Double.toString(dest.latitude));
    urlString.append(",");
    urlString.append(Double.toString(dest.longitude));
    urlString.append("&sensor=false&mode=");
    if (tipo != null) {
        urlString.append(tipo);
    }
    return urlString.toString();
}

它返回谷歌的答案,采用JSON格式,随时可以解码,我使用这种方法:

private ArrayList<LatLng> decodePoly(String encoded) {

    ArrayList<LatLng> poly = new ArrayList<LatLng>();
    int index = 0, len = encoded.length();
    int lat = 0, lng = 0;

    while (index < len) {
        int b, shift = 0, result = 0;
        do {
            b = encoded.charAt(index++) - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);
        int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
        lat += dlat;

        shift = 0;
        result = 0;
        do {
            b = encoded.charAt(index++) - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);
        int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
        lng += dlng;

        LatLng p = new LatLng((((double) lat / 1E5)),
                    (((double) lng / 1E5)));
        poly.add(p);
    }

    return poly;
}

这将返回一个LatLng点数组,我用它来绘制路径,使用此方法:

public void continuar(LatLng src){

    mapa.clear();
    latto = 40.416667;
    lonto = -3.70355;
    destino=new LatLng(latto, lonto);
    try {

        StringBuilder builder = new StringBuilder();
        HttpClient client = new DefaultHttpClient();

        HttpGet httpGet = new HttpGet(getUrl(src, destino, tiporuta));
        try {

            HttpResponse response = client.execute(httpGet);

            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();

            if (statusCode == 200) {

                HttpEntity entity = response.getEntity();

                InputStream content = entity.getContent();

                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(content));

                String line;

                while ((line = reader.readLine()) != null) {
                    builder.append(line);
                }

                resp = new JSONObject(builder.toString());

                JSONArray routeObject = resp.getJSONArray("routes");
                JSONObject routes = routeObject.getJSONObject(0);

                JSONObject overviewPolylines = routes
                        .getJSONObject("overview_polyline");
                String encodedString = overviewPolylines.getString("points");
                ArrayList<LatLng> puntos=decodePoly(encodedString);                 

                for(int i=0;i<puntos.size();i++){
                    ruta.add(new LatLng(puntos.get(i).latitude, puntos.get(i).longitude));

                }       

                ruta.color(Color.RED).width(7);
                mapa.clear();


                Polyline polygon=mapa.addPolyline(ruta);                    


            } else {
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}

“ruta”在onCreate()方法中实例化,这样:

ruta=new PolylineOptions();

第一次一切正常 . 但是,有一些ImageViews可以在步行路线和行车路线之间进行切换 . 这些是听众:

ImageView rutacoche=(ImageView) findViewById(R.id.botonrutacoche);
    rutacoche.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {


            tiporuta="driving";
            mapa.clear();//also tried doing polygon.remove()
            if(origen!=null){
                continuar(origen);
            }else{
                continuar(new LatLng(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude()));
            }

        }
    });

    ImageView rutawalk=(ImageView) findViewById(R.id.botonrutawalk);
    rutawalk.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {


            tiporuta="walking";
            mapa.clear();//also tried doing polygon.remove()
            if(origen!=null){
                continuar(origen);
            }else{
                continuar(new LatLng(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude()));
            }

        }
    });

如你所见,我第一次做的是mapa.clear(); mapa以这种方式实现:

GoogleMap mapa = ((SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map)).getMap();

因此,如果文档是正确的,它应该删除所有折线,标记等,但是,当我单击图像以交换路由类型时,不会删除以前的路径 . 并且,通过选择步行路线,它在建筑物,河流等上划出一条直线 . 我的问题是,为什么mapa.clear()呼叫不起作用?并且,为什么路线第一次完美显示,但第二次和后续时间被错误地绘制?

谢谢 .

(PD:我不确定我已经解释了一切正常,这可能有点令人困惑,如果你需要澄清请问) .

编辑:

只是为了说明我所说的,这是第一次绘制时的折线:Polyline ok这是下一次绘制的:Polyline not ok

1 回答

  • 0

    我终于放弃了这个实现,在StackOverflow上搜索这个方法找到了this方式来绘制一条路线,它完美无缺 .

相关问题