Add polyline to onMarkerClickListener ()

0

I have a program where I need to add a polyline when clicking on a bookmark and if I click a different bookmark the previous one is deleted and I add the new one.

Until now add the line and delete the previous one and add the new one, the problem is that the polyline points are overwritten adding more than once so the polyline is deformed

Code

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    ArrayList<FormatoPolyline> Poly=new ArrayList<>();
    Polyline line;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        int height = 50;
        int width = 50;
        BitmapDrawable bitmapdraw = (BitmapDrawable) getResources().getDrawable(R.drawable.marker);
        Bitmap b = bitmapdraw.getBitmap();
        Bitmap smallMarker = Bitmap.createScaledBitmap(b, width, height, false);
        marcadores(mMap,smallMarker);

        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(-33.502482,-70.573841), 15f));


        mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                Polylines(mMap,marker.getTitle());
                return false;
            }
        });

    }
 public void Polylines(GoogleMap map, String ruta){
 ArrayList<LatLng>puntos=new ArrayList<>();
        PolylineOptions polylineOptions= new PolylineOptions();

        if(line!=null) {
            line.remove();
        }

        for(int i=0;i<Poly.size();i++){
            if (Poly.get(i).getTitulo().equals(ruta)){
                LatLng punto=Poly.get(i).getPunto();
                puntos.add(punto);
                Log.d("punto",""+String.valueOf(Poly.get(i).getPunto()));
            }
        }
        Log.d("tamaño",String.valueOf(puntos.size()));
        polylineOptions.addAll(puntos).width(8).color(Color.RED);
        line = map.addPolyline(polylineOptions);

            puntos.clear();

    }
    public void marcadores(GoogleMap map, Bitmap smallMarker){
 map.addMarker(new MarkerOptions().position(new LatLng(-33.459932,-70.574753)).title("Dublé Almeyda").snippet("Vereda / Poniente a Oriente / Los Corteses a Exequiel Figueroa ").icon(BitmapDescriptorFactory.fromBitmap(smallMarker)));
        map.addMarker(new MarkerOptions().position(new LatLng(-33.4442263,-70.560658)).title("Echeñique").snippet("Vereda / Bidireccional / Tobalaba a Vicente Pérez Rosales ").icon(BitmapDescriptorFactory.fromBitmap(smallMarker)));
        map.addMarker(new MarkerOptions().position(new LatLng(-33.401914,-70.629014)).title("Einstein / Las Torres").snippet("Vereda / Bidireccional / Antonia Prado a Los Zapadores ").icon(BitmapDescriptorFactory.fromBitmap(smallMarker)));
        map.addMarker(new MarkerOptions().position(new LatLng(-33.6410041,-70.7339984)).title("El Barrancón").snippet("Vereda / Norte a Sur / Camino Calera De Tango a Santa Inés ").icon(BitmapDescriptorFactory.fromBitmap(smallMarker)));
        map.addMarker(new MarkerOptions().position(new LatLng(-33.589223,-70.800941)).title("El Copihue").snippet("Vereda / Bidireccional / Los Nogales a Camino a Lonquén ").icon(BitmapDescriptorFactory.fromBitmap(smallMarker)));
        map.addMarker(new MarkerOptions().position(new LatLng(-33.502482,-70.573841)).title("Los Cerezos").snippet("Los Cerezos").icon(BitmapDescriptorFactory.fromBitmap(smallMarker)));
    }
}

If I execute the method once, the line stays like this:

 06-15 21:56:33.966 14152-14152/com.example.nacho.mapa D/punto: lat/lng: (-33.505714000000005,-70.574659)
    06-15 21:56:33.966 14152-14152/com.example.nacho.mapa D/punto: lat/lng: (-33.505027,-70.574479)
    06-15 21:56:33.966 14152-14152/com.example.nacho.mapa D/punto: lat/lng: (-33.503835,-70.574168)
    06-15 21:56:33.966 14152-14152/com.example.nacho.mapa D/punto: lat/lng: (-33.502827,-70.573889)
    06-15 21:56:33.966 14152-14152/com.example.nacho.mapa D/punto: lat/lng: (-33.502482,-70.573841)
    06-15 21:56:33.966 14152-14152/com.example.nacho.mapa D/punto: lat/lng: (-33.50191,-70.573704)
    06-15 21:56:33.966 14152-14152/com.example.nacho.mapa D/punto: lat/lng: (-33.50087,-70.573441)
    06-15 21:56:33.966 14152-14152/com.example.nacho.mapa D/punto: lat/lng: (-33.50004,-70.573224)
    06-15 21:56:33.966 14152-14152/com.example.nacho.mapa D/tamaño: 8

