tags:

views:

36

answers:

1

Hi

I need to know the distance (in kilometers) from center map to the other side of the screen, (and if the zoom change the distance will change).

I need to implement this feature in this function

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
}

Any ideas how i can do this?

Thanks

A: 

MkMapView has properties named centerCoordinate(CLLocationCoordinate2D) and region (MKCoordinateRegion). Region is a struct that:

typedef struct {
CLLocationDegrees latitudeDelta;
CLLocationDegrees longitudeDelta;
}MKCoordinateSpan

You should be able to create another point, based on centerCoordinate, let's say, by adding latitudeDelta to you latitude property or centerCoordinate, and calculate distance using the method of CLLocation:

- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location

Something like this

MkMapView * mapView; // init somewhere
MKCoordinateRegion region = mapView.region;
CLLocationCoordinate2D centerCoordinate = mapView.centerCoordinate;
CLLocation * newLocation = [[[CLLocation alloc] initWithLatitude:centerCoordinate.latitude+region.latitudeDelta longitude:centerCoordinate.longitude] autorelease];
CLLocation * centerLocation = [[[CLLocation alloc] initWithLatitude:centerCoordinate.latitude:longitude:centerCoordinate.longitude] autorelease];
CLLocationDistance distance = [centerLocation distanceFromLocation:newLocation]; // in meters

And just calculate each time a delegate fires a certain method (decide which you need: MKMapViewDelegate)

krzyspmac