0

i want to delete a marker that it keeps multiplying because of the location Changed i've tried map.clear() but it also erases the polyline that i drew and marker.remove but it stops showing, i also want to implement another marker that shows me a GPS location and get the distance between those markers, does anyone know how i can do that? Thank you!

This is my code

package com.example.alejandro.integradora2;

import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;

public class Route extends AppCompatActivity implements OnMapReadyCallback {

    public GoogleMap mMap;
    Marker marker;
    @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);
        setUpMapIfNeeded();

    }
//here is the marker that keeps multiplying
    private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return;
            }
            mMap.setMyLocationEnabled(true);

            // Check if we were successful in obtaining the map.
            if (mMap != null) {


                mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {

                    @Override
                    public void onMyLocationChange(Location arg0) {
                        // TODO Auto-generated method stub

                     marker= mMap.addMarker(new MarkerOptions().position(new LatLng(arg0.getLatitude(), arg0.getLongitude())).title("Tu Ubicacion Actual"));

                    }
                });

            }
        }
    }

    public void onMapReady(GoogleMap map) {
        map=mMap;

        // Add a marker in Sydney and move the camera
        LatLng inicio = new LatLng(23.979804, -104.641936);
        LatLng fin = new LatLng(24.024227, -104.664647);
        mMap.addMarker(new MarkerOptions().position(inicio).title("Inicio Ruta").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
        mMap.addMarker(new MarkerOptions().position(fin).title("Fin Ruta"));
        //mMap.animateCamera(CameraUpdateFactory.zoomTo(10));

        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(23.979804, -104.641936), 12));

        PolylineOptions RutaIda = new PolylineOptions()
                .add(new LatLng(23.979804, -104.641936),new LatLng(23.986028, -104.663826),new LatLng(23.986337, -104.664357),new LatLng(23.996928, -104.662372),new LatLng(23.997051, -104.662469),
                        new LatLng(23.998414, -104.662179),new LatLng(24.007054, -104.662335),new LatLng(24.008568, -104.662292),new LatLng(24.015409, -104.663338),new LatLng(24.017710, -104.663729),
                        new LatLng(24.019288, -104.664051),new LatLng(24.023110, -104.663981),new LatLng(24.024296, -104.664153),new LatLng(24.024227, -104.664647)
                        )
                .width(5)
                .color(Color.RED);
        Polyline polyline = mMap.addPolyline(RutaIda);

        PolylineOptions RutaVuelta = new PolylineOptions()
                .add(new LatLng(24.024227, -104.664647),new LatLng(24.023497, -104.669067),new LatLng(24.021361, -104.668826),new LatLng(24.021430, -104.668161),new LatLng(24.019950, -104.667893),
                        new LatLng(24.019666, -104.665200),new LatLng(24.019490, -104.664937),new LatLng(24.019431, -104.664524),new LatLng(24.018319, -104.664030),new LatLng(24.016864, -104.664062),
                        new LatLng(24.015170, -104.663458),new LatLng(24.008048, -104.662445),new LatLng(23.998369, -104.662447),new LatLng(23.993684, -104.663177),new LatLng(23.986396, -104.664572),
                        new LatLng(23.986318, -104.664277),new LatLng(23.986053, -104.663848),new LatLng(23.979804, -104.641936))
                .width(8)
                .color(Color.parseColor("#802E2EFE"));
        //.color(Color.BLUE);





        Polyline polylinevenida = mMap.addPolyline(RutaVuelta);

    }





}

1 Answers1

2

For preventing the current location Marker from being duplicated, just call remove() on the Marker reference if it's not null each time a new Location comes in:

@Override
public void onMyLocationChange(Location arg0) {
        //Add this:
        if (marker != null) {
            marker.remove();
        }

        marker= mMap.addMarker(new MarkerOptions().position(new LatLng(arg0.getLatitude(), arg0.getLongitude())).title("Tu Ubicacion Actual"));

}

Please note though that using the GoogleMap location listener is deprecated, so you should switch to using the FusedLocationProviderAPI, see here for a complete example.

As for getting the distance between two markers, you can use the Location.distanceBetween() method.

If you have two Markers:

Marker marker1;
Marker marker2;

Here is how you can get the distance between them:

LatLng latLng1 = marker1.getPosition();
LatLng latLng2 = marker2.getPosition();

float[] distance = new float[2];

Location.distanceBetween( latLng1.latitude, latLng1.longitude,
        latLng2.latitude, latLng2.longitude, distance);

if( distance[0] < 100 ){
    // do something if distance between
    // is less than 100 meters
}
Community
  • 1
  • 1
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
  • Thank you Daniel I've seen that i should use FusedLocationProviderAPI but i'm new to Android and i don't know how to use it i'm going to check out those links, one more question..with FusedLocationProviderAPI i can also get the distance between 2 locations? one being a GPS tracker? thank you again – Alejandro Gurrola Mar 29 '16 at 01:20
  • Getting the distance between two points is easy, will you be comparing the distance between two Marker objects? – Daniel Nugent Mar 29 '16 at 02:56
  • I'll try to explain you what i'm trying to do: i'm doing an app that shows a polyline and i want to get users location and show the location of a GPS and as the GPS comes closer to the users location, it indicates in the app how far is it one from another and that it shows the GPS going through the polyline...is it too complicated? – Alejandro Gurrola Mar 29 '16 at 05:03
  • @AlejandroGurrola I just updated the answer. Also, you should take a look at this other answer of mine, it sounds like it could help get you further in what you are implementing: http://stackoverflow.com/questions/30431230/do-something-when-im-inside-radius-of-circle – Daniel Nugent Mar 29 '16 at 05:04
  • Thanks a lot!! i will take a look into it Thank you again – Alejandro Gurrola Mar 29 '16 at 05:25