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 :-)
Related
Any buddy have Custom Text View with Rotate, Zoom In, Zoom Out & Move functionality like MutiTouch Image View like this http://judepereira.com/blog/multi-touch-in-android-translate-scale-and-rotate/ in android?
I Want exactly like this screen shots.
1. In that screen draw simple text.
2. In that screen when i zoom this view Text auto wrap.
3. In that screen Move Text on view.
4. In that screen Rotate Text.
This all functionality do on TextView touch.
I used https://github.com/jcmore2/Collage to achieve a rotated textview
but i changed the whole code since the plugin works with images
CardView extends ImageView
i chnaged to MyCardView extends RelativeLayout then removed all the code related to images
and instead of using collage.createCollageResources(listRes);
i created my own function in my own CollageView class
public void createCollage(List<MyCardView> list) {
for (MyCardView layout : list) {
addCard(layout);
}
}
Now in the Activity you can add to the collageview a complete RelativeLayout with it's children not just images
this also will allow dealing with single view or multiple .. TextView or any thing else .. finally this will make the view not just rotated but dragged and zoomed with multi touch gesture.
Yes you can create it see below image and follow Here.
ImageView: To add ImageView
// add a stickerImage to canvas
StickerImageView iv_sticker = new StickerImageView(MainActivity.this);
iv_sticker.setImageDrawable(getResources().getDrawable(R.drawable.c10));
canvas.addView(iv_sticker);
TextView: To add TextView
// add a stickerText to canvas
StickerTextView tv_sticker = new StickerTextView(MainActivity.this);
tv_sticker.setText("nkDroid");
canvas.addView(tv_sticker);
However, build this did an awesome job.
I'm trying to display a custom button on a googlemap.
<Button
android:id="#+id/button_backHome"
android:layout_alignParentRight="true"
android:layout_marginTop="50dp"
android:layout_marginRight="11.5dp"
android:layout_height="39dp"
android:layout_width="39dp"
android:background="#drawable/custom_button"
/>
This displays the button directly below googlemap's Set Camera to Current location button in the top right of the screen, but obviously only on my test device. On other devices these settings don't align the button correctly.
XXHDPI (galaxy S4)
XHDPI (Nexus 10)
What would be a good solution, other then making all the buttons myself? Could I inherit the margins from the google button without knowing the style's name?
I'm pretty much at a loss here, and any help would be Much apreciated.
Thanks in advance!
Parent Layout for Google Maps map is RelativeLayout so best approach for custom button alignment with default Google Maps control button (for all screens) is to put that custom button into same layout as default Google Maps control button. Assume our activity with Google Maps map has layout like:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="<YOUR_PACKAGE>.MainActivity">
<fragment
android:id="#+id/map_fragment"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ImageView
android:id="#+id/custom_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:visibility="gone"
android:src="#drawable/ic_home_target"/>
</RelativeLayout>
As custom button View is better to use ImageView, because the default buttons layouts has 9 patches padding in them (details here), or it's necessary to set additional padding to default Google Maps control button. Now it belongs to activity layout. So in onMapReady(GoogleMap googleMap) we should:
1) get parent View for default Google Maps control button;
2) remove custom button view from root activity layout;
3) add custom button view to parent View for default Google Maps control button (defined in p.1);
4) set alignment for custom button view relative to default Google Maps control button.
With Source code like this:
...
private GoogleMap mGoogleMap;
private MapFragment mMapFragment;
private ImageView mCustomButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCustomButton = (ImageView) findViewById(R.id.custom_button);
mMapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map_fragment);
mMapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
// don't forget to allow LOCATION permission
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int locationPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
if (locationPermission == PackageManager.PERMISSION_GRANTED) {
setConrolsPositions();
}
} else {
setConrolsPositions();
}
}
void setConrolsPositions() {
try {
mGoogleMap.setMyLocationEnabled(true);
mGoogleMap.getUiSettings().setMyLocationButtonEnabled(true);
// get parent view for default Google Maps control button
final ViewGroup parent = (ViewGroup) mMapFragment.getView().findViewWithTag("GoogleMapMyLocationButton").getParent();
parent.post(new Runnable() {
#Override
public void run() {
try {
// get view for default Google Maps control button
View defaultButton = mMapFragment.getView().findViewWithTag("GoogleMapMyLocationButton");
// remove custom button view from activity root layout
ViewGroup customButtonParent = (ViewGroup) mCustomButton.getParent();
customButtonParent.removeView(mCustomButton);
// add custom button view to Google Maps control button parent
ViewGroup defaultButtonParent = (ViewGroup) defaultButton.getParent();
defaultButtonParent.addView(mCustomButton);
// create layout with same size as default Google Maps control button
RelativeLayout.LayoutParams customButtonLayoutParams = new RelativeLayout.LayoutParams(defaultButton.getHeight(), defaultButton.getHeight());
// align custom button view layout relative to defaultButton
customButtonLayoutParams.addRule(RelativeLayout.ALIGN_LEFT, defaultButton.getId());
customButtonLayoutParams.addRule(RelativeLayout.BELOW, defaultButton.getId());
// add other settings (optional)
mCustomButton.setAlpha(defaultButton.getAlpha());
mCustomButton.setPadding(defaultButton.getPaddingLeft(), defaultButton.defaultButton(),
defaultButton.getPaddingRight(), defaultButton.getPaddingBottom());
// apply layout settings to custom button view
mCustomButton.setLayoutParams(customButtonLayoutParams);
mCustomButton.setVisibility(View.VISIBLE);
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
and enabled Show layout boundaries option in Developer Options you should get something like that:
As you can see custom button has exactly same size as default button layout and placed exactly below it.
For better result it's necessary to adjust custom button glyph (padding, transparency, colors, etc.)
Or may be easiest way: don't use default Google Maps "GoogleMapMyLocationButton" button at all - set it's Enabled property to false (mGoogleMap.getUiSettings().setMyLocationButtonEnabled(false);) but use custom layout with button with same icon and functionality emulated by GoogleMap.animateCamera() on same layout with "target home" custom button.
You have a dpi problems in your posted image, Also in your posted xml you are only using 1 attribute values for all devices android:layout_marginRight="11.5dp" which will fail in other device with higher dpi, except the one you first tried it on.
solution:
Use the values folder with dpi click here to know about it.
What you need is to create a dimens attribute on each of the dpi folder ldpi, hdpi, etc. for the margin
sample:
<resources>
<dimen name="margin_right">11.5dp</dimen> //this is located in hdpi values dimension
</resources>
and another in xhdpi
<resources>
<dimen name="margin_right">greater than hdpi</dimen> //this is located in xhdpi values dimension
</resources>
to use it is to just call #dimension
android:layout_marginRight="#dimension/margin_right"
now using this solution will enable your device to pick the right value of the margin on different devices.
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)
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;
}
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.