如何在Android中单击标记时更改标记颜色?

时间:2014-03-11 05:11:14

标签: android google-maps

我正在开发一个应用程序,因为我使用HUE_AZURE颜色显示红色和周围位置的当前位置,现在我尝试用HUE_GREEN(绿色)更改两种颜色,当我点击标记时,我写了一些代码,但它无法正常工作。当我点击标记时,地图会转到起始位置并且只改变颜色一次这是我的问题。

我的代码

  @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;
}

2 个答案:

答案 0 :(得分:1)

/** 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;
}

答案 1 :(得分:0)

使用此方法可以简单地更改标记颜色。

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

要在单击标记时更改颜色,请使用以下方法

 @Override
public boolean onMarkerClick(Marker marker) {
    marker.setIcon(getMarkerIcon(getResources().getColor(R.color.your_color)));
    return true;
}