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);
Related
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.
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);
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've been trying to work out a method for my application to indicate when there are markers on the map that are outside of the current view/screen/bound/VisibleRegion.
For example, if there are current 5 markers on the map but the user is zoomed into a part of the map where they can't see any of the markers then I would like to be able to flag up to the user that there are markers on the map that they cannot see or something along those lines (it would be nice to be able to indicate in which direction the markers are from the current position).
I thought of using
LatLngBounds currentScreen = googleMap.getProjection()
.getVisibleRegion().latLngBounds;
but this would only be able to tell me which markers are in the current visibleRegion.
any help would be appriciated, thanks.
First, you should save all your markers somewhere. For example, in list
List<Marker> markers = new ArrayList<>();
Marker marker = mMap.addMarker(new MarkerOptions().position(new LatLng(55.123, 36.456)));
markers.add(marker);
After this you can check, are there markers outside your screen
LatLngBounds currentScreen = mMap.getProjection().getVisibleRegion().latLngBounds;
for(Marker marker : markers) {
if(currentScreen.contains(marker.getPosition())) {
// marker inside visible region
} else {
// marker outside visible region
}
}
I have lat&long values from database.how to display markers based on lat &long values using android google map api v2.In original android google maps,we display markers based on concept itemoverlay. In v2,I dont know how to display markers.
dbAdapter.open();
Cursor points = dbAdapter.clustercall(Btmlft_latitude, toprgt_latitude,
Btmlft_longitude, toprgt_longitude, gridsize1);
int c = points.getCount();
Log.d("count of points", "" + c);
if (points != null) {
if (points.moveToFirst()) {
do {
int latitude = (int) (points.getFloat(points
.getColumnIndex("LATITUDE")) * 1E6);
int longitude = (int) (points.getFloat(points
.getColumnIndex("LONGITUDE")) * 1E6);
mapView.addMarker(new MarkerOptions().position(
new LatLng(latitude, longitude)).icon(
BitmapDescriptorFactory.defaultMarker()));
} while (points.moveToNext());
}
}
points.close();
dbAdapter.close();
That is mycode.I am getting lat&long values from database,but how to add markers based on lat &long values to map.
I read the android google maps api v2.In that have only give statically added data of markers
Use the Marker Class
An icon placed at a particular point on the map's surface. A marker icon is drawn oriented against the device's screen rather than the map's surface; i.e., it will not necessarily change orientation due to map rotations, tilting, or zooming.
A marker has the following properties:
Anchor
The point on the image that will be placed at the LatLng position of the marker. This defaults to 50% from the left of the image and at the bottom of the image.
Position
The LatLng value for the marker's position on the map. You can change this value at any time if you want to move the marker.
Title
A text string that's displayed in an info window when the user taps the marker. You can change this value at any time.
Snippet
Additional text that's displayed below the title. You can change this value at any time.
Icon
A bitmap that's displayed for the marker. If the icon is left unset, a default icon is displayed. You can specify an alternative coloring of the default icon using defaultMarker(float). You can't change the icon once you've created the marker.
Drag Status
If you want to allow the user to drag the marker, set this property to true. You can change this value at any time. The default is true.
Visibility
By default, the marker is visible. To make the marker invisible, set this property to false. You can change this value at any time.
GoogleMap map = ... // get a map.
// Add a marker at San Francisco.
Marker marker = map.addMarker(new MarkerOptions()
.position(new LatLng(37.7750, 122.4183))
.title("San Francisco")
.snippet("Population: 776733"));
You have to use the method GoogleMap.addMarker(MarkerOptions);
I've explained this in a tutorial on my blog:
http://bon-app-etit.blogspot.be/2012/12/add-informationobject-to-marker-in.html
On this blog, you'll find some more posts concerning the new Maps API:
Move the map to current or some location
Create the custom InfoWindow
Associate geodata or objects to a marker
I'm guessing the issue is that you are multiplying your latitude/longitude values by 1E6 instead of dividing them by 1E6. If your stored values came from GeoPoints, that might be your issue. Otherwise, the way you are adding the Markers is fine and should work.
i.e. the lat/lng values you pass to new LatLng() should look like 45.4,-95.5 NOT 45400000,-95500000
Check out the following code:
//mMap is a GoogleMap and point is a GeoPoint
this.mMap.addMarker(getMarker(point, drawableId))
...
public MarkerOptions getMarker(GeoPoint point, int drawableId)
{
return new MarkerOptions()
.position(new LatLng(point.getLatitudeE6() / 1000000.0, point.getLongitudeE6() / 1000000.0))
.icon(BitmapDescriptorFactory.fromResource(drawableId));
}