My map fragment seems to be having some issues. I'm calling it in my 2nd activity from the first which is my custom adapter set to a recyclerView
.
I am adding 2 markers to the map from coordinates that change depending on the item clicked in the adapter.
What I want to happen is for the camera to move the map to show each pair of coordinates with padding around them, as in the image below.
The problem is that it only appears this way maybe 10% of the time. The rest of the time the map is zoomed all the way out like in the below image.
Am I not building my bounds or moving the camera correctly or getting the coordinates correctly or what? I can't figure it out. Any ideas what I am doing wrong? This is how I am sending the lat/lng from my adapter:
holder.routeinfo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view){
Intent intent = new Intent(context, RouteDetailsActivity.class);
intent.putExtra("lat1", routeList.get(position).getLat1());
intent.putExtra("lng1", routeList.get(position).getLng1());
intent.putExtra("lat2", routeList.get(position).getLat2());
intent.putExtra("lng2", routeList.get(position).getLng2());
context.startActivity(intent);
}
});
And here is my onMapReady()
from my RouteDetailsActivity
:
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
Double lat1 = getIntent().getExtras().getDouble("lat1");
Double lng1 = getIntent().getExtras().getDouble("lng1");
Double lat2 = getIntent().getExtras().getDouble("lat2");
Double lng2 = getIntent().getExtras().getDouble("lng2");
//Origin Marker
LatLng origin = new LatLng(lat1, lng1);
mMap.addMarker(new MarkerOptions()
.position(origin)
.title("Origin"));
//Destination Marker
LatLng destination = new LatLng(lat2, lng2);
mMap.addMarker(new MarkerOptions()
.position(destination)
.title("Destination"));
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(origin);
builder.include(destination);
LatLngBounds bounds = builder.build();
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 5));
}