26

I'm using the new Google Maps SDK for iOS

Can I get the true coordinate form GMSMapView.center? Now it returns a value of CGPoint, but it's not the true coordinate.

Thank and Regards.

6 Answers 6

43

Use the projection method - (CLLocationCoordinate2D)coordinateForPoint:(CGPoint)pointof the mapview to convert a point in the map to the geological coordinates

CGPoint point = mapView.center;
CLLocationCoordinate2D coor = [mapView.projection coordinateForPoint:point];
0
42

I suggest to use [yourMapView.camera target] instead of Jing's solution.

Jing's solution work fine on iOS 6, but maybe having issue on iOS 7/7.1, the projection may report wrong coordinate (a bit downward shift) ! I have checked the map bounds and padding is correct, both result of [projection pointForCoordinate: coordinate] and [projection coordinateForPoint:point] can contrast to each other, no ideas where the problem is...

3
  • could you please explain it more,i want to get centre of camera and lat or long at that centre.
    – Daljeet
    Nov 21, 2014 at 19:45
  • thx! I was trying to use projection with mapView.center but just as per your highlight the coordinate was shifted downward - I spent an hour trying to figure out why my pin is in wrong position. camera.target did the trick
    – vir us
    Nov 25, 2016 at 18:45
  • 1
    I wouldn't assume camera.target is the centre of the view - it will be shifted if you have applied any "padding" to your map view.
    – Ryan
    Oct 31, 2017 at 1:22
16

Here is how to do it in Swift 4

let coordinate = mapView.projection.coordinate(for: mapView.center)
11

Are you looking for mapView.camera.target? This is a CLLocationCoordinate2D.

2
  • IMO this is better solution than accepted. Both giving the same result but this one is shorter and more straightforward Jan 14, 2019 at 12:36
  • I agree that it's better, but in my case this solution and the one from above answer with projecting mapView.center are not giving the same result, in fact mapView.center gives me wrong location, shifted south on the map, as someone wrote.. not sure why.
    – bra.Scene
    Jun 24, 2020 at 22:00
0

Swift 4

let point = mapView.center
let coordinate = mapView.convert(point, toCoordinateFrom: mapView)
0

Perhaps a better way if the user is allowed to interact with the map is using the map's did-become-idle delegate method. Here you can get the center coordinate after each camera change.

func mapView(_ mapView: GMSMapView, idleAt position: GMSCameraPosition) {
    print(position.target)
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.