views:

215

answers:

3

Hi, i'm looking for answer about getting latitude and longitude from uimapkit. there is same problem that has been answered, but it didn't give right coordinate. http://yit.me/3ddp73

it's code from thread i'm linking above.

CLLocationCoordinate2D topLeft, bottomRight;
topLeft = [mapView convertPoint:CGPointMake(0,0) toCoordinateFromView:mapView];
CGPoint pointBottomRight = CGPointMake(mapView.frame.size.width, mapView.frame.size.height);
bottomRight = [mapView convertPoint:pointBottomRight toCoordinateFromView:mapView];


NSLog(@"topleft = %f", topLeft);
NSLog(@"bottom right = %f", bottomRight);

any idea to fix this issue ?

thanks

A: 

The map view has a region property.

MKCoordinateRegion region = mapView.region;

the region contains the center and the 2D span, so

CLLocationCoordinate2D topLeft = CLLocationCoordinate2DMake(
  region.center.latitude - region.span.latitudeDelta/2,
  region.center.longitude - region.span.longitudeDelta/2,
);

etc.

KennyTM
i'm sorry, but it gives me "error: invalid initializer".can you explain why you use CLLocationCoordinate2DMake Not simple CGPoint ?thanks
damniatx
@dam: You need to import CoreLocation.
KennyTM
that didn't resolve the error. -_-
damniatx
A: 

Anyone have idea to fix this issue ?

thanks

damniatx
A: 

The original solution is close. The view's coordinate system has the origin at the bottom left. Therefore CGPoint(0,0) is actually bottom left, not top. And the other coordinate will be your top right.

scott