Animating Camera with bounds returns nullPointerException - android

I am develepoing an app that needs to view various map points at the same time. I am trying to animate camera with bounds and Builder, but it's returning null every time, and I checked the coordinates and the .build() and all of them have data. this is the code.
LatLngBounds.Builder builderMap = new LatLngBounds.Builder();
LocationManager locationManager = (LocationManager) getActivity().getSystemService(getActivity().LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, false);
android.location.Location location = locationManager.getLastKnownLocation(bestProvider);
if(location!=null)
{
Log.i("MapACtivity","builder includes myLocation");
builderMap.include(new LatLng(location.getLatitude(),location.getLongitude()));
coordinate_bounds= coordinate_bounds +1;
}
for(CircleContact myContact : newCircle.getMembers())
{
if(myContact.getLatitude()!=0 && myContact.getLongitude()!=0)
{
Log.i("MapACtivity","builder includes locations " + myContact.getDevice());
Log.i("MapACtivity","Lat " + myContact.getLatitude()+ " longitude" +myContact.getLongitude());
builderMap.include(new LatLng(myContact.getLatitude(),myContact.getLongitude()));
coordinate_bounds= coordinate_bounds +1;
}
}
if(coordinate_bounds>1)
{
Log.i("CircleFragment","builderMap.build()" + builderMap.build().getCenter());
Log.i("CircleFragment","builderMap.build()" + builderMap.build().northeast);
Log.i("CircleFragment","builderMap.build()" + builderMap.build().southwest);
mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(builderMap.build(), 50));
}
The log that I get is :
builderMap.build()lat/lng: (-11.075408249999999,-75.85188305)
builderMap.build()lat/lng: (-10.078819,-74.748112)
builderMap.build()lat/lng: (-12.0719975,-76.9556541)
But it's returning null in this line:
mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(builderMap.build(), 50));
and it is not the map because, before I update the camera to my location, and that doesn't crash, this is the code before trying to get all positions inside the map and it is working fine:
MapView mMapView;
mMapView = (MapView)getActivity().findViewById(R.id.map_locations);
mMap = mMapView.getMap();
mMap.addMarker(myMarker);
CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(Location, 15);
mMap.animateCamera(yourLocation);

This comes from the official google maps android page:
Note: Only use the simpler method newLatLngBounds(boundary, padding) to generate a CameraUpdate if it is going to be used to move the camera after the map has undergone layout. During layout, the API calculates the display boundaries of the map which are needed to correctly project the bounding box. In comparison, you can use the CameraUpdate returned by the more complex method newLatLngBounds(boundary, width, height, padding) at any time, even before the map has undergone layout, because the API calculates the display boundaries from the arguments that you pass.
And this is how they are setting bounds:
private GoogleMap mMap;
private LatLngBounds AUSTRALIA = new LatLngBounds(
new LatLng(-44, 113), new LatLng(-10, 154));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(AUSTRALIA.getCenter(), 10));

Related

How to move android google map camera with constant velocity and zoom

I cannot find the way to move android google map camera with constant velocity and zoom.
For example, if the current zoom is 15, and calling
CameraPosition pos= CameraPosition
.builder(map.mMap.getCameraPosition())
.target(target)
.zoom(15)
.build();
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(pos), duration * 1000, null);
it modifies the zoom 15 - > 12 -> 15
(the mid value depends on the distance)
My target is to show an object moving on the map with constant velocity and always keep it in the center of the map
Tried to call moveCamera in the ValueAnimator of the object, but then the map jumps. I need to move it smoothly
Use fused API for location update and move the camera to the updated location
mFusedLocationClient.getLastLocation()
.addOnSuccessListener(MainActivity.this, new OnSuccessListener<android.location.Location>() {
#Override
public void onSuccess(android.location.Location location) {
if(location!=null){
try{
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
Geocoder geocoder = new Geocoder(MainActivity.this, Locale.getDefault());
String addresses = geocoder.getFromLocation(location.getLatitude(),
location.getLongitude(), 1).get(0).getAddressLine(0);
marker_origin = new MarkerOptions().
position(latLng).title(addresses)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
mMap.addMarker(marker_origin);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 16.0f));
getPlacesData(latLng, "restaurant");
}catch (Exception e){
e.printStackTrace();
}
}
}
});
For further details check the android developer documentation
https://developer.android.com/training/location/receive-location-updates.html

Osmdroid maximum zoom level displaying two geo locations

