Associate data to a marker
It is possible to store an arbitrary data object with a marker using Marker.setTag()
and recover the data object using Marker.getTag()
, as shown in this example code.
/**
* A demo class that stores and retrieves data objects with each marker.
*/
public class MarkerDemoActivity extends FragmentActivity implements
OnMarkerClickListener,
OnMapReadyCallback {
private static final LatLng PERTH = new LatLng(-31.952854, 115.857342);
private static final LatLng SYDNEY = new LatLng(-33.87365, 151.20689);
private static final LatLng BRISBANE = new LatLng(-27.47093, 153.0235);
private Marker mPerth;
private Marker mSydney;
private Marker mBrisbane;
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.marker_demo);
SupportMapFragment mapFragment =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
/** Chamado quando o mapa está pronto. */
@Override
public void onMapReady(GoogleMap map) {
mMap = map;
// Add some markers to the map, and add a data object to each marker.
mPerth = mMap.addMarker(new MarkerOptions()
.position(PERTH)
.title("Perth");
mPerth.setTag(0);
mSydney = mMap.addMarker(new MarkerOptions()
.position(SYDNEY)
.title("Sydney");
mSydney.setTag(0);
mBrisbane = mMap.addMarker(new MarkerOptions()
.position(BRISBANE)
.title("Brisbane");
mBrisbane.setTag(0);
// Set a listener for marker click.
mMap.setOnMarkerClickListener(this);
}
/** Chamado quando o usuário clica em um marcador. */
@Override
public boolean onMarkerClick(final Marker marker) {
// Retrieve the data from the marker.
Integer clickCount = (Integer) marker.getTag();
// Check if a click count was set, then display the click count.
if (clickCount != null) {
clickCount = clickCount + 1;
marker.setTag(clickCount);
Toast.makeText(this,
marker.getTitle() +
" has been clicked " + clickCount + " times.",
Toast.LENGTH_SHORT).show();
}
// Return false to indicate that we have not consumed the event and that we wish
// for the default behavior to occur (which is for the camera to move such that the
// marker is centered and for the marker's info window to open, if it has one).
return false;
}
}
Here are some examples of scenarios when it is useful to store and recover data with bookmarks:
Your app can offer various types of bookmarks and you want to treat them differently when the user clicks on them. To do this, it is possible to store a String with the marker indicating the type.
You may be interfacing with a system that has unique registry identifiers, where markers represent specific records in this system.
Marker data may indicate a priority to be used when deciding the Z index of a marker.
Customize the color of the marker
You can customize the default marker image color by passing a Bitmapdescriptor object to the icon() method. You can use a set of preset colors in the Bitmapdescriptorfactory object or set a custom marker color with the Bitmapdescriptorfactory.defaultMarker(float Hue) method. The hue is a value between 0 and 360, representing points in a color palette.
static final LatLng MELBOURNE = new LatLng(-37.813, 144.962);
Marker melbourne = mMap.addMarker(new MarkerOptions()
.position(MELBOURNE)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
FOLLOWS OTHER DATA IN THIS LINK: https://developers.google.com/maps/documentation/android-api/marker?hl=pt-br
The color, I don’t think so. But you can set an image to be the marker.
– Renan Gomes
@Renan replace by image I saw. There are some questions about this here. But I don’t want to do the trick of creating an image of a marker with another color. I believe there is a way, I’ve seen applications with blue marker, for example. I just can’t find it now...
– eightShirt
If it refers to the map in the browser...you can change the icon to any other...
– Almeida
Must be this here then.
– Renan Gomes