I want to make maps with custom markers. In API v2 I can set icon, title, etc for markers. But I want to display title with marker at the first onset. Now title displays only when I taping the marker. In v1 was overlays, but in v2 I didn't found anything similar.
Edited: Maybe I was not clear enough. Something like Marker.showInfoWindow()
in API works only for one marker. I can't show info windows for all of my markers at the same time. Anyway I need to show titles for all of my markers, without waiting while user will tap on it.
For adding a custom marker to Google Maps navigate to the app > res > drawable > Right-Click on it > New > Vector Assets and select the icon which we have to show on your Map. You can change the color according to our requirements. After creating this icon now we will move towards adding this marker to our Map.
Your maps can now match your brand and style across your website and your apps! The Google Maps APIs now support you in creating beautiful styled maps for your Android and iOS apps as well as your website using the same JSON style object.
I have also stumbled upon this problem. V2 API is a step forward, two steps back. Google, please add an overridable 'draw' method on the Marker or GoogleMap classes so we can customize the drawing ourselves.
A possible solution is to generate the bitmap on the fly and attach it to the marker. i.e. Create a canvas, insert the marker bitmap, draw the text next to the marker. This involves some painful calculations (the appropriate canvas size with the marker bitmap and the text next to each other). Unfortunately, there's no setIcon method in Marker, so every time the text changes, a new marker has to be created. It may be fine if you just have a marker on the map, but with dozens of markers, this may not be feasible. Also there may be memory issue on creating those bitmaps dynamically. A sample code (with just the text):
Bitmap.Config conf = Bitmap.Config.ARGB_8888; Bitmap bmp = Bitmap.createBitmap(200, 50, conf); Canvas canvas = new Canvas(bmp); canvas.drawText("TEXT", 0, 50, paint); // paint defines the text color, stroke width, size mMap.addMarker(new MarkerOptions() .position(clickedPosition) //.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker2)) .icon(BitmapDescriptorFactory.fromBitmap(bmp)) .anchor(0.5f, 1) );
Hopefully, Google will add the appropriate methods so we can do this easily. Damn, I really like the new Map rotate feature in V2 API.
Finally did it. So what you do is have a background image (in my case i just use a blue rectangle). Create a marker like so:
Marker myLocMarker = map.addMarker(new MarkerOptions() .position(myLocation) .icon(BitmapDescriptorFactory.fromBitmap(writeTextOnDrawable(R.drawable.bluebox, "your text goes here"))));
Notice the writeTextOnDrawable() Method:
private Bitmap writeTextOnDrawable(int drawableId, String text) { Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId) .copy(Bitmap.Config.ARGB_8888, true); Typeface tf = Typeface.create("Helvetica", Typeface.BOLD); Paint paint = new Paint(); paint.setStyle(Style.FILL); paint.setColor(Color.WHITE); paint.setTypeface(tf); paint.setTextAlign(Align.CENTER); paint.setTextSize(convertToPixels(context, 11)); Rect textRect = new Rect(); paint.getTextBounds(text, 0, text.length(), textRect); Canvas canvas = new Canvas(bm); //If the text is bigger than the canvas , reduce the font size if(textRect.width() >= (canvas.getWidth() - 4)) //the padding on either sides is considered as 4, so as to appropriately fit in the text paint.setTextSize(convertToPixels(context, 7)); //Scaling needs to be used for different dpi's //Calculate the positions int xPos = (canvas.getWidth() / 2) - 2; //-2 is for regulating the x position offset //"- ((paint.descent() + paint.ascent()) / 2)" is the distance from the baseline to the center. int yPos = (int) ((canvas.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2)) ; canvas.drawText(text, xPos, yPos, paint); return bm; } public static int convertToPixels(Context context, int nDP) { final float conversionScale = context.getResources().getDisplayMetrics().density; return (int) ((nDP * conversionScale) + 0.5f) ; }
Thanks to Arun George: Add text to image in android programmatically
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With