Lowrider
Lowrider

Reputation: 31

Erased polylines shows on google maps

I need a little bit of help. I'm building a GPS tracking app where the app draws polylines of my movement and when I stop the tracking of my movement and click start again the app would erase all of the previously drawed polylines and start drawing newones. Bud everytime I start the polylinedrawing again the app after a while shows the previously drawed lines (even after they been erased).

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;

import java.util.ArrayList;
import java.util.List;


public class MapsActivity extends AppCompatActivity implements LocationListener {

private GoogleMap mMap;
Button start,stop;
double OLDlatitude = 100;
double OLDlongitude = 100;
boolean draw = false;
int width = 5;
int color = Color.BLUE;
LocationManager locationManager;
Polyline polyline = null;
PolylineOptions polylineOptions;
List<Polyline> mPolylines = new ArrayList<Polyline>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);

    start = (Button) findViewById(R.id.start);
    stop = (Button) findViewById(R.id.stop);
    stop.setEnabled(false);

    start.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startK();
        }});

    stop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            stopK();
        }});


    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);


    mMap=mapFragment.getMap();

    polylineOptions = new PolylineOptions();
    polylineOptions.width(width);
    polylineOptions.color(color);
    polylineOptions.geodesic(true);


    mMap.setMyLocationEnabled(true);


    mMap.getUiSettings().setMyLocationButtonEnabled(false);


    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);



    boolean gps_enabled = false;

    try {
        gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    } catch(Exception ex) {}
    if(!gps_enabled ) {
            final AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Aplikácia potrebuje aktívne GPS na fungovanie. Chcete ho zapnúť ?")
                    .setCancelable(false)
                    .setPositiveButton("Áno", new DialogInterface.OnClickListener() {
                        public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
                            startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                        }
                    })
                    .setNegativeButton("Nie", new DialogInterface.OnClickListener() {
                        public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
                            dialog.cancel();
                            System.exit(0);
                        }
                    });
            final AlertDialog alert = builder.create();
            alert.show();
    }


    Criteria criteria = new Criteria();


    String provider = locationManager.getBestProvider(criteria, true);


    Location location = locationManager.getLastKnownLocation(provider);

    if(location!=null){
        onLocationChanged(location);
    }


    locationManager.requestLocationUpdates(provider, 2000, 0, this);

    Toast.makeText(this, "Na začatie kreslenia polohy kliknite na štart", Toast.LENGTH_LONG).show();

}


@Override
public void onLocationChanged(Location location) {

    double latitude = location.getLatitude();
    double longitude = location.getLongitude();


    LatLng NEWlatLng = new LatLng(latitude, longitude);

    CameraPosition cameraPosition = new CameraPosition.Builder()
            .target(NEWlatLng)      
            .zoom(17)                   
            .bearing(0)                
            .tilt(0)                   
            .build();                   
    mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

    if(draw)
    {
        if(OLDlatitude==100 && OLDlongitude==100)
        {
            OLDlatitude = latitude;
            OLDlongitude = longitude;
        }

        Location loc1 = new Location("");
        loc1.setLatitude(OLDlatitude);
        loc1.setLongitude(OLDlongitude);

        Location loc2 = new Location("");
        loc2.setLatitude(latitude);
        loc2.setLongitude(longitude);

        float distanceInMeters = loc1.distanceTo(loc2);


        if(distanceInMeters >= 1.0 )
        {
            polylineOptions.add(NEWlatLng);
            mPolylines.add(this.mMap.addPolyline(polylineOptions));

            OLDlatitude = latitude;
            OLDlongitude = longitude;
        }
    }
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}

public void startK() {
    Toast.makeText(this, "Kreslenie bolo začaté", Toast.LENGTH_LONG).show();
    draw = true;
    stop.setEnabled(true);
    start.setEnabled(false);
    if(mPolylines.size() != 0)
    {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Na mape je nakreslená trasa chcete túto trasu uložiť ?")
                .setCancelable(false)
                .setPositiveButton("Áno", new DialogInterface.OnClickListener() {
                    public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {

                    }
                })
                .setNegativeButton("Nie", new DialogInterface.OnClickListener() {
                    public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
                        for(Polyline line : mPolylines)
                        {
                            line.remove();
                        }

                        mPolylines.clear();
                        mPolylines = new ArrayList<Polyline>();
                    }
                });
        final AlertDialog alert = builder.create();
        alert.show();
    }
}

public void stopK() {
    Toast.makeText(this, "Kreslenie bolo zastavené", Toast.LENGTH_LONG).show();
    OLDlongitude = 100;
    OLDlatitude = 100;
    draw = false;
    stop.setEnabled(false);
    start.setEnabled(true);
}

@Override
protected void onPause() {
            if (this.isFinishing()){
        draw = false;
        locationManager.removeUpdates(this);
    }
    super.onPause();
}

@Override
protected void onResume() {
    super.onResume();
}

}

Upvotes: 0

Views: 67

Answers (2)

Lowrider
Lowrider

Reputation: 31

Just figured it by my self instead of

mPolylines = new ArrayList<Polyline>();

I've put there

polylineOptions = new PolylineOptions();

not entirely shore but I think polylineOptions keeps track of all added latlang objects even when they are erased from arrayList. If someone have some more complex explanation I would gratefully apprishiate it.

Upvotes: 0

Waza_Be
Waza_Be

Reputation: 39558

You should clear your map:

mMap.clear();

https://developers.google.com/android/reference/com/google/android/gms/maps/GoogleMap.html#public-methods

public final void clear ()

Removes all markers, polylines, polygons, overlays, etc from the map.

Upvotes: 1

Related Questions