I am using osmdroid library to render maps. I have two geo points that vary intermittently, and want the org.osmdroid.views.MapView to resize and translate making sure both points are always visible. I can easily re-centre the map on the point mid-way between the two, but how do I set the zoom level to ensure my two points are visible?
UPDATE
//Getting pickup and destination latitude and longitude
GeoPoint pickupLocation = null;
if (tripRequest.getPickupLat() != null && tripRequest.getPickupLong() != null) {
pickupLocation = new GeoPoint(tripRequest.getPickupLat(), tripRequest.getPickupLong());
}
GeoPoint destinationLocation = null;
if (tripRequest.getDestinationLat() != null && tripRequest.getDestinationLong() != null) {
destinationLocation = new GeoPoint(tripRequest.getDestinationLat(), tripRequest.getDestinationLong());
}
if (destinationLocation != null && pickupLocation != null) {
//Adding destination marker to map
Marker destinationLocationMarker = new Marker(map);
destinationLocationMarker.setPosition(destinationLocation);
destinationLocationMarker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
destinationLocationMarker.setIcon(context.getResources().getDrawable(R.mipmap.pin_green_small));
map.getOverlays().add(destinationLocationMarker);
//Adding pickup marker to map
Marker pickupLocationMarker = new Marker(map);
pickupLocationMarker.setPosition(pickupLocation);
pickupLocationMarker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
pickupLocationMarker.setIcon(context.getResources().getDrawable(R.mipmap.pin_red_small));
map.getOverlays().add(pickupLocationMarker);
BoundingBox boundingBox = BoundingBox.fromGeoPoints(Arrays.asList(destinationLocation, pickupLocation));
mapController = map.getController();
mapController.setCenter(boundingBox.getCenter());
mapController.setZoom(10);// I need to set this zoom properly
touchListener = new TouchListener();
map.setOnTouchListener(touchListener);
Use zoomToBoundingBox. It should work and if it doesn't problem will be elsewhere.
Try:
map.zoomToBoundingBox(boundingBox, false);
map.invalidate();
It this still don't work, check this response:
zoom mapView to a certain bounding box on osmdroid
If you really want to calculate zoom yourself you can always find an inspiration in source code of zoomToBoundingBox method and modify the calculation to fit your needs.
I believe your are looking for zoom to bounds which is in mapview or map controller. Use the max/min lat lon from your points. You'll want to test at the date line and equator

How to Zoom and get latitude and longitude from mMap.setMyLocationEnabled(true) in Google Map (Android)

I was setting mMap.setMyLocationEnabled(true) and I am successfully getting my current location but...
Not able to zoom to that position.
Not able to get lat and lng from that position
From getting latitude and longitude I am using below code but it gives me null. I am calling below method from onMapReady(Google Map) and passing google map object.
private void goToCurrentLocation(GoogleMap map) {
Log.d("Tag", "inside");
Location loc = map.getMyLocation();
if (loc != null) {
LatLng point = new LatLng(loc.getLatitude(), loc.getLongitude());
Log.d("Tag=", "latlng=" + point);
map.animateCamera(CameraUpdateFactory.newLatLngZoom(point, 15));
}
}
Is there any alternative to getting latitude and longitude as getMyLocation deprecated?
use link provided in the answer to get current location and then zoom over that location using below code:
latLng = new LatLng(location.getLatitude(), location.getLongitude());
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 10);
mGoogleMap.animateCamera(cameraUpdate);
http://blog.teamtreehouse.com/beginners-guide-location-android

How to apply Camera bounds with CameraPosition in google map

I need to use few features of both CameraPosition and CameraBounds, Like my specific requirement is to move the camera on a bearing and tilt it on a few degrees as well as there are other markers on the map which i want to cover while i perform a camera update .
CameraUpdateFactory class have two different methods on two different purpose.
1) newCameraPosition 2) newLatLngBounds
But i doubt if i can use both of them simultaneously to get the result i want.
CameraPosition works as below :
LatLng current_lat_lng=new LatLng(lat, lon);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(current_lat_lng) // Sets the center of the map to Mountain View
.zoom(googleMap.getCameraPosition().zoom) // Sets the zoom
.bearing((float)mapHeading) // Sets the orientation of the camera to a bearing
.tilt(0) // Sets the tilt of the camera to 0 degrees
.build();
googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
CameraBounds work as below :
final LatLngBounds.Builder builder = new LatLngBounds.Builder();
if (marker_Searched != null) {
builder.include(marker_Searched.getPosition());
}
if (marker_selected_taxi != null) {
builder.include(marker_selected_taxi.getPosition());
}
LatLngBounds bounds = builder.build();
int padding = 300; // offset from edges of the map in pixels
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
googleMap.moveCamera(cu);
Do anyone have tried any workaround with this ?
Animate your camera to the LatLngBounds with CancellableCallback, then in onFinish get zoom value (map.getCameraPosition().zoom) then apply this value into CameraPosition. It works for me fine.

Android: Google maps v2, zoom is not working

Programmatically changing camera zoom is not working for me. Take look at the code:
public void moveToMyLocation(GoogleMap map)
{
Criteria crit = new Criteria();
String provider = locationManager.getBestProvider(crit, true);
Location loc = locationManager.getLastKnownLocation(provider);
LatLng latlng=new LatLng(loc.getLatitude(),loc.getLongitude());
float czoom = map.getCameraPosition().zoom; // eq 2.0
map.moveCamera(CameraUpdateFactory.newLatLng(latlng));
map.animateCamera(CameraUpdateFactory.newLatLngZoom(latlng,(float) 19.6));
czoom = map.getCameraPosition().zoom; // STILL eq 2.0!!
}
I can still zoom map using default controls.
m.
This is what worked for me:
map.animateCamera(CameraUpdateFactory.newLatLng(
LatLng(it.position.latitude, it.position.longitude)))
Handler().postDelayed({
map.animateCamera(CameraUpdateFactory.zoomIn())
}, 500)
you can use this to zoom directly without the animation :
map.moveCamera( CameraUpdateFactory.newLatLngZoom(new LatLng(xxxx,xxxx) , 14.0f) );

Categories

Resources