I know there are a few threads around on this topic but i havent yet found a good enough solution.
I NEED to place a marker over the top of all other markers. How do i do this?
I have tried adding this marker before and after all other markers, however the ordering seems to be from bottom (front) to top (back) on the map view. This is not acceptable as I want to center on a marker which is in front of all markers.
I know that Polygons can be zOrdered but I would MUCH prefer a nicely styled graphic!
In-fact you can't order a Polygon over a marker!
"The order in which this polygon is drawn with respect to other overlays, including Polylines, GroundOverlays and TileOverlays, but not Markers. An overlay with a larger z-index is drawn over overlays with smaller z-indices. The order of overlays with the same z-index value is arbitrary. The default is 0." - https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/model/Polygon
Does anyone have any ideas on this?
Aiden Fry's answer works if you do actually display an InfoWindow. If you don't, the marker won't come to the front. Here is a hack to make it come to the front anyway:
Create a custom InfoWindowAdapter that displays a 0dp info window:
public class MapWindowAdapter implements GoogleMap.InfoWindowAdapter {
private Context context = null;
public MapWindowAdapter(Context context) {
this.context = context;
}
// Hack to prevent info window from displaying: use a 0dp/0dp frame
#Override
public View getInfoWindow(Marker marker) {
View v = ((Activity) context).getLayoutInflater().inflate(R.layout.no_info_window, null);
return v;
}
#Override
public View getInfoContents(Marker marker) {
return null;
}
}
Here is the code for the layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="0dp">
</LinearLayout>
When you set up your map, set the adapter and custom InfoWindowAdapter and OnMarkerClickListener (using showInfoWindow and returning true as AIden Fry advised):
mMap.setInfoWindowAdapter(new MapWindowAdapter(this));
//Call showInfoWindow to put marker on top of others.
myMarker.showInfoWindow();
This is not beautiful, but I didn't find any other way to deal with z-indexing without using info windows.
This was added in Google Play Services 9.2 (June 27, 2016)
The new MarkerOptions.zIndex() sets the stack order of a marker in relation to other markers on the map. Read more about marker z-indexes and the effect of z-index on click events. (Issue 4688)
Whilst not the perfect solution! I have figured out how to show a selected (tapped) marker over all other markers by consuming the onMarkerClick event. Returning TRUE will consume this event, so we have to do the showInfoWindow and zoom to center
#Override
public boolean onMarkerClick(Marker marker) {
//Manually open the window
marker.showInfoWindow();
//Animate to center
sMapFrag_v2.getMap().animateCamera(CameraUpdateFactory.newLatLng(marker.getPosition());
//Consume the method
return true;
}
Related
In my map , I have some list of Markers, now I need to achieve the output as like below
i.e. marker with index number.
I have searched over google but didn't get any solution, Please help me to get an idea to implement this.
Do whatever you want with Google Map Markers
I have made a very flexible and interesting hack to achieve whatever kind of marker you want.
What I do is,
Create an XML Layout file and design it the way you want your marker to appear. For example in your case it is going to be a Green Marker image with a TextView on it.
Set the values of TextViews as per requirement.
Convert the Layout file to an image and get BitmapDescriptor Object.
Create a custom marker by using the image you just created.
The benefit of such view over Info Window is that you can open window for multiple markers at same time. As in InfoWindow you can open it only for one marker at one time.
Code Sample
Check out my this answer for Code Help.
For custom marker:
You can create a custom image and draw an index number in top of
that image
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 do I disable the Zoom In button on the Google Map?
I tried looking for commands like:
map.getUiSettings().setZoomControlsEnabled(true)...SOMETHING but nothing exists.
I want to leave creating my own buttons for zooming as a last resort.
UPDATE: I should clarify, I only want to disable the Zoom In button, not the whole Zoom Control.
the setting you used is right, but to disable it, it should be false.
map.getUiSettings().setZoomControlsEnabled(false);
hope I got your question right..
Edit
Unfortunately, you can't hide one zoom buttons, what you can do is hide both buttons and create your own custom zoom control and place them on the your map.
#Coderji said it's impossible. I say it's possible :-)
You can hide one of zoom button (+ or -) and perform on them all operations like in normal view (margins, size, padding, etc.)
SupportMapFragment mapView = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map));
View zoomControls = mapView.getView().findViewById(0x1);
for(int i=0;i<((ViewGroup)zoomControls).getChildCount();i++){
View child=((ViewGroup)zoomControls).getChildAt(i);
if (i==0) {
// there is your "+" button, zoom in
}
if (i==1) {
// there is your "-" button, I hide it in this example
child.setVisibility(View.GONE);
}
}
Call this on your GoogleMap to get UiSettings
https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/GoogleMap#getUiSettings()
ThensetZoomControlsEnabled(false) on the UiSettings
https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/UiSettings#setZoomControlsEnabled(boolean)
In jetpack compose you can easily set zoomControlsEnabled = false
GoogleMap(uiSettings =
MapUiSettings(
zoomControlsEnabled = false
)
) {}
In Kotlin you can use this code to enable
mMap.uiSettings.setZoomControlsEnabled(true)
Can I disable drag functionality when the user tries to drag the map with his fingers without disturbing the Zoom in and Zoom out?
Any one please suggest an idea of doing this!
Thanks for your Precious help!!
You can disable dragging in MapFragment using:
googleMap.getUiSettings().setScrollGesturesEnabled(false);
I think here's what you're looking for:
Inside Google Maps Android v2
Scroll (pan) gestures
A user can scroll (pan) around the map by dragging the map with their
finger. You can disable scrolling by calling
UiSettings.setScrollGesturesEnabled(boolean).
for disable dragging in MapFragment this code :
googleMap.getUiSettings().setScrollGesturesEnabled(false);
works as #tomrozb said.
but it dosn't disable map zoom by touch on map. for that use this code beside above code:
googleMap.getUiSettings().setZoomGesturesEnabled(false);
you can use isScrollGesturesEnabled for map
java:
googleMap.getUiSettings().setZoomGesturesEnabled(false);
kotlin
googleMap?.uiSettings?.isScrollGesturesEnabled = false
You can disable dragging in MapFragment using:
mMap.getUiSettings().setScrollGesturesEnabled(false);
mMap.getUiSettings().setZoomGesturesEnabled(false);
mMap.getUiSettings().setScrollGesturesEnabledDuringRotateOrZoom(false);
You don't have to set this in code. You can configure the gestures from XML:
<fragment
xmlns:map="http://schemas.android.com/apk/res-auto"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:id="#+id/mapFragment"
map:uiScrollGestures="false" />
As well as uiScrollGestures, you can set uiZoomGestures, uiTiltGestures, and uiRotateGestures.
See XML Attributes documentation.
Can be achieved inside the onMapReady class:
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
//Hide zoom controls or buttons
mMap.getUiSettings().setZoomControlsEnabled(false);
//Restrict zoom gestures
mMap.getUiSettings().setZoomGesturesEnabled(false);
}```
Hai, I am new android developer. I want to create a Tab host application . There will be 3 different tab items .Each item is attached with map view. I want to change the zoom label of each map when tab each item. How can i do it...
In each tab item , set programatically the following line with MapView:
mapView.setBuiltInZoomControls(true);
mapView.getController().setZoom(13); //set zoom level according to need for each tab item
Hope this will solve your issue.
Use this event
public void onTabChanged(String arg0) {
int currentTabvalue = tabHost.getCurrentTab();
if(currentTabvalue==0)
mapView.getController().setZoom(----);
else if(currentTabvalue==1)
mapView.getController().setZoom(----);
else if(currentTabvalue==2)
mapView.getController().setZoom(----);
}
I thinks this is the code you want to achieve this.
In this code you fill the value in the place of ----- according to your requirement.
I hope you have got your answer.
I am facing a problem of adding overlays. I want to add button which will toggle between normal and satelitte view, and some textView, which will display my actual coordinates and those will be updated on my location change. I tried to put there classic TextView, added into my map.java import for using GPS, and onLocationChanged I am trying to update TextView's, but no success. It must be done via Overlays, but I can't see, how to set on the screen my desired items - button with specific function and regularly updated textView. Any ideas?
THanks
edit:
int g=0;
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setEnabled(true);
mapView.setClickable(true);
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.toggle:
if (g!=1){
mapView.invalidate();
mapView.setSatellite(true);
mapView.invalidate();
g=1;
}else{
mapView.invalidate();
mapView.setSatellite(false);
mapView.invalidate();
}
break;
}
}
You don't need a overlay for this. Just define you layout using a layout xml file with an RelativeLayout as root element.
With a relative layout you can place elements like TextViews or a Buttons on top of each other. So in your case you would first define the MapView in your layout file and then the elements you want to display on the MapView. Now all elements should be rendered in the upper left corner on top of each other. Using the attributes defined by the RelativeLayout you can now align your elements in the way you want. Have a look at the RealtiveLayout tutorial for a better understanding.
Here a some nice resources:
http://developer.android.com/resources/tutorials/views/hello-mapview.html
http://mobiforge.com/developing/story/using-google-maps-android
Efficient Map Overlays in Android Google Map
Hope you get an answer :-)