Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change marker color when click on marker in android ?

I am developing one application in that I am showing current location with red and surrounding location with HUE_AZURE colors , now I am trying to change both colors with HUE_GREEN(green) when I click on marker, I wrote some code but it does not work properly. When I click on marker the map go to starting position and change color only one time this my problem.

my code

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

    _googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(
            R.id.mapId)).getMap(); 

    LocationManager service = (LocationManager)getSystemService(LOCATION_SERVICE);

    boolean enableGPS = service.isProviderEnabled(LocationManager.GPS_PROVIDER);
    boolean enableWiFi= service.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

    Log.e("GPS",""+enableGPS);


    service = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String provider = service.getBestProvider(criteria, true);


    service.requestLocationUpdates(provider, 0, 0, this);


    if(_googleMap==null){
        Toast.makeText(getApplicationContext(), "Google Map Not Available", 
           Toast.LENGTH_LONG).show();
        }





}
@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub

    //locationManger.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,   
              this);
    //_googleMap.clear();
    ArrayList<HashMap<String, String>> arl = (ArrayList<HashMap<String, String>>) 
            getIntent().getSerializableExtra("arrayList");


    if(location!=null){
         latitude = location.getLatitude();
         langitude = location.getLongitude();
        LatLng latlang = new LatLng(latitude, langitude);

        myPosition = new LatLng(latitude, langitude);

    }
         if(arl.size()!=0){


            for(int j = 0;j<arl.size();j++){


          String lat =arl.get(j).get("lat").toString();
          String lng =arl.get(j).get("lng").toString();
          if ( !lat.trim().equals("") && !lng.trim().equals("") ) 
          {
          double Hlat = Double.parseDouble(lat.trim());
          double Hlong= Double.parseDouble(lng.trim());

           dabaseLocations =new LatLng(Hlat, Hlong);

          getOtherLocation(dabaseLocations);
          getCurrentLocation(myPosition);
          // Show current location with database locations

             }
           }
         }
     else{
             // Show  Current Location Only

            getCurrentLocation(myPosition);
    }
         _googleMap.setOnMarkerClickListener(this);

}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}
private boolean getOtherLocation(LatLng location){
    _googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location,10));
     mCustomerMarker = _googleMap.addMarker(new MarkerOptions()
     .position(location)
     .title("other")
     .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
     .flat(true));

     return true;

}
private boolean getCurrentLocation(LatLng location){
    _googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location,10)); 
    currentMarker=_googleMap.addMarker(new  
           MarkerOptions().position(location).title(TITILE));
    return true;
}

@Override
public boolean onMarkerClick(final Marker arg0) {
    // TODO Auto-generated method stub

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            ShowMapWhenLoginActivity.this);

        // set title
        alertDialogBuilder.setTitle("Favourate Location");

        // set dialog message
        alertDialogBuilder
            .setMessage("Is it Your favourate location")
            .setCancelable(false)
            .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    if(getCurrentLocation(myPosition)){
                    _googleMap.addMarker(new MarkerOptions()
                    .position(myPosition)

                 .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))
                    .flat(true));
                    Toast.makeText(getApplicationContext(),
                              "Marker Clicked: " + a       
                      rg0.getPosition(), Toast.LENGTH_LONG)
                              .show();
                    }
                    else{
                        _googleMap.addMarker(new MarkerOptions()
                        .position(dabaseLocations)

              .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))
                        .flat(true));
                    }


                }
              })
            .setNegativeButton("No",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    // if this button is clicked, just close
                    // the dialog box and do nothing
                    dialog.cancel();
                }
            });
        // create alert dialog
        AlertDialog alertDialog = alertDialogBuilder.create();

        // show it
        alertDialog.show();
        return true;
}
like image 521
Durga Avatar asked Oct 02 '22 08:10

Durga


2 Answers

/** Called when the map is ready. */
@Override
public void onMapReady(GoogleMap map) {
    mMap = map;

    /* Your configuration*/

    // Set a listener for marker click.
    mMap.setOnMarkerClickListener(this);
}

/** Called when the user clicks a marker. */
@Override
public boolean onMarkerClick(final Marker marker) {

    marker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
    return false;
}
like image 148
Alireza Sattari Avatar answered Oct 12 '22 11:10

Alireza Sattari


use this method to simply change the marker color.

 public BitmapDescriptor getMarkerIcon(int color) {
    float[] hsv = new float[3];
    Color.colorToHSV(color, hsv);
    return BitmapDescriptorFactory.defaultMarker(hsv[0]);
}

to change color when marker click, use this method like below

 @Override
public boolean onMarkerClick(Marker marker) {
    marker.setIcon(getMarkerIcon(getResources().getColor(R.color.your_color)));
    return true;
}
like image 30
hardik kava Avatar answered Oct 12 '22 09:10

hardik kava