Show Marker title permanently in Google maps for android - android

I want to display each marker title and snippet for all the markers in Google maps without clicking the marker, i.e it should display by default for all markers, something like this is trying to achieve?
How can I show label/title for marker permanently in Google Maps V3?
according to documentation there is an way
showInfoWindow() but only displays for only one marker, how can i do this for all markers? or is there any other way to display marker information without clicking it?

For multiple markers with title write below code on onMapReady method of your activity.
Hope this helps...
for (int i = 0; i < listOfMarkers.size(); i++) {
LatLng latLng = new LatLng(listOfMarkers.get(i).latitude,listOfMarkers.get(i).longitude);
Marker mk = googlemap.addMarker(new MarkerOptions()
.position(latLng)
.icon(BitmapDescriptorFactory.fromBitmap(getMarkerBitmapFromView(listOfMarkers.get(i).getTitle());
}
}
private Bitmap getMarkerBitmapFromView(String resId) {
View customMarkerView = ((LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.map_item_textview, null);
TextView markerImageView = (TextView) customMarkerView.findViewById(R.id.tv_map_item_price);
markerImageView.setText(resId);
markerImageView.setTextColor(Color.parseColor("#000000"));
customMarkerView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
customMarkerView.layout(0, 0, customMarkerView.getMeasuredWidth(), customMarkerView.getMeasuredHeight());
customMarkerView.buildDrawingCache();
Bitmap returnedBitmap = Bitmap.createBitmap(customMarkerView.getMeasuredWidth(), customMarkerView.getMeasuredHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
canvas.drawColor(Color.WHITE, PorterDuff.Mode.SRC_IN);
Drawable drawable = customMarkerView.getBackground();
if (drawable != null)
drawable.draw(canvas);
customMarkerView.draw(canvas);
return returnedBitmap;
}

Related

how to use xml layout as cluster marker Android

I have a question, Is it possible to set a layout with multi imageviews and textviews as a cluster marker on the google map by android map utils? and how? if somebody provide an good example it would be great this an example of what i want to do:
thanks
I don't know if there is an easier way than what I used that was a year a go this method takes a lot of time if the data set is big so use it on small data set or talk to the designer for other options.
you can create a layout with what you want the get the bitmap of this layout this add the bitmap as a marker to the map since add marker only takes images.
*you need to set the data before creating the bitmap
first step inflate and set data to your layout
View marker = ((LayoutInflater) getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.marker_layout, null);
second get the bitmap from this view
Bitmap bitmap = createDrawableFromView(
getActivity(), marker);
third add the bitmap as a marker
MarkerOptions mo = new MarkerOptions().position(
new LatLng(lat, lng))
// .title(address)
.snippet(i + "")
.icon(BitmapDescriptorFactory
.fromBitmap(bitmap));
Marker customMarker = googleMap.addMarker(mo);
this is how to get the bitmap
public static Bitmap createDrawableFromView(Context context, View view) {
DisplayMetrics displayMetrics = new DisplayMetrics();
if (context != null) {
((Activity) context).getWindowManager().getDefaultDisplay()
.getMetrics(displayMetrics);
view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels);
view.layout(0, 0, displayMetrics.widthPixels,
displayMetrics.heightPixels);
view.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(),
view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
return null;
}
goodluck with that it was a lot of pain.

Customising marker in android map, placing text inside image

I have an image to show as marker, inside that image i have to update time when the map is moving
The problem facing now is
I dont know how to show time inside my custom image.
BitmapDescriptor descriptor = BitmapDescriptorFactory.fromResource(R.drawable.ic_remaining_time);
googleMap.addMarker(new MarkerOptions()
.position(cameraPosition.target)
.title(cameraPosition.toString())
.icon(descriptor)
);
Please anyone help me
Make drawable on every time when map get changed or move and set icon like below
marker.setIcon(BitmapDescriptorFactory.fromBitmap(CommonUtils.writeOnDrawable(mContext,R.drawable.ic_location,"your text here").getBitmap()));
// Bitmap Drawable method
public static BitmapDrawable writeOnDrawable(Context context, int drawableId, String text){
Bitmap bm = BitmapFactory.decodeResource(context.getResources(), drawableId).copy(Bitmap.Config.ARGB_8888, true);
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
paint.setTextSize(30);
paint.setTextAlign(Paint.Align.CENTER);
Canvas canvas = new Canvas(bm);
// Change the position of text here
canvas.drawText(text,bm.getWidth()/2 //x position
, bm.getHeight()/2 // y position
, paint);
return new BitmapDrawable(context.getResources(),bm);
}
you can get the marker when u first time add marker in google map object
Marker marker = googleMap.addMarker(new MarkerOptions().position(new LatLng(mLocation.getLatitude(), mLocation.getLongitude())).title(allJob.getBusiness().getName()).icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_location_icon)));
I don't reccomend to update repeatedly a text inside an image in marker because to achieve that you must creat a bitmap with the image and text and put that bitmap as icon marker and that's not free (in terms of memory usage).
But you can try, anyway... Create the bitmap descriptor that way and put inside the icon:
public static Bitmap createDrawableFromView(Context context, View view) {
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
view.setLayoutParams( new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT ) );
view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels);
view.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels);
view.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap); view.draw(canvas);
return bitmap;
}
Pass to that function the view you can set as icon, inflating an xml maybe.
Try something like this -
BitmapDescriptor descriptor = BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getActivity().getResources(),R.drawable.ic_remaining_time));
googleMap.addMarker(new MarkerOptions()
.position(cameraPosition.target)
.title(cameraPosition.toString())
.icon(descriptor)
);
To get customized marker inflate your custom layout at the place of marker like below -
private Bitmap fillCustomizedMarker(int markerResource) {
//Log.d(TAG, "fillMarkerWithText ++");
View markerLayout;
markerLayout = getActivity().getLayoutInflater().inflate(R.layout.custom_marker, null);
markerLayout.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
markerLayout.layout(0, 0, markerLayout.getMeasuredWidth(), markerLayout.getMeasuredHeight());
ImageView markerImage = (ImageView) markerLayout.findViewById(R.id.marker_image);
TextView timeText = (TextView) markerLayout.findViewById(R.id.timeText);
markerImage.setImageResource(markerResource);
markerCost.setText(time);
final Bitmap bitmap = Bitmap.createBitmap(markerLayout.getMeasuredWidth(), markerLayout.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
markerLayout.draw(canvas);
return bitmap;
}
And to update this layout i would suggest you to use handler in android. It will update your UI efficiently.

Add Glow Effect Around my custom marker in Android Google Map v2

I am stuck in this task of adding custom marker with glow. I have attached my desired Image. I am Wondering about to find solution but not able to solve.
marker code which i am using is this.
map.addMarker(new MarkerOptions()
.title(title)
.position(points)
.snippet(snippet)
.icon(BitmapDescriptorFactory.fromResource(res))
).showInfoWindow();
I don't want to use any extra Image. I want to do it through code
,help me please. Thanks!
You can draw a normal custom marker bitmap over the glow effect(only) bitmap.
That should look something like this:
int resourceIdCar = R.drawable.car;
Bitmap bmpCar = BitmapFactory.decodeResource(getResources(), resourceIdCar);
int resourceIdArrow = R.drawable.arrow;
Bitmap bmpArrow = BitmapFactory.decodeResource(getResources(), resourceIdArrow);
Bitmap bmpOverlay = overlay(bmpCar,bmpArrow);
myMarker = googleMap.addMarker(new MarkerOptions()
.position(DerekPos)
.icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher))
.title("Hello world")
.draggable(true)
//.icon(BitmapDescriptorFactory.fromResource(R.drawable.car))
.icon(BitmapDescriptorFactory.fromBitmap(bmpOverlay))
);
//=-=-=-=-=-=-=
private Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bmp1, new Matrix(), null);
canvas.drawBitmap(bmp2, new Matrix(), null);
return bmOverlay;
}

