Hassan Ali Mughal
Hassan Ali Mughal

Reputation: 85

Change Markers icon onMap click

Hello I'm making the google maps app, in my app in add the features of area measurement, in area measurement the user click on the map and the marker are set on pointing areas if user point the seven places then the seven markers are set on pointing places but I change the marker icons on every point but the icon are never change please help me to change the marker icons on every click

 My code is

     public void onMapReady(final GoogleMap googleMap) {
            mMap = googleMap;
            marker = BitmapDescriptorFactory.fromResource(R.drawable.marker);
            pointaa = BitmapDescriptorFactory.fromResource(R.drawable.pointaa);
            pointb = BitmapDescriptorFactory.fromResource(R.drawable.pointb);

            changeView(getSharedPreferences("settings", Context.MODE_PRIVATE)
                    .getInt("mapView", GoogleMap.MAP_TYPE_NORMAL));

            mMap.setOnMarkerClickListener(new OnMarkerClickListener() {
                @Override
                public boolean onMarkerClick(final Marker clickcount) {

                    marker = BitmapDescriptorFactory.fromResource(R.drawable.marker);
                    pointaa = BitmapDescriptorFactory.fromResource(R.drawable.pointaa);
                    pointb = BitmapDescriptorFactory.fromResource(R.drawable.pointb);
                    addPoint(clickcount.getPosition());

                    return true;
                }
            });



            mMap.setOnMapClickListener(new OnMapClickListener() {
                @Override
                public void onMapClick(final LatLng center) {
                    clickcounter=clickcounter+1;
    if (clickcounter==1){
        if (marker!=null){
            Toast.makeText(getApplicationContext(),"1",Toast.LENGTH_SHORT).show();
            marker = BitmapDescriptorFactory.fromResource(R.drawable.marker);

        }



    }if (clickcounter==2){
                        Toast.makeText(getApplicationContext(),"2",Toast.LENGTH_SHORT).show();
                        pointaa = BitmapDescriptorFactory.fromResource(R.drawable.pointaa);
                        addPoint(center);


                    }if (clickcounter==3){
                        Toast.makeText(getApplicationContext(),"3",Toast.LENGTH_SHORT).show();
                        pointb = BitmapDescriptorFactory.fromResource(R.drawable.pointb);
                        addPoint(center);

                    }

                }
            });


      private Marker drawMarker(final LatLng center) {
            return mMap.addMarker(
                    new MarkerOptions().position(center).flat(true).anchor(0.5f, 0.5f).icon(pointb).icon(pointaa).icon(marker));

        }

Upvotes: 2

Views: 128

Answers (2)

Mayur Rajvir
Mayur Rajvir

Reputation: 140

Use Below code

  mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {

                marker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));

                return true;
            }
        });

Upvotes: 2

Thida Swe Zin
Thida Swe Zin

Reputation: 309

You can change the marker icon as below.

Marker marker = new MarkerOptions().position(center).flat(true).anchor(0.5f, 0.5f);
BitmapDescriptor descriptor = BitmapDescriptorFactory.fromResource(R.drawable.marker); 
   // Changing marker icon
marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.my_marker_icon)));

Upvotes: 0

Related Questions