سؤال

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.

هل كانت مفيدة؟

المحلول 2

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...

نصائح أخرى

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];

Here is how to do it in Swift 4

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

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

Swift 4

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

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)
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top