I am trying to set min zoom level to be 15 and user cannot zoom out of that level but he can zoom into 20 .
So throughout the app the user can use zoom enabled(true) but cannot go out of zoom level 15.Should I disable zoom afte 15 ? but that would even disable zoom in .
I dont know how to manage onl one condition in this case .How do I do it.I really appreciate any help.
Thanks in Advance
public OnCameraChangeListener getCameraChangeListener()
{
return new OnCameraChangeListener()
{
#Override
public void onCameraChange(CameraPosition position)
{
Log.d("Zoom", "Zoom: " + position.zoom);
if(previousZoomLevel != position.zoom)
{
isZooming = true;
if(zoomscale==15){
googleMap.getUiSettings().setZoomGesturesEnabled(false);
googleMap.getUiSettings().setZoomControlsEnabled(false);
}
}
previousZoomLevel = position.zoom;
}
};
}
As of current version (3.2.65) there is no API for setting min/max zoom level.
Hopefully there will be in the future, as this is already requested on gmaps-api-issues.
Anyway there is a way to handle it nicely in your own code: in onCameraChange if zoom is smaller than 15, call GoogleMap.animateCamera back to zoom 15. It will not stop the user from zooming out to see the whole world, but it will start zooming in after user finishes doing what they feel like doing. It will look similar to this: http://youtube.com/watch?v=-nCZ37HdheY (not only zoom, but also position is "corrected").
Try this
This won't allow the user to zoom out after the given zoom level.
mMap.setMinZoomPreference(float yourDesiredLevel);
Related
I want to implement a touchListener on a polyline displayed with Google Maps V2 Android API.
Zoom level:
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(lat_Lng, 5);
I tried the following polyline touch code:
boolean onpoly = false;
for (Polyline polyline : mPolylines) {
for (LatLng polyCoords : polyline.getPoints()) {
float[] results = new float[1];
Location.distanceBetween(points.latitude, points.longitude, polyCoords.latitude, polyCoords.longitude, results);
if (results[0] < 100 ) {
onpoly = true;
Marker mark = map.addMarker(new MarkerOptions().position(points).title("AddEvent")
.snippet("" + addressaddexpense).icon(BitmapDescriptorFactory.fromResource(R.drawable.addicon)));
UtilsClass.dropPinEffect(mark);
}// end if..
} // end for loop
}// end for loop
if (onpoly == true) {
Toast.makeText(getActivity(), "Poly line detected", Toast.LENGTH_SHORT).show();
}// end if
It worked but not perfectly.
it will not detect the touch event unless i zoom in, sometimes forcing me to tap the map more than 5 times before zooming to achieve detection.
I then changed the aforementioned if condition from if (results[0] < 100 ) to if (results[0] < 150 * (22 - map.getCameraPosition().zoom)) and the functionality improved but it still does not work all the time.
Note: I want to detect polyline touch at any zoom level.
try this
final List<LatLng> latLngList; // Extract polyline coordinates and put them on this list
private GoogleMap map;
for(int i = 0; i < latLngList.size(); i++){
MarkerOptions mar = new MarkerOptions();
mar.position(new LatLng(latLngList.get(i).latitude, latLngList.get(i).longitude)).icon(BitmapDescriptorFactory.fromResource(R.drawable.trasparent_image)); //this image should be very small in size and transparent
map.addMarker(mar);
}
map.setOnMarkerClickListener(new OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker arg0) {
for(int i = 0; i < latLngList.size(); i++){
if(latLngList.get(i).latitude == arg0.getPosition().latitude && latLngList.get(i).longitude == arg0.getPosition().longitude){
Toast.makeText(MainActivity.this, "PolyLineClick ", Toast.LENGTH_SHORT).show();
break;
}
}
return false;
}
});
Until the questions in my comments are answered i thought i'll try to make them redundant by suggesting the usage of a 3rd party library; android-maps-utils
To do what i think you might be trying to do simply integrate the library and use the following line:
PolyUtil.isLocationOnPath(point, polyline.getPoints(), isGeodesic, tolerance);
For more information you can also look into this thread which seems applicable.
Goodluck.
I think your approach is correct. The only thing that fails is the distance check. And this is because os the touch and the zoom level:
You know that when you tap on the screen, the screen point that is passed to the applications is the center of your finger surface, that is in touch with the screen. This means, that even if it seems, that your finger is exactly over the PolyLine, it can be displaced with some pixels...
Now is time for the Zoom level, and depending in its current value, the distance between the point passed to the application and the PolyLine, can vary very much.
As a result, the if clause fails, and you have to tap several times, until some of your taps is near enough to the PolyLine. And of course it gets better with higher zoom level.
You should include the zoom level as you have done in you edited code, but with some extras: Check the "delta" that you will allow to enter the if, but on the max zoom level. It should be a small value. Then you have to just multiply it by the current zoom level, and calculate how this delta changes. Use this zoom dependant value in your if comparison.To enhance it, you can make a more complex calculation and get this delta, starting from pixel distance. Lets say, a tap that at 50px or less to the PolyLine will be accepted. Calculate this pixel distance in meters, again on the max zoom level and use it multiplied by the current zoom...To enhance it even more, you can get this pixel distance, to be dependant on the screen resolution and density.
Here, you can find how to calculate screen pixels to meters: https://stackoverflow.com/a/13635952/4776540
I implemented a similar thing in the following way:
Convert all locations to the screen coordinates using map.getProjection().toScreenLocation()
Use standard distance formula to determine distance from point to point (or from point to segment, if you want to detect clicks on line segments too) and if this distance is less than some threshold - click is detected.
The key point here is to use map projection to get screen coordinates. This way the decision depends on the real pixel distance on the screen, and does not depend on a zoom level.
I am trying to change the zoom level when I click on my Location button but it is always getting default zoom value.
Guess what, the zoom doesn't work only in Animate part. If I use moveCamera it changes the zoom level to what I have specified.
Here is what I am trying:
googleMap.setOnMyLocationButtonClickListener(new OnMyLocationButtonClickListener()
{
#Override
public boolean onMyLocationButtonClick()
{
Log.e("Inside","Click part");
LatLng ll = new LatLng(curlat,curlong);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(ll, 20);
googleMap.animateCamera(update);
return false;
}
});
Not sure what is wrong here in the animate part?
You should return true instead of false. Otherwise the map performs the default action in addition, which most probably (I did not check in detail) includes to move to the current position (same as your ll) but using the "current" zoom level. The current zoom level has not yet changed much, if you animate the camera. But it has changed completely if you move the camera directly without animation.
I need to zoom out my map. I have tried --mMapView.zoomout();-- but it only zoom out 1 level, I want to zoom out further. I know can do in .xml --mapoptions.ZoomLevel="5"-- but I wanted to put in my Activity Class. Some guides is needed, thank you very much.
MapOptions option = new MapOptions(MapOptions.MapType type);
// Sets zoom level.
option.setZoom(int zoom);
// Switches basemap by the given options.
mMapView.setMapOptions(MapOptions options);
I think, Best way to handle zoom with animation,Even, it will work nice if you add any other layer like "One Map".
/* set zoom level */
double LEVEL=5;
mMapView.centerAt(lat, lng, false);
mMapView.setScale(ZOOM_LEVEL, true);
if you want to show current location with proper Zooming use below code
mMapView.getLocationDisplayManager().setAutoPanMode(LocationDisplayManager.AutoPanMode.LOCATION);
mMapView.getLocationDisplayManager().start();
I have an app that uses a Google Map Fragment and i want to set somehow a limit to the zoom level that the user can get to by using the +/- icons provided in the map fragment.
Is there any known way to do this?
MY SOLUTION:
private final GoogleMap.OnCameraChangeListener mOnCameraChangeListener =
new GoogleMap.OnCameraChangeListener() {
#Override
public void onCameraChange(CameraPosition cameraPosition) {
Log.d("Zoom", "Zoon: " + cameraPosition.zoom);
if (cameraPosition.zoom > 16.01) {
getMap().animateCamera(CameraUpdateFactory.zoomTo(16));
}
}
};
in onViewCreated() :
//set the zoom limit
getMap().setOnCameraChangeListener(mOnCameraChangeListener);
So far this is showing me the zoom level each time the zoom level changes. i am not sure how i can limit the zoom here though... i can't just do cameraPosition.zoom=15 since it's a final variable.
Any suggestions highly appreciated.
You cannot limit zoom as of version 4.0.30 of the API.
There is a pending feature request for that here: https://code.google.com/p/gmaps-api-issues/issues/detail?id=4663
If you cannot wait, you can try to implement something like this: http://youtube.com/watch?v=-nCZ37HdheY
This uses OnCameraChangeListener and GoogleMap.animateCamera.
You can remove the zoom buttons and use your own, where you have more control. Then, you can disable buttons as needed based upon business rules.
We all know that some of the predefined landmarks on Google Maps does not appear on a lower zoom level, but on a higher zoom level, it suddenly appears. I would like to know If I can make a customized marker not appear at lower zoom levels, then appear at higher ones.
EDIT: Here is a snippet of my code.
// Changing marker icon
marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.gasbig));
// adding marker
map.addMarker(marker);
//position on Center
CameraPosition cameraPosition = new CameraPosition.Builder().target(
new LatLng(14.635356, 121.03272914)).zoom(16).build();
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
map.setOnCameraChangeListener(new OnCameraChangeListener() {
#Override
public void onCameraChange(CameraPosition arg0) {
if(arg0.zoom > 7){
marker.visible(true);
}
}
});
I tried the suggestion of MaciejGórski but the marker still appears on all zoom levels. I'm sorry about the question I'm still an android newbie.
Thanks in advance.
You can do that for any Marker you want: call setVisible in OnCameraChangeListener.onCameraChange callback with true or false depending on CameraPosition.zoom value.
Edit after question edit:
You need to keep a reference to Marker instead of MarkerOptions:
// adding marker
marker = map.addMarker(markerOptions);
and call setVisible on that marker:
#Override
public void onCameraChange(CameraPosition cameraPosition) {
marker.setVisible(cameraPosition.zoom > 7);
}
Note: setVisible is always called there, but this might not be optimal when using many Markers.
You could possibly do it by modifying my answer to: Android Maps v2 - animate camera to include most markers
Otherwise using Android Maps Extensions might be a good choice. No experince with your specific needs though.
Just realized I might have misunderstood the question. Thought you meant your own markers. Nevertheless have a look at the extensions library. Could very well be that they have something useful.