Android Google Map: Dynamic Map zoom Level vs Static Map Zoom Level - android

I have to use Google dynamic map to search a location, scroll, zoom, etc and then take a screenshot of the map. Due to license restriction, I cannot take a screenshot (GMap.Snapshot(this)). So it was decided to download an image from google static map
When the user wants to switch from map dynamic mode to static map, he clicks a button and I take center latitude and longitude, zoom level and map size (it's xamarin android)
{
CenterLatLng = new double[] { GMap.CameraPosition.Target.Latitude, GMap.CameraPosition.Target.Longitude },
ZoomLevel = (int)GMap.CameraPosition.Zoom,
Type = GMap.MapType,
MapSize = new int[] { MapFragment.View.MeasuredWidth, MapFragment.View.MeasuredHeight }
};
with this parameters i will try to download a static map
int scale = 2;
var url = $"https://maps.googleapis.com/maps/api/staticmap?center={myobject.CenterLatLng[0]},{myobject.CenterLatLng[1]}&zoom={myobject.ZoomLevel}&size={myobject.MapSize[0] / scale}x{myobject.MapSize[1] / scale}&scale={scale}&maptype=satellite&format=png&key=xxxxxxxx";
but downloaded static map seems to have a different zoom from dynamic Map
Here a screenshot of the Dynamic Map displayed before than switching to Static map
Here a screenshot of the Static Map displayed after
how can I fix that difference between dynamic map zoom and static map zoom?

how can I fix that difference between dynamic map zoom and static map zoom?
I think the issue is caused by the size you set in the url size={myobject.MapSize[0] / scale}x{myobject.MapSize[1] / scale}.
You could create a new variable to divide the MapSize instead of using scale to change the size.

Related

add marker to map when required using lazy loading on android

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;
}
});

How to show boundary in map v2 on opening map?

I have a boundary on map and i saved all those tapped co-ordinates of that boundary. I want to show that complete boundary when i opens the map. How can i achieve it? I tried it by changing the (zoom level) camera position but not getting.
Actually my intention is how to pass more than 5 co-ordinates to map so that it shows all those passed co-ordinates on screen (fix the passed co-ordinates in mobile screen and show it when open it).
I tried this but not working perfectly, i want to change the zoom level dynamically based on passed co-ordinates.
private void showBoundary() {
final LatLng hmt = new LatLng(17.42214430,78.5483395);
final LatLng tharnaka = new LatLng(17.426542300,78.53627059999997);
final LatLng habsiguda = new LatLng(17.40681270000,78.547629599999940);
final LatLng uppal = new LatLng(17.401809200,78.56018919999997);
LatLngBounds bounds = new LatLngBounds.Builder()
.include(hmt)
.include(tharnaka)
.include(habsiguda)
.include(uppal)
.build();
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds,50));
}
Actually my intention is how to pass more than 5 co-ordinates to map so that it shows all those passed co-ordinates on screen (fix the passed co-ordinates in mobile screen and show it when open it).

MapController, OSMdroid, zoomToSpan() freezes application

I use OSMdroid to display an offline map. The MapView is placed inside a Fragment. I would like to zoom the map to cover two points (top_left, bottom_right). Currently when I call zoomToSpan() in onViewCreated() the app freezes. I have tried using BoundingBoxE6 and latitude, longitude span but nothing changes.
BoundingBoxE6 bb = new BoundingBoxE6(
LocationConstants.TOP_LEFT_LATITUDE,
LocationConstants.TOP_LEFT_LONGITUDE,
LocationConstants.BOTTOM_RIGHT_LATITUDE,
LocationConstants.BOTTOM_RIGHT_LONGITUDE);
or
int latitudeSpan = (int) Math.abs(LocationConstants.TOP_LEFT_LATITUDE*1E6 - LocationConstants.BOTTOM_RIGHT_LATITUDE*1E6);
int longitudeSpan = (int) Math.abs(LocationConstants.TOP_LEFT_LONGITUDE*1E6 - LocationConstants.BOTTOM_RIGHT_LONGITUDE*1E6);
give the same results.

Drawing dotted line on google maps v2 instead of solid

Instead of having a polygon with a solid line surrounding it I want to create one with a dotted line, is this possible?
I know you could do this when you override the onDraw method of the overlay in v1 but the Overlay class does not exist anymore so how else can I achieve this?
It's currently not possible, but you may upvote this enhancement here: http://code.google.com/p/gmaps-api-issues/issues/detail?id=4633
UPDATE
Recently, Google implemented this feature for polylines and polygons in Google Maps Android API v2 and marked issue 4633 as Fixed.
See information about stroke patterns in the Shapes Guide. See an example in the Polylines and Polygons tutorial.
You can also read the corresponding blog post here:
https://maps-apis.googleblog.com/2017/02/styling-and-custom-data-for-polylines.html
First of all, take a look on the API
https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/model/Polyline
it is not yet possible with v2, but on v3 javascript API, it already is, look here:
https://developers.google.com/maps/documentation/javascript/overlays#PolylineSymbols
But it seems that it's possible to use this v3 javascript API in an android app, look here:
https://developers.google.com/maps/articles/android_v3
Maybe, this will help you
Find a LatLng at a distance of radius units from center LatLng on Map
now convert both these LatLngs to screenCoordinates
Use the formula used to construct a cirle x = Rsin(theta) , y = Rcos(theta)
you divide the circle into N segments and then draw polylines(drawn on map) on the circumference of the circle converting the screen coordinates to LatLngs
more the number of N more it looks like a circle , I have used N = 120 according the zoom level ,I am using 13.
private void addDottedCircle(double radius) {//radius is in kms
clearDottedCircle();
LatLng center,start,end;
Point screenPosCenter,screenPosStart,screenPosEnd;
Projection p = mMap.getProjection();
center = searchCenterMarker.getPosition();
start = new LatLng(center.latitude + radius/110.54,center.longitude);
// radius/110.54 gives the latitudinal delta we should increase so that we have a latitude at radius distance
// 1 degree latitude is approximately 110.54 kms , so the above equation gives you a rough estimate of latitude at a distance of radius distance
screenPosCenter = p.toScreenLocation(center);
screenPosStart = p.toScreenLocation(start);
double R = screenPosCenter.y - screenPosStart.y;
int N = 120;//N is the number of parts we are dividing the circle
double T = 2*Math.PI/N;
double theta = T;
screenPosEnd = new Point();
screenPosEnd.x = (int)(screenPosCenter.x-R*Math.sin(theta));
screenPosEnd.y = (int) (screenPosCenter.y-R*Math.cos(theta));
end = p.fromScreenLocation(screenPosEnd);
for(int i =0;i<N;i++){
theta+=T;
if(i%2 == 0){
//dottedCircle is a hashmap to keep reference to all the polylines added to map
dottedCircle.add(mMap.addPolyline(new PolylineOptions().add(start,end).width(5).color(Color.BLACK)));
screenPosStart.x = (int) (screenPosCenter.x-R*Math.sin(theta));
screenPosStart.y = (int) (screenPosCenter.y-R*Math.cos(theta));
start = p.fromScreenLocation(screenPosStart);
}
else{
screenPosEnd.x = (int)(screenPosCenter.x-R*Math.sin(theta));
screenPosEnd.y = (int) (screenPosCenter.y-R*Math.cos(theta));
end = p.fromScreenLocation(screenPosEnd);
}
}
}
If you are still looking for an answer have a look at this :
How to draw dashed polyline with android google map sdk v2?

Is there something similar to 'zoomToRegion' in Android?

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));

Categories

Resources