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);
Related
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.
I would like to force the camera to behave like you are using navigation, it means when you rotate 90° left, the camera does the same thing.
I have a Google Map where my location (as a blue dot) is shown.
mGoogleMap.setMyLocationEnabled(true);
When I am moving, blue dot is with an arrow which shows my current bearing. I would like to get this bearing.
I am getting my current location using FusedLocationApi:
currentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
Code below is animating camera to current location, but without the bearing. I can add the bearing, but I don't know the value.
#Override
public void onLocationChanged(Location location) {
LatLng latLng = new LatLng( location.getLatitude(), location.getLongitude() );
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
}
Do you know how to get the current location direction? I was unable to find any proper information about that. I would greatly appreciate any help you can give me.
Thank you
I think you can try to use getOrientation(R, orientation) (and adjust for true north) to get the device rotation. You can refer to here.
Also, use the CameraPosition class here to adjust camera position.
EDIT: I even found better solution. Use getBearing() method in Location class.
if (location.hasBearing()) {
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(latLng) // Sets the center of the map to current location
.zoom(15) // Sets the zoom
.bearing(location.getBearing()) // Sets the orientation of the camera to east
.tilt(0) // Sets the tilt of the camera to 0 degrees
.build(); // Creates a CameraPosition from the builder
mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
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
I'm working on an application that uses google maps. Can I get the geographic location of a particular point on the screen?.For example, I have a view (button, image, etc) with a X and Y position on the screen. What I want to know is the geographic location (latitude and longitude) of that position (view position).
Of course this position (X and Y) is on the map.
I found the solution. This is how:
Projection projection = googleMap.getProjection();
// Returns the geographic location that corresponds to a screen location
LatLng geographicalPosition = projection.fromScreenLocation(new Point(your_X, your_Y));
// Returns a screen location that corresponds to a geographical coordinate
Point screenPosition = projection.toScreenLocation(new LatLng(your_latitude, your_longitude));
More information here.
In short yes(your questions lacks details and your own attempt), I would drop a marker on the screen at my current location and set the marker property to draggable. This will allow me to move the marker to any position on the map
For example,
private GoogleMap mMap;
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
mMap.addMarker(new MarkerOptions()
.position(new LatLng(Your_Lat, Your_Lng))
.title("My Marker")
.draggable(true));
After placing it anywhere you want you can retrieve the markers new Latitude and Longitude by simply using the position function. Something like this.
I want to perform a zoom on a region in google map
map.animateCamera(CameraUpdateFactory.newLatLngBounds(new LatLngBounds(southwest, northeast), padding)
the problem is that i dont have: southwest and northeast points
I just have the center of the map (LatLnt) and the distance in meters between southwest and northeast,
I'd like to know how to calculate southwest and northeast points from map_center and the distance?
any help is welcome
thank you in advance.
This answer assumes that you are using a square map. In other words that the angle from the starting point is 45 degrees (also assuming that the map is flat which means the distances are small). I am also assuming that you are not at either of the poles (yeah I know duh!).
Ok with that in mind it is simple geometry. Since you know the start point and the length from the Southwest corner to the Northeast corner I use half this distance as that is the distance from the starting point to each of these points. I am then finding the x and y component (which is the same with a square map) of this "half distance". To do this I just use the formula latlongOffset equals half of the "half distance" times the square root of 2.
the code is as follows (you provide a LatLng startPoint, Double someDistance, and int padding):
LatLng southwest, northeast;
Double swLat, neLat, swLng, neLng;
Double halfSomeDistance = someDistance / 2.0;
Double latlongOffset = halfSomeDistance * Math.sqrt(2.0) / 2.0;
swLat = startPoint.latitude - latlongOffset;
swLng = startPoint.longitude - latlongOffset;
neLat = startPoint.latitude + latlongOffset;
neLng = startPoint.longitude + latlongOffset;
southwest = new LatLng(swLat, swLng);
northeast = new LatLng(neLat, neLng);
map.animateCamera(CameraUpdateFactory.newLatLngBounds(new LatLngBounds(southwest, northeast), padding));
If you are not using a square map you will need to use right triangle geometry to get the latOffset and lngOffset as they will not be the same.
I hope this helps.