I have came up with this post but it is for the deprecated Google Maps API
http://tech.truliablog.com/2012/02/23/custom-map-markers-for-android-google-maps/
If you use Clusters
, a behaviour is slightly different.
Looking at https://medium.com/@yilmazvolkan/custom-map-marker-8b136212d766 and here, I wrote so.
styles.xml:
<!-- Google Maps marker -->
<style name="MarkerText">
<item name="android:textSize">12sp</item>
<item name="android:textColor">#f0f0f0</item>
</style>
marker_bg.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<solid android:color="#3232a0" />
<corners android:radius="18dp" />
</shape>
Fragment:
private var clusterManager: ClusterManager<SomeClusterItem>? = null
private var clusterRenderer: MarkerClusterRenderer<SomeClusterItem>? = null
private var unselectedBitmap: BitmapDescriptor? = null
private var iconGenerator: IconGenerator? = null
private var selectedItem: SomeClusterItem? = null
override fun onMapReady(googleMap: GoogleMap) {
this.googleMap = googleMap
...
unselectedBitmap =
BitmapUtils.bitmapDescriptorFromVector(context!!, R.drawable.unselected_item)
// Create a text marker background.
iconGenerator = IconGenerator(context!!)
iconGenerator!!.setTextAppearance(R.style.MarkerText)
val markerBackground = ContextCompat.getDrawable(context!!, R.drawable.marker_bg)
iconGenerator!!.setBackground(markerBackground)
clusterManager = ClusterManager(context!!, googleMap)
clusterRenderer = MarkerClusterRenderer(context!!, googleMap, clusterManager!!,
unselectedBitmap!!)
clusterManager!!.renderer = clusterRenderer
...
// Create a list of markers.
val boundsBuilder = LatLngBounds.Builder()
...
// Show clusters and markers.
clusterManager!!.cluster()
clusterManager!!.setOnClusterItemClickListener { item ->
// Deselect selected marker.
// See https://stackoverflow.com/a/53829888/2914140.
if (selectedItem != null) {
deselectMarker(selectedItem)
}
selectedItem = item
selectMarker(selectedItem!!, iconGenerator)
// Show info window. See GoogleMap.InfoWindowAdapter.
false
}
clusterManager!!.setOnClusterClickListener {
deselectMarker(selectedItem)
true
}
private fun selectMarker(item: SomeClusterItem,
iconGenerator: IconGenerator) {
// Generate a text marker.
val icon = iconGenerator.makeIcon(item.title)
val bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(icon)
clusterRenderer?.getMarker(item)?.setIcon(bitmapDescriptor)
}
private fun deselectMarker(item: SomeClusterItem?) =
clusterRenderer?.getMarker(item)?.setIcon(unselectedBitmap)
Then you will see these pictures (all items are not selected and one item is selected).
Note that InfoWindow is shown. To customize info window, see https://stackoverflow.com/a/59952706/2914140.
To remove info window, just return true
in an appropriate method (see Hide markers info window in android google maps API v2).
clusterManager!!.setOnClusterItemClickListener { item ->
...
true
}
It can be simply achieved using this useful lib provided by google.
IconGenerator icg = new IconGenerator(this);
icg.setColor(Color.GREEN); // green background
icg.setTextAppearance(R.style.BlackText); // black text
Bitmap bm = icg.makeIcon("200 mi");
In style.xml
<style name="BlackText" parent="TextDefault">
<item name="android:textColor">@color/black</item>
</style>
http://googlemaps.github.io/android-maps-utils/javadoc/
http://googlemaps.github.io/android-maps-utils/
https://github.com/googlemaps/android-maps-utils
you can set up upper links in your project from this link
As far as I know, this is not currently supported by the google maps api v2. What you can do, on the other hand, is dynamically create the bitmap for your marker, and write the value you want to show in it. Alas, there may easily be performance issues if you happen to have plenty of pins.
Canvas canvas = new Canvas(bitmap);
canvas.drawText("Your text", textXOffset, textYOffset, mPictoPaint);
MarkerOptions options = new MarkerOptions().position([…]).icon(BitmapDescriptorFactory.fromBitmap(bitmapResult));
Marker newMarker = map.addMarker(options);
Note that bitmap
needs to be mutable. Also you will have to scale the base image (probably using a 9.patch) to your need.
I know this question is old but I'll post my answer here in case it helps someone.
None of the above solutions were working for me because
I already have custom icons for markers in my app
My app display potentially thousands of different marker titles so creating that many images in memory would be a performance problem
As the marker icons become bigger, the map becomes very quickly unreadable
I found no solution on the internet so I rolled up my sleeves and made this library which solves my problem, hopefully it will help others too:
https://github.com/androidseb/android-google-maps-floating-marker-titles
Here is a preview of how it works:
by referring to Google's sample code
IconGenerator iconFactory = new IconGenerator(this);
iconFactory.setColor(Color.CYAN);
addIcon(mMap, iconFactory, "Custom color", new LatLng(-33.9360, 151.2070));
addIcon() Method here:
private void addIcon(GoogleMap googleMap, IconGenerator iconFactory, CharSequence text, LatLng position) {
MarkerOptions markerOptions = new MarkerOptions().
icon(BitmapDescriptorFactory.fromBitmap(iconFactory.makeIcon(text))).
position(position).
anchor(iconFactory.getAnchorU(), iconFactory.getAnchorV());
googleMap.addMarker(markerOptions);
}
Chris Broadfoot created a utility library that does exactly that.
The library is available here: https://github.com/googlemaps/android-maps-utils
Also see this short video: http://www.youtube.com/watch?v=nb2X9IjjZpM