but if I press the same marker one more time:

 06-15 21:57:48.938 14152-14152/com.example.nacho.mapa D/punto: lat/lng: (-33.505714000000005,-70.574659)
    06-15 21:57:48.938 14152-14152/com.example.nacho.mapa D/punto: lat/lng: (-33.505027,-70.574479)
    06-15 21:57:48.938 14152-14152/com.example.nacho.mapa D/punto: lat/lng: (-33.503835,-70.574168)
    06-15 21:57:48.938 14152-14152/com.example.nacho.mapa D/punto: lat/lng: (-33.502827,-70.573889)
    06-15 21:57:48.938 14152-14152/com.example.nacho.mapa D/punto: lat/lng: (-33.502482,-70.573841)
    06-15 21:57:48.939 14152-14152/com.example.nacho.mapa D/punto: lat/lng: (-33.50191,-70.573704)
    06-15 21:57:48.939 14152-14152/com.example.nacho.mapa D/punto: lat/lng: (-33.50087,-70.573441)
    06-15 21:57:48.939 14152-14152/com.example.nacho.mapa D/punto: lat/lng: (-33.50004,-70.573224)
    06-15 21:57:48.943 14152-14152/com.example.nacho.mapa D/punto: lat/lng: (-33.505714000000005,-70.574659)
    06-15 21:57:48.943 14152-14152/com.example.nacho.mapa D/punto: lat/lng: (-33.505027,-70.574479)
    06-15 21:57:48.943 14152-14152/com.example.nacho.mapa D/punto: lat/lng: (-33.503835,-70.574168)
    06-15 21:57:48.943 14152-14152/com.example.nacho.mapa D/punto: lat/lng: (-33.502827,-70.573889)
    06-15 21:57:48.943 14152-14152/com.example.nacho.mapa D/punto: lat/lng: (-33.502482,-70.573841)
    06-15 21:57:48.943 14152-14152/com.example.nacho.mapa D/punto: lat/lng: (-33.50191,-70.573704)
    06-15 21:57:48.943 14152-14152/com.example.nacho.mapa D/punto: lat/lng: (-33.50087,-70.573441)
    06-15 21:57:48.943 14152-14152/com.example.nacho.mapa D/punto: lat/lng: (-33.50004,-70.573224)
    06-15 21:57:48.944 14152-14152/com.example.nacho.mapa D/tamaño: 16

maintains the points of the first execution and returns them to add, so the polyline

is deformed

Update

I think I found the error:

 if(line!=null) {
                line.remove();
                line=null;
                Log.d("Polyline","no es nula");
            }

            for(int i=0;i<Poly.size();i++){
                if (Poly.get(i).getTitulo().equals(ruta)){
                    LatLng punto=Poly.get(i).getPunto();
                    puntos.add(punto);
                    Log.d("punto",""+String.valueOf(Poly.get(i).getPunto()));
                }
            }

            polylineOptions= new PolylineOptions();
            Log.d("tamaño",String.valueOf(puntos.size()));
            polylineOptions.addAll(puntos).width(8).color(Color.RED);
            line = map.addPolyline(polylineOptions);
            puntos.clear();
    // reviso directamente los puntos del polyline y si existen los intento remover
            if(line.getPoints().isEmpty()){
                Log.d("puntos","vacio");
            }else{
                line.getPoints().clear();
                line.getPoints().removeAll(line.getPoints());
                Log.d("puntos","contiene puntos");
            }

run the code and the logcat shows me the following:

 06-16 22:17:55.690 31525-31525/com.example.nacho.mapa D/punto: lat/lng: (-33.505714000000005,-70.574659)
    06-16 22:17:55.691 31525-31525/com.example.nacho.mapa D/punto: lat/lng: (-33.505027,-70.574479)
    06-16 22:17:55.691 31525-31525/com.example.nacho.mapa D/punto: lat/lng: (-33.503835,-70.574168)
    06-16 22:17:55.691 31525-31525/com.example.nacho.mapa D/punto: lat/lng: (-33.502827,-70.573889)
    06-16 22:17:55.691 31525-31525/com.example.nacho.mapa D/punto: lat/lng: (-33.502482,-70.573841)
    06-16 22:17:55.691 31525-31525/com.example.nacho.mapa D/punto: lat/lng: (-33.50191,-70.573704)
    06-16 22:17:55.692 31525-31525/com.example.nacho.mapa D/punto: lat/lng: (-33.50087,-70.573441)
    06-16 22:17:55.692 31525-31525/com.example.nacho.mapa D/punto: lat/lng: (-33.50004,-70.573224)
    06-16 22:17:55.692 31525-31525/com.example.nacho.mapa D/tamaño: 8
    06-16 22:17:55.706 31525-31525/com.example.nacho.mapa D/puntos: contiene puntos

therefore the problem is not the fix but the PolylineOptions is not cleaned in each execution, I need to see find the way that polylineoptions becomes null at the beginning of the execution of the method, probe with:

  

polylineOptions = null;

but it did not work.

    
asked by zhet 16.06.2017 в 03:56
source

1 answer

1

I have seen what you asked but you closed the question, in this case I suggest you define the ArrayList puntos at the class level:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    ArrayList<FormatoPolyline> Poly=new ArrayList<>();
    Polyline line;

   ArrayList<LatLng> puntos = new ArrayList<>(); //***
...
...

and comment on the one you generate inside Polylines ():

public void Polylines(GoogleMap map, String ruta){
  //ArrayList<LatLng>puntos=new ArrayList<>();
        PolylineOptions polylineOptions= new PolylineOptions();

        if(line!=null) {
            line.remove();
        }

        for(int i=0;i<Poly.size();i++){
            if (Poly.get(i).getTitulo().equals(ruta)){
                LatLng punto=Poly.get(i).getPunto();
                puntos.add(punto);
                Log.d("punto",""+String.valueOf(Poly.get(i).getPunto()));
            }
        }
        Log.d("tamaño",String.valueOf(puntos.size()));
        polylineOptions.addAll(puntos).width(8).color(Color.RED);
        line = map.addPolyline(polylineOptions);

            puntos.clear();

    }
    
answered by 16.06.2017 в 17:39