Android Google Map没有绘制最后一个折线

时间:2014-06-06 08:14:02

标签: android google-maps polyline

我想实现多边形添加模块,显示当前设备的黄线,历史轨迹的红线。但是当谈到执行时。我发现没有绘制黄色针脚和红色针脚之间的最后一条线。我已经尝试设置subList参数来设置要绘制的点的子列表,但它是在vail中绘制最后一个段。你能告诉我如何绘制折线的最后一段吗?

enter image description here     公共类MainActivity扩展FragmentActivity实现LocationListener {

    final int RQS_GooglePlayServices = 1;

    private GoogleMap myMap;
    Location myLocation;
    LocationManager lm;
    private ArrayList<Checkpoint> cpList = new ArrayList<Checkpoint> ();
    private ArrayList<Marker> thisMarkerList = new ArrayList<Marker> ();
    private List<LatLng> ppoints = new ArrayList<LatLng> ();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SupportMapFragment sFragment =  (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        myMap =  sFragment.getMap();
        myMap.setMyLocationEnabled(true);
        myMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        lm = (LocationManager) getSystemService (LOCATION_SERVICE);
        String provider = lm.getBestProvider(new Criteria(), true);
        Location loc = lm.getLastKnownLocation(provider);
        if(loc!=null)
        {
            onLocationChanged(loc);
        }

        myMap.setOnMapLongClickListener(setting());

    }


    private OnMapLongClickListener setting() {
        // TODO Auto-generated method stub
        return new OnMapLongClickListener(){

            @Override
            public void onMapLongClick(LatLng point) {
                // TODO Auto-generated method stub
                Log.d (point.toString() , "user one long click");

                Checkpoint cp = new Checkpoint("Destination", point, 0);
                cpList.add(cp);
                Checkpoint lastlyAdded = cpList.get(cpList.size() -1 );
                Log.d("size" , String.valueOf(cpList.size()));

                //  myMap.clear();
                for(int i = 0 ; i < cpList.size() ; i ++){
                    Checkpoint cPoint =  cpList.get(i);
                    if(!cPoint.getPoint().equals(lastlyAdded.getPoint())){
                        cPoint.setState(1); 
                        cPoint.setDesp("Checkpoint "+String.valueOf(i+1) );
                    }
                }
                addMarkersToMap();
            }};
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onResume() {

        super.onResume();
        int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());

        if (resultCode == ConnectionResult.SUCCESS) {
            Toast.makeText(getApplicationContext(),
                    "isGooglePlayServicesAvailable SUCCESS", Toast.LENGTH_LONG)
                    .show();
        } else {
            GooglePlayServicesUtil.getErrorDialog(resultCode, this,
                    RQS_GooglePlayServices);
        }
    }

    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        LatLng latlng = new LatLng(location.getLatitude() , location.getLongitude());
        myMap.moveCamera(CameraUpdateFactory.newLatLng(latlng));
        myMap.animateCamera(CameraUpdateFactory.zoomTo(11));
    }


    private void addMarkersToMap() {
        myMap.clear();
        ppoints.clear();
        PolylineOptions polylineOptions = null ;
        LatLng ll; 
        for (int i = 0; i < cpList.size(); i++) {         
            ll = new LatLng(cpList.get(i).getPoint().latitude, cpList.get(i).getPoint().longitude);
            BitmapDescriptor bitmapMarker;
            polylineOptions = new PolylineOptions();
            polylineOptions.color(Color.YELLOW);
            polylineOptions.width(5);
            switch (cpList.get(i).getState()) {
            case 0:
                bitmapMarker = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW);               
                break;
            case 1:
                bitmapMarker = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED);  
                break;
            case 2:
                bitmapMarker = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE);
                break;
            default:
                bitmapMarker = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED);
                break;
            }   
            ppoints.add(ll);
            thisMarkerList.add(myMap.addMarker(new MarkerOptions().position(ll).title(cpList.get(i).getDesp()) 
                    .snippet(String.valueOf(cpList.get(i).getPoint().latitude) + "," + 
                            String.valueOf(cpList.get(i).getPoint().longitude))
                            .icon(bitmapMarker)));

        }
        int length  = ppoints.size();
        if(length >2){
myMap.addPolyline(new PolylineOptions().width(5).color(Color.RED).addAll(ppoints.subList(0, ppoints.size())));
        myMap.addPolyline(new PolylineOptions().width(5).color(Color.YELLOW).addAll(ppoints.subList(ppoints.size()-2, ppoints.size())));
        }else{

            myMap.addPolyline(new PolylineOptions().width(5).color(Color.YELLOW).addAll(ppoints));
        }

    }
}

0 个答案:

没有答案