Map Markers with text in Google Maps Android API v

2020-01-29 05:13发布

问题:

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/

In the new API, I could not find an easy way to do this. In fact, I could not do it at all.

Basicly I want to have TextViews as a Marker on the map with 9Patch drawable as a background of the text. Trulia is still doing it with the new API v2 in their current app. You can check it here

How can I do this?

回答1:

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



回答2:

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



回答3:

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.



回答4:

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 wanted the titles to show only if there is room, potentially hiding if not enough room to display

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:



回答5:

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