Map Markers with text in Google Maps Android API v2
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
Multiple markers with text on Android Google Maps API v2
to do that you'll have to customize the icon of each individual marker to be what you need.
Here's the link: https://developers.google.com/maps/documentation/android/marker#customize_a_marker
Then you can put some PNG resources (a blue, a yellow and a red), and at runtime get correct Bitmap, write the text on the bitmap by code, and then set as the custom-marker with the fromBitmap (Bitmap image)
method.
android Maps API v2 with custom markers
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.
Write text in Google Map v2 custom marker - Android
For custom marker:
- You can create a
custom image
anddraw
anindex number
in top of
thatimage
- Check this example for custom marker
For Marker Window
:
You can create a custom xml
file that contains ImageView
and TextViews
:
Something like customMapView.xml
:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/white_full"
android:layout_width="match_parent"
android:padding="@dimen/padding_micro"
android:layout_height="match_parent">
<ImageView
android:id="@+id/img"
android:layout_gravity="left"
android:src="@drawable/ic_launcher"
android:textColor="@color/black_full"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:gravity="right"
android:id="@+id/name"
android:text="test"
android:textColor="@color/black_full"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- Other TextViews or Whatever you want-->
</RelativeLayout>
After you initialize Markers
you call setInfoWindowAdapter
and inflate your custom view
. Something like:
private void customizeMarkerInfoWindow(){
//Setting custom info window
mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
//Use default infoWindow frame
@Override
public View getInfoWindow(Marker marker) {
return null;
}
@Override
public View getInfoContents(Marker marker) {
// Getting view from the layout file
View view = mActivity.getLayoutInflater().inflate(R.layout.customMapView, null);
// Getting the position from the marker
LatLng latLng = marker.getPosition();
//UI elements
ImageView img = (TextView) view.findViewById(R.id.img);
TextView coord = (TextView) view.findViewById(R.id.name);
mukAdTv.setText(mukAd);
//You set the image here depending on how you want to set it, if you are downloading from internet you can use PICASSO for it and set it to img
//With picasso
Picasso.with(mActivity).load(YOUR_IMAGE_URL).into(img);
//From drawables
//img.setBackground(yourDrawable);
name.setText(latLng.latitude + ", " + latLng.longitude);
return view;
}
});
}
how to show text next to marker in google map v2 for android
If you wanna show the info window without requiring user tap then
Marker.showInfoWindow()
will work if Marker.visible
is set to true.
Otherwise there is a good library here with an example here
This may be closer to what you are looking for.
Reference
Creating a custom marker menu in google maps api V2
You can customize the pop up with an Adapter, just call the setInfoWindowAdapter method and override the following methods, like this:
mGoogleMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker arg0) {
return null;
}
@Override
public View getInfoContents(Marker marker) {
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.item_marker_location, null);
TextView tvTitle = (TextView) rowView.findViewById(R.id.tvMarkerTitle);
TextView tvSnippet = (TextView) rowView.findViewById(R.id.tvMarkerSnippet);
tvTitle.setText(marker.getTitle());
tvSnippet.setText(marker.getSnippet());
return rowView;
}
});
You can read the documentation of the InfoWindowAdapter interface.
How to add letters to the google map standard marker
This method takes a drawable from your resources, draws some text on top of it(inside the marker) and returns the new drawable. All you need to do is give it the resource id of your bubble, and the text you want on top. Then you can pass the returned drawable wherever you want it.
public BitmapDrawable writeOnDrawable(int drawableId, String text){
Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId).copy(Bitmap.Config.ARGB_8888, true);
Paint paint = new Paint();
paint.setStyle(Style.FILL);
paint.setColor(Color.BLACK);
paint.setTextSize(20);
Canvas canvas = new Canvas(bm);
canvas.drawText(text, 0, bm.getHeight()/2, paint);
return new BitmapDrawable(bm);
}
Note:
To preserver density you need this constructor
BitmapDrawable (Resources res, Bitmap bitmap)
So, keeping your context, last return should be something like
return new BitmapDrawable(context.getResources(), bm);
This prevent an undesired resized drawable.
Google Maps API v2: How to make markers clickable?
All markers in Google Android Maps Api v2 are clickable. You don't need to set any additional properties to your marker.
What you need to do - is to register marker click callback to your googleMap and handle click within callback:
public class MarkerDemoActivity extends android.support.v4.app.FragmentActivity
implements OnMarkerClickListener
{
private Marker myMarker;
private void setUpMap()
{
.......
googleMap.setOnMarkerClickListener(this);
myMarker = googleMap.addMarker(new MarkerOptions()
.position(latLng)
.title("My Spot")
.snippet("This is my spot!")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
......
}
@Override
public boolean onMarkerClick(final Marker marker) {
if (marker.equals(myMarker))
{
//handle click here
}
}
}
here is a good guide on google about marker customization
Best way to add marker with name on Google Map v2
This is what i am expected:
// To show the map with existing user created markers
private void addExistingMarkers(int color) {
userMarkers.clear();// remove all existing markers
/**
* Placing Marker
* */
List<MarkingInfo> markingInfos = SQLLiteManager.getInstance(getApplicationContext())
.getAllAppMarkingInfos();
for (MarkingInfo markingInfo : markingInfos) {
// Set title on marker
//Bitmap drawBmp = writeTextOnDrawable(markingInfo, color);
Bitmap drawBmp = drawTitleOnMarkerIcon(markingInfo, color);
MarkerOptions markerOptions = new MarkerOptions()
.position(new LatLng(markingInfo.getLattitude(), markingInfo.getLongtitutde()))
.title(markingInfo.getName())
.icon(BitmapDescriptorFactory.fromBitmap(drawBmp))
.icon(BitmapDescriptorFactory.fromBitmap(drawBmp))
// .anchor(0.5f, 1)
.draggable(true)
.visible(false);
if (googleMap.getCameraPosition().zoom >= markingInfo.getZoomlevel()) {
markerOptions.visible(true);
}
Marker marker = googleMap.addMarker(markerOptions);
userMarkers.put(marker, markingInfo);
marker.setDraggable(false);
}
Log.i("Existing markers", "Successfully placed existing markes on map ");
}
// To put marker name and icon on the map
private Bitmap drawTitleOnMarkerIcon(MarkingInfo markingInfo, int color) {
String title = markingInfo.getName();
int icon = AddLocationTypeIcon.getIcon(markingInfo.getType());
Bitmap bm = BitmapFactory.decodeResource(getResources(), icon).copy(Bitmap.Config.ARGB_8888, true);
int bmWidth = bm.getWidth();
int bmHeight = bm.getHeight();
int canvasWidth = bmWidth;
int canvasHeight = bmHeight;
if (bmWidth > 90) {
canvasWidth = (int)convertToPixels(getApplicationContext(), bmWidth - bmWidth/2);
canvasHeight = (int)convertToPixels(getApplicationContext(), bmHeight - bmHeight/2);
bm = Bitmap.createScaledBitmap(bm, canvasWidth, canvasHeight, true);
} else {
canvasWidth = (int)convertToPixels(getApplicationContext(), 72);
canvasHeight = (int)convertToPixels(getApplicationContext(), 72);
bm = Bitmap.createScaledBitmap(bm, canvasWidth, canvasHeight, true);
}
Paint paint = new Paint();
paint.setColor(color);
paint.setAntiAlias(true);
paint.setFakeBoldText(true);
paint.setTextSize(convertToPixels(getApplicationContext(), paint.getTextSize()-3));
float canvasCoorHeight = canvasHeight/2 + paint.getTextSize() + 6;// first y coordinate to start text from
while(title.length() > 0) {
title = drawTextOnCanvas(paint, bm, title, canvasWidth, canvasCoorHeight);
canvasCoorHeight = canvasCoorHeight + paint.getTextSize();// next text to draw at 12.5 + height
}
return bm;
}
Android Google maps API V2 center markers
You should use the CameraUpdate class to do (probably) all programmatic map movements.
To do this, first calculate the bounds of all the markers like so:
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for each (Marker m : markers) {
builder.include(m.getPosition());
}
LatLngBounds bounds = builder.build();
Then obtain a movement description object by using the factory: CameraUpdateFactory:
int padding = 0; // offset from edges of the map in pixels
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
Finally move the map:
googleMap.moveCamera(cu);
Or if you want an animation:
googleMap.animateCamera(cu);
*Got from another post: Android map v2 zoom to show all the markers
Related Topics
Back to Main Activity from Notification-Created Activity
Android: Subscribe to Firebase Cloud Messaging(Fcm) Topic
Viewpager Offscreen Page Limit
How to Detect If Android Device Is Paired with Android Wear Watch
Mediaplayer Setdatasource, Better to Use Path or Filedescriptor
Httpclient on Android:Nohttpresponseexception Through Umts/3G
Android - Getting from a Uri to an Inputstream to a Byte Array
Pop the Fragment Backstack Without Playing the Pop-Animation
Android Gridview Keep Item Selected
Launching an Android Application from the Browser
Get Current Visible Text in Textview
Android Studio 3.0 Compile Issue (Cannot Choose Between Configurations)
Lint Error "Do Not Treat Position as Fixed; Only Use Immediately..."