Change Marker with different color image in android GoogleMap V2

I created custom marker in android Google Maps API V2 by using below lines of code:
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);
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(),
R.drawable.map_brown), 0, 0, paint);
mMap.addMarker(new MarkerOptions()
.position(clickedPosition)
.icon(BitmapDescriptorFactory.fromBitmap(bmp))
.anchor(0.5f, 1)
);
Now, how can I change the marker image while clicking on the marker from map_brown to map_gray
thanks in advance..
addMarker returns Marker object you can interact after it is created.
After you keep a reference to this Marker
mMarker = mMap.addMarker(...);
you can change its icon with
mMarker.setIcon(BitmapDescriptorFactory.fromBitmap(bmpWithGraySomething));
in GoogleMap.OnMarkerClickListener callback.
You may also use callback's parameter instead of keeping the reference as a field.

How to add text above a marker on android Google Maps v2?

I made an android application and i want add text above (or below) a marker. I found this link How to add text above a marker on Google Maps? but it's for the old api of google maps. So i wonder if we can do the same thing with the new api ?
I have ever tried to do this with this code :
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.pin_favoris).copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(bm);
Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setTextSize(25);
canvas.drawText("Favoris", 0, bm.getHeight(), paint); // paint defines the text color, stroke width, size
BitmapDrawable draw = new BitmapDrawable(getResources(), bm);
Bitmap drawBmp = draw.getBitmap();
// gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(fakeMarker, 15));
gMap.addMarker(new MarkerOptions()
.position(fakeMarker)
.icon(BitmapDescriptorFactory.fromBitmap(drawBmp))
);
but the text can only be displayed in the Bitmap (not above/below) !
if you just need to add text above a marker, do something like this:
mMap.addMarker(new MarkerOptions().position(point).title(
"fsdfsdf sdfsdf")).showInfoWindow();
Have you tried this :
gMap.addMarker(new MarkerOptions()
.position(fakeMarker)
.title("Favoris")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.pin_favoris))
);
Why not just make your Bitmap / Canvas bigger, then you can draw your text above the marker.
This should help: How to increase Canvas size in Android?

Categories

Resources