2

First, I plot a Marker like this:

public void addMarker(String title,String  lat,String Lng,int id,String address,int f)
{    
        marker= mMap.addMarker(new MarkerOptions().snippet(title)
                .title(title+", "+address)
                .position(new LatLng(Double.valueOf(lat), Double.valueOf(Lng)))
                .icon(BitmapDescriptorFactory.fromResource(id)));

        LatLng coordinate = new LatLng(Double.valueOf(lat), Double.valueOf(Lng));
        CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(coordinate, 10);
        mMap.animateCamera(yourLocation);
        mMarkerArray.add(marker);
}

After that I am trying to replace the Marker with another icon when ever I reached at any existing Location

@Override
public void onLocationChanged(Location location)
{
    Log.d("latitude_main", "onlocation???");
    Geocoder geocoder;
    List<Address> addresses;
    geocoder = new Geocoder(this, Locale.getDefault());
    double latitude = location.getLatitude();
    double longitude = location.getLongitude();
    Log.e("latitude_main", "latitude--" + latitude+"longitude="+longitude);

    current_lat= String.valueOf(latitude);
    current_lng= String.valueOf(longitude);
    Log.e("latitude_main","size-=="+salesmanlocationArrayList.size() );

   for(int i=0;i<salesmanlocationArrayList.size();i++)
   {           
       if(salesmanlocation.getLati().equals("12.9165757") && salesmanlocation.getLongi().equals("77.6101163"))
       {           
            mMap.addMarker(new MarkerOptions()
                   .snippet(""+i).title(salesmanlocation.getFirm_name()+", "+salesmanlocation.getAddress())
                   .position(new LatLng(Double.valueOf(salesmanlocation.getLati().toString()), Double.valueOf(salesmanlocation.getLongi().toString())))
                   .icon(BitmapDescriptorFactory.fromResource(R.drawable.event_events_select)));
       }
       mapFragment.getMapAsync(this);
   }
}

I want to remove the marker from the map when the user visits that location.

HenriqueMS
  • 3,864
  • 2
  • 30
  • 39
gautam
  • 522
  • 3
  • 14

1 Answers1

1

You can simply define one OnMyLocationChangeListener class that performs your tasks, and set it on your GoogleMap instance, this way you can use it whenever you want in your application.

Step 1 - define your listener

public class MyMarkerLocationListener implements GoogleMap.OnMyLocationChangeListener {

    List<Marker> markerList;
    int MY_DISTANCE;
    GoogleMap mMap;

    public MyMarkerLocationListener(List<Marker> markerList, int meters, GoogleMap mMap)
    {
        this.markerList = markerList;
        this.MY_DISTANCE = meters;
        this.mMap = mMap;
    }

    @Override
    public void onMyLocationChange(Location location) {

        // your code/logic
        //...

        Location myNewLocation = location;
        Location someMarkerLocation = new Location("some location");

        //for each marker on your list
        //check if you are close to it
        for (Marker m : markerList) {

            LatLng markerPosition = m.getPosition();

            someMarkerLocation.setLatitude(markerPosition.latitude);
            someMarkerLocation.setLongitude(markerPosition.longitude);

            if (myNewLocation.distanceTo(someMarkerLocation) < MY_DISTANCE) {

                //remove marker
                m.remove();

                //or if you still want to use it later
                //m.setVisible(false);

                // add your new marker
                //mMap.addMarker(new MarkerOptions().icon()....);
            }

        }
    }
}

After defining your class you just set the listener on your map on your fragment or activity code =)

Step 2 - instanciate the listener and set it

MyMarkerLocationListener myListener = new MyMarkerLocationListener(mMarkerArray, 100, mMap);
mMap.setOnMyLocationChangeListener(myListener);

UPDATE to answer your question in the comments:

You should initialize mMap before using it, take a look at this piece of code from this Stackoverflow question

    public class MapPane extends Activity implements OnMapReadyCallback {

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

    MapFragment mapFragment = (MapFragment) getFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap map) {
//DO WHATEVER YOU WANT WITH GOOGLEMAP
 map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        map.setMyLocationEnabled(true);
        map.setTrafficEnabled(true);
        map.setIndoorEnabled(true);
        map.setBuildingsEnabled(true);
        map.getUiSettings().setZoomControlsEnabled(true);
    }
}

don't forget your activity should implement the OnMapReadyCallback interface so the onMapReady method is called

you can use the map only after it is ready

Hope this helps!

Community
  • 1
  • 1
HenriqueMS
  • 3,864
  • 2
  • 30
  • 39
  • thank you for giving response but still i am getting null pointer exception – gautam Oct 03 '16 at 07:42
  • @user3881024 have you tracked where the null pointer is comming from? – HenriqueMS Oct 03 '16 at 10:30
  • @user3881024 if I were you I'd go into debug mode, line by line, step by step so I could track it down. Also are you sure you need to call mapFragment.getMapAsync(this); inside that loop? Unless I am missing something you don't need that in this method at all – HenriqueMS Oct 03 '16 at 10:36
  • 1
    mMap.setOnMyLocationChangeListener(myListener); – gautam Oct 03 '16 at 12:16
  • at this line i am getting null poinnter – gautam Oct 03 '16 at 12:16
  • alright then, are you initializing mMap before mMap.setOnMyLocationChangeListener(myListener); ? – HenriqueMS Oct 03 '16 at 12:18
  • @user3881024 have you been able to solve your problem? I believe this answer should be marked as the correct one, let me know if this helped =) – HenriqueMS Oct 17 '16 at 18:12