I am trying to set LatLangBound in map.When I give SouthWest LatLang & NorthEast Latlang of India, I am getting an error message that latitude of SouthWest > latitude of NorthEast (28.5827577 > 20.593684).
Is there any other way to display India regions when I start MapFragment.
My code snippet is.
private LatLngBounds India=new LatLngBounds(new LatLng(28.5827577,77.0334179),new LatLng(20.593684,78.96288));
mapView = (MapView) rootview.findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mMap = mapView.getMap();
try {
mMap.setMyLocationEnabled(true);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, latitude), 6));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(India.getCenter(),6));
CameraPosition cameraPosition =new CameraPosition.Builder().target(new LatLng(latitude,latitude)).zoom(2).bearing(0).tilt(90).build();
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}catch (Exception e) {
e.printStackTrace();
}
return rootview;
use the com.google.android.gms.maps.model.LatLngBounds.Builder
Adapted to your source code it should look something like this:
Builder builder = new LatLngBounds.Builder();
builder .include(currentLocationLatLng);
builder .include(targetLocationLatLng);
// pan to see all markers on map:
LatLngBounds bounds = builder .build();
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 3));
I have solved my problem at the end .
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude,longitude),7.0f));
It will move the camera focus at the given LatLng() and will zoom the map.
Related
I plotted a polyline in a Google Map and I want a marker in the polyline when the polyline is clicked. Then only the marker should appear in the middle of the polyline.
#Override
public void onPolylineClick(Polyline polyline) {
double latitude = polyline.getPoints().get(0).latitude;
double longitude = polyline.getPoints().get(0).longitude;
mMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(latitude, longitude))
.zoom(18).build();
}
With the code above the marker gets plotted only at the first point, but I need it in the middle of the polyline. How can I do that?
You can find the center of a polyline by getting the bound's center.
I haven't executed the code but I hope it will work fine.
public LatLng getPolylineCentroid(Polyline p) {
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for(int i = 0; i < p.getPoints().size(); i++){
builder.include(p.getPoints().get(i));
}
LatLngBounds bounds = builder.build();
return bounds.getCenter();
}
You can use the extrapolate function that I propose in this solution: How can I extrapolate farther along a road?
double middleDistance = SphericalUtil.computeLength(polyline.getPoints());
LatLng markerPosition = extrapolate(polyline.getPoints(), polyline.getPoints().get(0), middleDistance);
mMap.addMarker(new MarkerOptions().position(markerPosition).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(markerPosition)
.zoom(18).build();
Hi guys my problem is about mapView. I want to set the camera to a new position but when i call SetNewPosition the log tell to me that coordinates are changed but the mapview on the screen shows always the same place.
public void SetNewPosition(double log,double lat)
{
Log.i("Current position",""+mappa.getCameraPosition());
LatLng latLng= new LatLng(log, lat);
CameraUpdate cameraUpdate= CameraUpdateFactory.newLatLng(latLng);
mappa.moveCamera(cameraUpdate);
mappa.clear();
map.invalidate();
map.postInvalidate();
Log.i("Changed position",""+mappa.getCameraPosition());
}
Can anyone tell where is my error?
This is what you may want to use :
LatLng latLng = new LatLng (log, lat);
LatLngBounds.Builder builder = new LatLngBounds.Builder ();
builder.include (latLng);
LatLngBounds bounds = builder.build ();
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds (bounds, 0);
googleMap.animateCamera (cameraUpdate);
how can i set the zoom level of a SupportMapFragment ?
I tried some answers I found in similar questions, like this one answered buy a guy named Rob who proposed:
LatLng currentLocation = mService.getCurrentLocation();
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(currentLocation) // Sets the center of the map to the current location of the user
.zoom(17) // Sets the zoom
.bearing(90) // Sets the orientation of the camera to east
.tilt(30) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
But this solution didn't work. So, how can I set the Zoom level knowing that I use a SupportMapFragment.
if your current location is not null then try this code it will work
LatLng currentLocation = mService.getCurrentLocation();
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(currentLocation , 16);
mMap.addMarker(new MarkerOptions().position(location).title(""));
mMap.moveCamera(CameraUpdateFactory.newLatLng(currentLocation ));
mMap.animateCamera(cameraUpdate)
Marker currentMarker;
MarkerOptions marker = new MarkerOptions().position(new
LatLng(Double.
parseDouble(lat),Double.parseDouble(lon))).title(title);
marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_schoolbus));
currentMarker = map.addMarker(marker);
currentMarker.showInfoWindow();
currentMarker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.ic_schoolbus));
map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(Double.parseDouble(lat),Double.parseDouble(lon)), 15));
Can someone explain how I can use the Google maps API in my project?
Cause I want to draw a marker on the map while moving the map with my finger.
But I don't know how to achieve this.
I am using the following code:
camCenter = mMap.getCameraPosition().target;
if (camCenter != null && camCenter != camCenterFirst) {
CameraUpdate center =
CameraUpdateFactory.newLatLng(new LatLng(camCenter.latitude,camCenter.longitude));
CameraUpdate zoom = CameraUpdateFactory.zoomTo(16);
mMap.moveCamera(center);
mMap.animateCamera(zoom);
camCenterFirst = camCenter;
}
First you have to add a marker to your Map like this:
LatLng pos = new LatLng(53.557115, 10.023159);
Marker m = mMap.addMarker(new MarkerOptions().position(pos).title(""));
The example above, gives you a default position of the marker.
Then you can change the position of the marker with following command:
m.setPosition(new LatLng(latitude,longitude));
Hope this helps!
public void showOnMap(Double lat,Double lng,String place)
MarkerOptions marker = new MarkerOptions().position(new LatLng(lat,
lng)).title(""+place);
mMap.addMarker(marker);
CameraPosition cameraPosition = new CameraPosition.Builder().target(
new LatLng(lat, lng)).zoom(12).build();
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
double radiusInMeters = 5000.0;
int strokeColor = 0xffff0000; //red outline
int shadeColor = 0x44ff0000; //opaque red fill
CircleOptions circleOptions = new CircleOptions().center(new LatLng(lat,lng)).radius(radiusInMeters).fillColor(shadeColor).strokeColor(strokeColor).strokeWidth(8);
mMap.addCircle(circleOptions);
}
I have done a complete demo of what you are looking for,
Check this Github code
https://github.com/cseshaiban/GeoApp
I'm sorry for my bad english,
I want to display the path between two markers and do an zoom out or in to display also the origin marker and the destination marker.
Thank you.
polyline = mMap.addPolyline(lineOptions);
polyline.isVisible();
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(origin);
builder.include(destination.getPosition());
LatLngBounds bounds = builder.build();
mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds,20));
Try this link for display maps and use following code for zooming:
http://www.androidhive.info/2013/08/android-working-with-google-maps-v2/
GoogleMap googleMap;
----
----
CameraPosition cameraPosition = new CameraPosition.Builder().target(
new LatLng(17.385044, 78.486671)).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));