1

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.

enter image description here 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.

enter image description here

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));
}
glokul
  • 77
  • 1
  • 2
  • 10
  • Can you post the origin and destination coordinates that caused the output as in your second image? – Much Overflow Oct 06 '17 at 03:14
  • @Much Overflow Sure, both maps should be showing the same set of points, which are: `lat1=29.425745, lng1=-98.4861562` and `lat2=29.5215668, -98.5022129` – glokul Oct 07 '17 at 12:38
  • I added code showing how I am sending the coordinates to my map fragment. Hmm this is a real puzzler, if the points are accessible, I'm guessing it has something to do with the rendering of the map. Do I need to clear the map before loading it each time or what? I'll try and do some more research. – glokul Oct 08 '17 at 13:49
  • you may try with this.. used animateCamera instead of moveCamera... i.e. use mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 5)); – Amitabha Biswas Oct 08 '17 at 17:38
  • I've tried `animateCamera();` already, doesn't seem to make a difference. – glokul Oct 08 '17 at 17:52

0 Answers0