How to get center of map for v2 android maps?
Asked Answered
G

6

82

In Google Maps for Android v1, MapView had a convenience method: getMapCenter(). Now I cannot figure out how to get map center with v2 of this api. I have perused the API documentation, but there is no mention of such a feature. Please advise.

Thanks, Igor

Gyniatrics answered 16/12, 2012 at 18:59 Comment(0)
D
201

I had the same problem. It seems you can get the center this way:

mMap.getCameraPosition().target

where mMap is the GoogleMap instance from your activity. This will return a LatLng object which basically represents the center of the map.

Note that the GeoPoint class is not available anymore.

According to http://developer.android.com/reference/com/google/android/gms/maps/model/CameraPosition.html

target is "The location that the camera is pointing at." (I tested it with the sample code and it worked okay for me)

Dias answered 18/12, 2012 at 11:34 Comment(3)
Thanks, I didn't realize that GeoPoint was unavailable in v2. I was gonna use the VisibleRegion object to calculate the middle point between the four corners of the map display.Gyniatrics
@Radu Comaneci How to do the same with html javascript? Any ideaLaxity
Good answer, but this gets affected if im using a padding for example to move the copyright google sign, so the center of the map will get this padding value as wellOverturn
C
35

I have found two ways of do this:

1) The easiest, The first is using the target property in the Map's CameraPosition Object

 LatLng center = mMap.getCameraPosition().target;

2) The second is using a VisibleRegion object:

VisibleRegion visibleRegion = mMap.getProjection()
                    .getVisibleRegion();

Point x = mMap.getProjection().toScreenLocation(
                    visibleRegion.farRight);

Point y = mMap.getProjection().toScreenLocation(
                    visibleRegion.nearLeft);

Point centerPoint = new Point(x.x / 2, y.y / 2);

LatLng centerFromPoint = mMap.getProjection().fromScreenLocation(
                    centerPoint);

I have compared both answers:

Log.d("MapFragment: ", "Center From camera: Long: " + center.longitude
                        + " Lat" + center.latitude);

Log.d("Punto x", "x:" + x.x + "y:" + x.y);
Log.d("Punto y", "y:" + y.x + "y:" + y.y);

Log.d("MapFragment: ", "Center From Point: Long: "
                    + centerFromPoint.longitude + " Lat"
                    + centerFromPoint.latitude);
Convertiplane answered 19/5, 2013 at 22:51 Comment(5)
Option 2 gives a completely different answer for me than option 1. Maybe because I have a transparent Toolbar? Thanks for the tip!Harrar
so which method is accurate?Patten
@Convertiplane How to do the same with html javascript? Any ideaLaxity
javascript no idea.Convertiplane
Option 2 Gave a more accurate position. ThanksPettaway
Y
15

You can use :

latlng=map.getProjection().getVisibleRegion().latLngBounds.getCenter();
Yadirayaeger answered 24/2, 2016 at 19:4 Comment(1)
Get the accuracy with this solutionSheilasheilah
U
7

to get center of map I used onMapReady() method in activity, then used googleMap.setOnCameraChangeListener() method to get position of Came:

@Override
public void onMapReady(GoogleMap googMap) {
    googleMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
        @Override
        public void onCameraChange(CameraPosition cameraPosition) {

       Log.i("centerLat",cameraPosition.target.latitude);

       Log.i("centerLong",cameraPosition.target.longitude);

        }
    });
}
Unipolar answered 11/12, 2015 at 19:7 Comment(0)
T
5

If you only want to get the position once (e.g. after the user has stopped panning the map), use setOnCameraIdleListener:

https://developers.google.com/android/reference/com/google/android/gms/maps/GoogleMap.OnCameraIdleListener

mMap.setOnCameraIdleListener(new GoogleMap.OnCameraIdleListener() {
    @Override
    public void onCameraIdle() {
        LatLng position = mMap.getCameraPosition().target;
        Log.d("MapActivity", "Position: " + position);
    }
});

or using a Java 8 lambda:

mMap.setOnCameraIdleListener(() -> {
    LatLng position = mMap.getCameraPosition().target;
    Log.d("MapActivity", "Position: " + position);
});

Note that the listener registered with setOnCameraChangeListener is called many times, and as the documentation states:

This may be called as often as once every frame and should not perform expensive operations.

Torbert answered 17/9, 2018 at 13:6 Comment(0)
A
3

best way use cameraPosition

java:

LatLng centerMap = googleMap.getCameraPosition().target;

kotlin:

googleMap?.cameraPosition?.target?.let {
    // it is LatLng center
}
Aeolus answered 22/9, 2018 at 13:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.