-1

I need to load only two markers on google map but multiple markers are loaded.

Below is my code:

public class MainActivity extends FragmentActivity {
        GoogleMap map;

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

        // Getting reference to SupportMapFragment of the activity_main
        SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

        // Getting Map for the SupportMapFragment
        map = fm.getMap();

        if (map != null) {

            // Enable MyLocation Button in the Map
            if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != 
                                                    PackageManager.PERMISSION_GRANTED 
                                                    && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != 
                                                    PackageManager.PERMISSION_GRANTED) {
                return;
            }
            map.setMyLocationEnabled(true);

            //It is used to zoom when google first loading on your device.
            map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(23.0310791, 72.5954376), 11));
            map.setMyLocationEnabled(true);
            map.animateCamera(CameraUpdateFactory.zoomTo(11));            
        }

        map.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {

                // Creating a marker
                MarkerOptions markerOptions = new MarkerOptions();

                // Setting the position for the marker
                markerOptions.position(latLng);

                // Placing a marker on the touched position
                map.addMarker(markerOptions);

            }
        });
    }
}

I had implemented google map API to load map and above is what I am trying to load markers. It loads markers as per the provided lat and long but I need to plot only two markers on my google map.

cprakashagr
  • 751
  • 11
  • 28
Mohd Sakib Syed
  • 1,679
  • 1
  • 25
  • 42
  • 1
    you have added marker when anyone clicks on map. so as many times as you will click on it that number of marker will be placed on map. so do you want to delete previous marker when you place a new one ? – Sagar Nayak Mar 15 '16 at 06:42
  • Check this answer if it works for you http://stackoverflow.com/a/35770711/3514144 – Ajay Pandya Mar 15 '16 at 06:47

1 Answers1

1

What I think is, put 2 marker on map and keep on changing there position on every map click Important : set new position to the old marker.

    map.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
        boolean swap;
        Marker firstMarker = null, secondMarker = null;
        @Override
        public void onMapClick(LatLng latLng) {

            if (firstMarker == null) {
                MarkerOptions options = new MarkerOptions();
                options.position(latLng);
                firstMarker = map.addMarker(options);
                return;
            }

            if (secondMarker == null) {
                MarkerOptions options = new MarkerOptions();
                options.position(latLng);
                secondMarker = map.addMarker(options);
                return;
            }
            swap = !swap;
            if (swap) {
                firstMarker.setPosition(latLng);
            } else {
                secondMarker.setPosition(latLng);
            }
        }
    });
Bharatesh
  • 8,943
  • 3
  • 38
  • 67