0

I have a code which determine my position on google map by a marker when the position is changed the previous marker remains and it repeats in the new position. I want to have only marker when updating the position. Here is my code :

public class MainActivity extends FragmentActivity {
    private GoogleMap map;
    Marker marker;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        map = ((SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map)).getMap();
        map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
        networkLocation();

    }

    private void animateToLocation(double latitude, double longtitude) {
        LatLng latlang = new LatLng(latitude, longtitude);
        CameraPosition position1 = CameraPosition.builder().target(latlang)
                .zoom(15).build();
        map.animateCamera(CameraUpdateFactory.newCameraPosition(position1));
        marker = map.addMarker(new MarkerOptions().title("TEST").position(
                latlang));
        map.getUiSettings().setCompassEnabled(true);
        map.getUiSettings().setZoomControlsEnabled(true);
        map.animateCamera(CameraUpdateFactory.newLatLngZoom(latlang, 100));

    }

    public void networkLocation() {
        G.locationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() {
                    @Override
                    public void onStatusChanged(String provider, int status,
                            Bundle extras) {
                    }

                    @Override
                    public void onProviderEnabled(String provider) {
                    }

                    @Override
                    public void onProviderDisabled(String provider) {
                    }

                    @Override
                    public void onLocationChanged(Location location) {
                        animateToLocation(location.getLatitude(),
                                location.getLongitude());
                    }
                });
    }
}
A.A
  • 1,138
  • 1
  • 16
  • 29

2 Answers2

1

Seems like you are using

marker = map.addMarker(new MarkerOptions().title("TEST").position(
            latlang));

which actually adds a new marker on the map each time animateToLocation(lat, lng) is called. What you need to actually do is call

marker.setPostion(latLng);

there instead and initialize the marker somewhere at the beginning of your activity.

shyam
  • 1,348
  • 4
  • 19
  • 37
  • I use marker.setPostion(latLng); but give me crash . – A.A Aug 04 '14 at 04:43
  • Please give me a link for use marker.setPostion(latLng); and how i add this to map... . I can't add marker.setPostion(latLng); to my map.addMarker(...); – A.A Aug 04 '14 at 04:51
  • Don't add marker.setPosition(latLng) inside map.addMarker(...) instead, replace it. Here is a SO question which explains this: http://stackoverflow.com/questions/15905359/how-to-change-the-position-of-a-marker-on-a-android-map-v2 – shyam Aug 04 '14 at 05:06
1

Insert map.clear(); befor :

marker = map.addMarker(new MarkerOptions().title("TEST").position(latlang));

And your result is :

map.clear();
marker = map.addMarker(new MarkerOptions().title("TEST").position(latlang));

And you can use your code...Correctly. Good look

  • This just clears all the items on the map and adds a new marker right? I wouldn't want to do that if there were other useful markers on the map. But guess this works in this case. – shyam Aug 04 '14 at 12:19