I try to plot two location using this tutorial(http://wptrafficanalyzer.in/blog/driving-route-from-my-location-to-destination-in-google-maps-android-api-v2/) . But it work . Problem is it not zoom plot path.
I want to out put like this
But give the result like this
1) why this path not zoom ?
2) how to zoom the path?
You can use Latlngbounds to zoom your path using following code
Builder boundsBuilder = new LatLngBounds.Builder();
boundsBuilder.include(currentLocationLatLng);
boundsBuilder.include(targetLocationLatLng);
LatLngBounds bounds = boundsBuilder.build();
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 3));
or if you want to animate this too
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
googleMap.moveCamera(cu);
googleMap.animateCamera(cu);
Related
I'm new in this programming language so I kinda copy and paste the codes found in the net and then I've found and tried using these codes in bounding a certain area and it works:
private GoogleMap mMap;
// Create a LatLngBounds that includes the city of Adelaide in Australia.
private LatLngBounds ADELAIDE = new LatLngBounds(
new LatLng(-35.0, 138.58), new LatLng(-34.9, 138.61));
// Constrain the camera target to the Adelaide bounds.
mMap.setLatLngBoundsForCameraTarget(ADELAIDE);
But when I changed it using my campus coordinates into like this:
private LatLngBounds CPUBounds = new LatLngBounds(
new LatLng(10.732581, 122.548413), new LatLng(10.730052, 122.549958));
My app suddenly stop working. What should I do?
Try to paste the Logcat to review the Error
From If you want to show the specific position try the following code
To Move Camera Position Over a Specific Region Use this ,
mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds,
zoomWidth,
zoomHeight,
zoomPadding));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(10.732581, 122.548413),
zoomHeight));
If u want to add a marker Over region use
mMap.addMarker(new MarkerOptions()
.position(new LatLng(10.732581, 122.548413))
.title("campus name"));
To Disable the Dragging Use
mMap.getUiSettings().setScrollGesturesEnabled(false);
but it doesn't disable map zoom by touch on map. for that use Following Code
googleMap.getUiSettings().setZoomGesturesEnabled(false);
I have a GoogleMap (Lite Mode) object that has been properly initialized. It works properly with a single Marker and focuses on the proper area using CameraUpdate with newLatLngZoom.
However, when it comes to adding two markers, and have the map keep both in view using LatLngBounds, everything goes bonkers. The map is so zoomed out it shows the entire continent, and the two markers are on-top of each other (along with the polyline).
Kindly assume that the data from the pickup and destination is correct
Here's my code:
googleMap.addMarker(new MarkerOptions()
.position(pickup)
.title(job.getPickupAddress()));
googleMap.addMarker(new MarkerOptions()
.position(destination)
.title(job.getDestinationAddress())
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(new LatLngBounds.Builder().include(pickup).include(destination).build(), 0);
googleMap.moveCamera(cameraUpdate);
String pickupLatLng = job.getPickupLatLong().latitude + "," + job.getPickupLatLong().longitude;
String destinationLatLng = job.getDestinationLatLong().latitude + "," + job.getDestinationLatLong().longitude;
plotPolyLines(pickupLatLng, destinationLatLng);
Output:
Setting a zoom with latlng bounds is not an effective solution for all use cases.
Might be a related Bug Maps Lite mode with incorrect zoom level first time app starts
Try few solutions / hacks listed here : https://issuetracker.google.com/issues/36218443
Like refreshing the map by destroying and recreating
Also check if by using a single marker with bounds builder if the map sets to the proper zoom level
One last thing make sure that zoom level limits have not been applied.
try changing the line
googleMap.moveCamera(cameraUpdate);
to
googleMap.moveCamera( CameraUpdateFactory.newLatLngZoom(new LatLng(xxxx,xxxx) , 14.0f) );
as suggested in How do I set default location and Zoom level for google map api v2?
or use
map.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
That should move the camera closer to your marker.
My polyline Going out of focus. I'm using map.moveCamera(CameraUpdateFactory.newLatLngZoom(route.startLocation, 10));
but this is not a solution. How to do slove this issue
To solve your problem, lets first understand how the google map camera works. When you do:
map.moveCamera(CameraUpdateFactory.newLatLngZoom(route.startLocation, 10));
The camera focuses the startlocation in the middle with 10 as the zoom level. What we need is to focus the route. For this we need to use the latLng bounds. Once we create a bound with two or more latLngs, we can set it to the camera of the map. This way your route is visible to your users.
Create a bound with all the start and end markers on your route:
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(startMarker);
builder.include(endMarker);
LatLngBounds bounds = builder.build();
Then obtain a movement description object by using the factory: CameraUpdateFactory:
int padding = 0; // padding around start and end marker
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
googleMap.animateCamera(cu);
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 am implemented Google map v2 api in my android application. Here, in one activity I successfully init google map object. I successfully added five marker into the map and also current location that is fine.
Now, I want to see all these markers into the current screen using LatLngBounds.
Code is given below.
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for(Marker m : markers) {
builder.include(m.getPosition());
}
LatLngBounds bounds = builder.build();
mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 50));
but when I implement this there is no effect into map.
I have solved the problem with adding markers to the map before:
mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 50));
I just added:
_map.addMarker(nearestMarker);
_map.addMarker(_myLocationMarker);
My code looks like this:
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, 100, 100, 5);
_map.addMarker(nearestMarker);
_map.addMarker(_myLocationMarker);
_map.animateCamera(cu);
Hope this will help you.