ErenisR
ErenisR

Reputation: 80

Set CameraPosition to my location, but show other markers also

I am designing an Android app that should show multiple markers on map. I can do that easily. My problem is that I need to show all the markers around my position, and the CameraPosition needs to be in focused in my position. I can easily do each on them separately, but what I need is showing them together, my location in the middle of the map, and the markers around my location. Any idea how can I achieve that? Thanks in advance!

Upvotes: 1

Views: 589

Answers (2)

Sagar Nayak
Sagar Nayak

Reputation: 2218

The Functionality you need is to keep all the selected markers on the maps and it it should be visible at a single time. you have to keep all those points in the visible region with myLlocation (your current location) at the center of the screen always.

  • First keep your myLocation inside an latlng variable and all the other latlng which are to be inside the bounds inside another ArrayList of LatLng. Also define some variables to keep track of operation.

    LatLng myLocation=null;
    ArrayList<LatLng> latlngs = null; // the arraylist of other latlng;
    boolean are_all_inside_viewport = false;
    float current_zoom = googleMap.getCameraPosition().zoom;
    //initialise these variables myLocation and latlngs;
    
  • Then animate your camera to myLocation which will be placed into the center of the map. put a low zoom level here.

    CameraPosition cameraPosition = new CameraPosition.Builder()
                .target(myLocation)
                .zoom(14)
                .build();
    
    googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
    
  • Now check for each latlng in the ArrayList of they are in the google maps view port. if they are inside then its ok. if not then decrease a zoom level of the maps and animate the camera again.

    while (!are_all_inside_viewport) {
            are_all_inside_viewport = true;
            for (LatLng latLng : latlngs) {
                if (!bounds.contains(latLng)) {
                    are_all_inside_viewport = false;
                    break;
                }
            }
    
            bounds = googleMap_local.getProjection().getVisibleRegion().latLngBounds;
    
            cameraPosition = new CameraPosition.Builder()
                    .target(myLocation)
                    .zoom(current_zoom - 1)
                    .build();
            googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
        }
    

    This will go on checking for each latlng inside the ArrayList until all the latlng are inside the viewport.

Hope this answer helps you.

Upvotes: 2

Luke Villanueva
Luke Villanueva

Reputation: 2070

Set your zoom to a higher level so it can zoom out and see more of your map around your current location.

CameraPosition cameraPosition = new CameraPosition.Builder()
                                .target(yourLatLng).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

Upvotes: 3

Related Questions