Personalmente me parece más útil cuando alguien puede publicar un fragmento de código en comparación con la prosa general acerca de cómo se puede ir sobre esto. Esto es lo que le ocurrió con- aproximadamente hackeado a cabo simplemente responder mejor a esta pregunta:
En un archivo de cabecera que tengo:
#define SCROLL_UPDATE_DISTANCE 80.00
y en mi opinión (que es a la vez un delegado para CLLocationManagerDelegate, MKMapViewDelegate) :
// this method is called when the map region changes as a delegate of MKMapViewDelegate
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
NSLog(@"regionDidChangeAnimated");
MKCoordinateRegion mapRegion;
// set the center of the map region to the now updated map view center
mapRegion.center = mapView.centerCoordinate;
mapRegion.span.latitudeDelta = 0.3; // you likely don't need these... just kinda hacked this out
mapRegion.span.longitudeDelta = 0.3;
// get the lat & lng of the map region
double lat = mapRegion.center.latitude;
double lng = mapRegion.center.longitude;
// note: I have a variable I have saved called lastLocationCoordinate. It is of type
// CLLocationCoordinate2D and I initially set it in the didUpdateUserLocation
// delegate method. I also update it again when this function is called
// so I always have the last mapRegion center point to compare the present one with
CLLocation *before = [[CLLocation alloc] initWithLatitude:lastLocationCoordinate.latitude longitude:lastLocationCoordinate.longitude];
CLLocation *now = [[CLLocation alloc] initWithLatitude:lat longitude:lng];
CLLocationDistance distance = ([before distanceFromLocation:now]) * 0.000621371192;
[before release];
[now release];
NSLog(@"Scrolled distance: %@", [NSString stringWithFormat:@"%.02f", distance]);
if(distance > SCROLL_UPDATE_DISTANCE)
{
// do something awesome
}
// resave the last location center for the next map move event
lastLocationCoordinate.latitude = mapRegion.center.latitude;
lastLocationCoordinate.longitude = mapRegion.center.longitude;
}
esperanza que le envía en la dirección correcta.
distanceFromLocation es iOS 3.2 y posterior. initWithLatitude es iOS 2.0 y posterior. MKCoordinateRegion es iOS 3.0 y posterior. MKMapView centerCoordinate es iOS 3.0 y posterior.
Además, siéntase libre de saltar y dejarme en línea recta donde me he equivocado. Estoy pensando en todo esto yo mismo, pero esto está funcionando bastante bien para mí hasta ahora.
Espero que esto ayude a alguien.
Tienes razón. Debería haber publicado la respuesta.Si explica por qué usa la constante: 0.000621 ... en su respuesta, marcaré esto como la respuesta aceptada. – Alan
Conversión de metros a millas (aunque descuidado y debería ser una constante de algún tipo). –