calculating BOUNDS of google maps - android

m working with google maps for the first time. and my app api needs bounds of google map as a paramater. i dont know how to calculate it?
I have searched a bit but couldnt able to get anything.
33.867269,151.200857,-33.862022,151.215449 //look like this..
Thanks..

To get the bounds of current visible position of google map
VisibleRegion vr = googleMap.getProjection().getVisibleRegion();
LatLng sw = vr.latLngBounds.southwest;
LatLng ne = vr.latLngBounds.northeast;
LatLngBounds bounds = new LatLngBounds(sw, ne);
the the four points will be your southwest latitude,longitude and northeast latitude, longitude .
bounds.southwest.latitude , bounds.southwest.longitude ,bounds.northeast.latitude , bounds.northeast.longitude

Related

How to create a polyline/polygon around building or specific place using google map api v2?

Anybody knows how to create a polyline/polygon around building or specific place as shown in above image using google map api v2.
Thanks in advance.
Get the Latitude and Longitude of required points and use below code to draw polygon.
GoogleMap map;
// ... get a map.
// Add polygon in the area
Polygon polygon = map.addPolygon(new PolygonOptions()
.add(new LatLng(Lat1, Lon1), new LatLng(Lat2, Lon2), new LatLng(Lat3, Lon3), new LatLng(Lat4, Lon4))
.strokeColor(Color.RED)
.fillColor(Color.BLUE));
Its up to you to get the required Latitude and Longitude of targeted area

Snapshot google maps api v2 between two points

i'm doing an app in android studio and what i'm trying to do is a route tracking app, the user touch a play button and he can see in the map a marker with his route being drawn by polylines.
I did that already but i want your help because when the user touch the stop button i want to take a Snapshot of the complete route (including the start point and the end point) and i don't know how to do that, because if the route is long and the zoom is close to the map then the starting point isn't going to show...
I hope you understand and could help me! :)
Sorry for my English, I use this example in my code to zoom all the markers, its simple i use a cycle for add the LatLng and zoom all.. with CameraUpdateFactory.newLatLngBounds(bounds_latandlog,padding);, and you take the Snapshot...
LatLngBounds.Builder builder = new LatLngBounds.Builder();
///this is an example
LatLng latLng;// create var LatLng
for(int i=0;i<=10;i++)// you will do a type of cycle, tu add lat and long
{
latLng = new LatLng(lat, lon);// in this line put you lat and long
builder.include(latLng); //add latlng to builder
}
//create a lat long bounds
LatLngBounds bounds = builder.build();
//create a padding of the points and the camera of the map
int padding = 0; // offset from edges of the map in pixels
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
//finally move the camera, to zoom all the polylines
/// move the camera
googleMap.moveCamera(cu);
//or animate the camera...
googleMap.animateCamera(cu);
This is the link, for more information
Android map v2 zoom to show all the markers
if you see some error, or you have a better syntaxis, please explane me, I want to learn it.

how to open up a map of a particular city using google APIs in android

I have been able to open up a map of the world through code and mark a few places. However, I always have to zoom down to the city, which takes a little while. Is there any way to directly open up a map of a particular city?
You can use moveCamera with your last stored location (lat, lng and maybe zoomLevel too) for example.
final LatLng location = new LatLng(..., ...);// Loaded from SharedPreferences maybe.
// Move the camera to location with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 15));
You can use this sample code to animate the map directly to your city coordinate (lat, lng) and automatically zooming out to specified level :
// Set desired lat lng location, and zoom level (for example 10)
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(locationCity.getLatitude(), locationCity.getLongitude()), 10);
// Move the camera to location
gMap.animateCamera(cameraUpdate);

How to animateCamera at run time in Map V2

I am new in android, I am making application which have Google Map V2. I want to animateCamera Zoom value at runtime. It should be depend on the current location(latitude and longitude) and Destination Location(destination latitude and longitude).
It should be Zoom such it should show both marker as nearest possible and should not require any pan, zoom in/out gesture on mapv2.
I used this code but I need to pan gesture to see both market on the Map. I want it should be default as nearest possible and should not require any pan gesture.
map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 12.0f));
Thanks in advance.
you need to use newLatLngBounds()
Returns a CameraUpdate that transforms the camera such that the specified latitude/longitude bounds are centered on screen at the greatest possible zoom level. You can specify padding, in order to inset the bounding box from the map view's edges. The returned CameraUpdate has a bearing of 0 and a tilt of 0.
figure out the bounding box of the 2 points and give the southwest most point and the north east most point
example:
map.animateCamera(CameraUpdateFactory.newLatLngBounds(new LatLngBounds(southWest,northEast),10));
to get the bounding box you need to loop through your points and find the max/min lat/lng
example:
for(//for loop){
LatLng point = new LatLng(lat,lng)
if(point.getLatitude() > maxLat){
maxLat = point.getLatitude();
}
if(point.getLatitude() < minLat){
minLat = point.getLatitude();
}
if(point.getLongitude() > maxLon){
maxLon = point.getLongitude();
}
if(point.getLongitude() < minLon){
minLon = point.getLongitude();
}
}
northEast = new LatLng(maxLat,maxLon);
southWest = new LatLng(minLat,minLon);

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?

Categories

Resources