I want to zoom in or out of the map depending on where markers on my map are placed. The markers are dynamic so I can not chose a static zoom level. I understand that there is 'zoomToRegion' method in iOS to acheive this. Is there something similar in Android?
Yes.
You have to use the method zoomToSpan of MapController.
// zoom into map to box of results
int centerLat = (int)(((maxLat-minLat)/2+ minLat)*1E6);
int centerLon = (int)(((maxLon-minLon)/2 + minLon)*1E6);
mapView.getController().animateTo(new GeoPoint( centerLat, centerLon));
mapView.getController().zoomToSpan((int)((maxLat-minLat)*1E6), (int)((maxLon-minLon)*1E6));
Related
I have a geojson with some polygons, added it as a source and created a layer to show those features on the mapbox map.
Since I am using this method: (Edit from the sample souce: MapboxGLAndroidSDKTestApp)
final PointF pixel = mapboxMap.getProjection().toScreenLocation(point);
List<Feature> fList = mapboxMap.queryRenderedFeatures(pixel, "polygons");
for(Feature feature:fList){
Log.d(TAG,"FeatureInfo: "+feature.toJson());
}
I can check if the marker is inside one of those polygons by passing the marker position to it.
But when I move my camera far away to the marker, the polygon is not rendered and the checker is not work anymore.
So my question is, is there any way to check if the marker is inside the polygon, which no matter where is my camera pointing at?
To check a point whether lies or not inside a polygon on mapbox
can use turf.js library http://turfjs.org/
This is the way in which you don't need to plot point or polygon on the mapbox(without plotting).
var point = turf.point([-75.343, 39.984]);
// here first is lng and then lat
var polygon = turf.polygon([[
[-2.275543, 53.464547],
[-2.275543, 53.489271],
[-2.215118, 53.489271],
[-2.215118, 53.464547],
[-2.275543, 53.464547]
]], { name: 'poly1'});
// here first is lng and then lat
now you can use inside method from turf lib
i.e turf.inside(point, polygon); // return boolean True/False
Use TurfJoins.inside(yourPoint, yourPolygon)
I am trying to write an android application that use google map to show some points (shop places) on the map. I have shop specifications including LatLng, title, etc on a database server and have a rest api to access them. my problem is the total amount of shops are too much, so I can't get all of them and show them on the map, so I need to have some kind of lazy loading showing to access just shops that are required.
I found this link, but it isn't lazy loading, what I understood is that it gets all the item first and show them with marker, which isn't what exactly I need.
Is it possible to do this? if yes how ? please give me some example or tutorial if it is possible.
thanks in advance.
Why you dont use Google Maps Android Marker Clustering Utility?
I used to load 12000 markers in a map and is ok for me. I load all of this markers at the same time.
To use lazy loading you can determine the viewable map area and load only this markers.
You can use OnCameraChangeListener to determine it:
map.setOnCameraChangeListener(new OnCameraChangeListener() {
#Override
public void onCameraChange(CameraPosition position) {
BOUNDS = map.getProjection().getVisibleRegion().latLngBounds;
Float mapZoom = map.getCameraPosition().zoom;
LatLng latlong = map.getCameraPosition().target;
updateItems(BOUNDS);
mapZoom = map.getCameraPosition().zoom;
}
});
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();
Google recently introduced the circle as a map object in V2 and can easily be drawn:
https://developers.google.com/maps/documentation/android/shapes#circles
Does anyone know how to make this clickable, so that I can open an info window etc... ?
Thanks a lot for the help.
As of June 2016, Google has introduced clickable property of CircleOptions.
https://developers.google.com/android/reference/com/google/android/gms/maps/model/CircleOptions#public-constructor-summary
You can use Circle.setClickable(boolean) method as well and listen to OnCircleClickListener event.
https://developers.google.com/android/reference/com/google/android/gms/maps/model/Circle.html#public-methods
Update:
You should probably use GoogleMap::setOnCircleClickListener.
Old answer:
For Circles it should be easy: use OnMapClickListener and this code:
void onMapClick(LatLng position) {
LatLng center = circle.getCenter();
double radius = circle.getRadius();
float[] distance = new float[1];
Location.distanceBetween(position.latitude, position.longitude, center.latitude, center.longitude, distance);
boolean clicked = distance[0] < radius;
}
You can of course iterate over circles if you have more of them.
For others you have to code checking yourself. The algorithm for Polygon is not hard to find online.
It's very straight forward - mMap.setOnCircleClickListener(this);
I'm drawing a lot of markers on the map and when they located close they overlap each other. So I want to hide some markers on small zoom and show more markers when user zooming map. Like more zoom in, more markes. Here is example code of activity and creating of markers, as you can see I'm using google maps android api v2:
public class MainActivity extends FragmentActivity
{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GoogleMap map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
map.getUiSettings().setMyLocationButtonEnabled(true);
createMarkers(map);
}
private void createMarkers(GoogleMap map) {
double initLat = 48.462740;
double initLng = 35.039572;
for(float i = 0; i < 2; i+=0.2) {
LatLng pos = new LatLng(initLat + i,initLng);
map.addMarker(new MarkerOptions()
.position(pos)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)));
}
for(float i = 0; i < 2; i+=0.2) {
LatLng pos = new LatLng(initLat, initLng + i);
map.addMarker(new MarkerOptions()
.position(pos)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)));
}
}
It sounds like typical task for me, but i still didn't manage to find working solution. I've read this article https://developers.google.com/maps/articles/toomanymarkers but I've no idea how to implement it on android. Does anybody has some working code which can do this?
Here is my solution to this problem: https://github.com/Bersh/MarkersCluster
Maybe it's not the best solution, but it works for me. Hope it'll be usefull.
You are basically asking how to implement clustering on Android. As far as I know, there is no solution provided by Google to do this. The document you referenced in the comments (developers.google.com/maps/articles/toomanymarkers) is referring to the google maps javascript API. Unfortunately, none of the clustering related code is yet available on Android.
You will have to come up with your own algorithm to cluster Markers. The developers.google.com/maps/articles/toomanymarkers document that you pointed to may be a good starting point to decide what algorithm will work best for you (grid based clustering, distance based clustering, etc).
Another option that's slightly easier to implement (but not perfect) might be to change the Marker image to a smaller icon when the zoom level has changed so they don't overlap as much. Check out my answer to this question: https://stackoverflow.com/a/13976080/1103584
You could use that answer to determine when you've crossed a "zoom threshold" and change the Markers to something smaller once you've zoomed out passed a certain threshold and to a bigger image when you've zoomed back in. I use this trick in a couple of my own apps to change regular icons to small dot images when zoomed out, and back to the regular icon when you zoom back in.