简体   繁体   中英

zooming marker position according to current location android

I working with google maps in Android. I am stuck with adjusting zoom level.

I have one marker in center of map that i set by using mapController.setCenter(current_location);

And I have 10 or more marker showing in map with different color.

Expected Result: current location of person should in center always and I have to adjust a zoom level so that green marker will show in screen( Current location marker in center).

What I tried:

int greenlat = (int) (AppCoordinates.getInstance().latitudelist.get(0) * 1E6); int greenlong = (int) (AppCoordinates.getInstance().longitudelist.get(0) * 1E6); int currentlat = (int) (AppCoordinates.getInstance().getCurrentLatitude() * 1E6); int currentlong = (int) (AppCoordinates.getInstance().getCurrentLongitude() * 1E6);

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);

        mapController.zoomToSpan((int) (Math.abs(greenlat - currentlat) * 1), (int)(Math.abs(greenlong - currentlong) * 1));// to have some padding on the edges.
        mapController.animateTo(new GeoPoint((greenlat + currentlat), (greenlong + currentlong)/2));

With is I am able to show booth the markers in screen, but I want cureent location to be shown in center of screen.

My idea is to manipulate LatLngBounds since its representing all included markers to be viewed in aligned rectangle on the map. So we just need to recognise all points that make current location will be viewed at the center of map.

  1. Put all markers into the bound.
  2. Move camera to your current location as center point.
  3. Get 4 locations of all corners of the current map and put it together in list of point in the bound.

this is my sample:

LatLngBounds.Builder builder = new LatLngBounds.Builder();
Marker myLocation = mMap.addMarker(new MarkerOptions()
        .position(new LatLng(2.6,101.5))
        .title("My-Current-Location"));


Marker addMarker1 = mMap.addMarker(new MarkerOptions()
            .position(new LatLng(2.6,101))
            .title("Hello world-1"));

Marker addMarker2 = mMap.addMarker(new MarkerOptions()
            .position(new LatLng(2.7,101))
            .title("Hello world-2"));

Marker addMarker3 = mMap.addMarker(new MarkerOptions()
            .position(new LatLng(3.7,105))
            .title("Hello world-3"));

builder.include(myLocation.getPosition());
builder.include(addMarker1.getPosition());
builder.include(addMarker2.getPosition());
builder.include(addMarker3.getPosition());

//haha enough to use for loop for all your markers

int padding = 100; // offset from edges of the map in pixels

CameraUpdate cameraUpdate =CameraUpdateFactory.newLatLngBounds(builder.build(), padding);
mMap.moveCamera(cameraUpdate);
mMap.moveCamera(CameraUpdateFactory.newLatLng(myLocation.getPosition()));

LatLng nrLatLng = mMap.getProjection().getVisibleRegion().nearRight;
LatLng frLatLng = mMap.getProjection().getVisibleRegion().farRight;
LatLng nlLatLng = mMap.getProjection().getVisibleRegion().nearLeft;
LatLng flLatLng = mMap.getProjection().getVisibleRegion().farLeft;

builder.include(nrLatLng);
builder.include(frLatLng);
builder.include(nlLatLng);
builder.include(flLatLng);

cameraUpdate =CameraUpdateFactory.newLatLngBounds(builder.build(), padding);
mMap.animateCamera(cameraUpdate);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM