tutorial current annotation iphone mkmapview

iphone - current - mapkit tutorial



iOS MKMapView zoom para mostrar todos los marcadores (5)

Aquí hay una mejora que toma en cuenta la altura de las vistas de anotación que está superponiendo en el mapa (por ejemplo, la parte superior de la anotación no se corta cuando su desplazamiento de coordenadas está en la parte inferior, por ejemplo). O para generalizar aún más, le permite especificar el relleno en píxeles en lugar de un porcentaje. Requiere un pase de dos etapas en el que encuentre los límites de las anotaciones y luego aumente los límites para tener en cuenta el relleno de su mapa.

- (void) zoomToAnnotationsBounds:(NSArray *)annotations { CLLocationDegrees minLatitude = DBL_MAX; CLLocationDegrees maxLatitude = -DBL_MAX; CLLocationDegrees minLongitude = DBL_MAX; CLLocationDegrees maxLongitude = -DBL_MAX; for (MyAnnotation *annotation in annotations) { double annotationLat = annotation.coordinate.latitude; double annotationLong = annotation.coordinate.longitude; minLatitude = fmin(annotationLat, minLatitude); maxLatitude = fmax(annotationLat, maxLatitude); minLongitude = fmin(annotationLong, minLongitude); maxLongitude = fmax(annotationLong, maxLongitude); } // See function below [self setMapRegionForMinLat:minLatitude minLong:minLongitude maxLat:maxLatitude maxLong:maxLongitude]; // If your markers were 40 in height and 20 in width, this would zoom the map to fit them perfectly. Note that there is a bug in mkmapview''s set region which means it will snap the map to the nearest whole zoom level, so you will rarely get a perfect fit. But this will ensure a minimum padding. UIEdgeInsets mapPadding = UIEdgeInsetsMake(40.0, 10.0, 0.0, 10.0); CLLocationCoordinate2D relativeFromCoord = [self.mapView convertPoint:CGPointMake(0, 0) toCoordinateFromView:self.mapView]; // Calculate the additional lat/long required at the current zoom level to add the padding CLLocationCoordinate2D topCoord = [self.mapView convertPoint:CGPointMake(0, mapPadding.top) toCoordinateFromView:self.mapView]; CLLocationCoordinate2D rightCoord = [self.mapView convertPoint:CGPointMake(0, mapPadding.right) toCoordinateFromView:self.mapView]; CLLocationCoordinate2D bottomCoord = [self.mapView convertPoint:CGPointMake(0, mapPadding.bottom) toCoordinateFromView:self.mapView]; CLLocationCoordinate2D leftCoord = [self.mapView convertPoint:CGPointMake(0, mapPadding.left) toCoordinateFromView:self.mapView]; double latitudeSpanToBeAddedToTop = relativeFromCoord.latitude - topCoord.latitude; double longitudeSpanToBeAddedToRight = relativeFromCoord.latitude - rightCoord.latitude; double latitudeSpanToBeAddedToBottom = relativeFromCoord.latitude - bottomCoord.latitude; double longitudeSpanToBeAddedToLeft = relativeFromCoord.latitude - leftCoord.latitude; maxLatitude = maxLatitude + latitudeSpanToBeAddedToTop; minLatitude = minLatitude - latitudeSpanToBeAddedToBottom; maxLongitude = maxLongitude + longitudeSpanToBeAddedToRight; minLongitude = minLongitude - longitudeSpanToBeAddedToLeft; [self setMapRegionForMinLat:minLatitude minLong:minLongitude maxLat:maxLatitude maxLong:maxLongitude]; } -(void) setMapRegionForMinLat:(double)minLatitude minLong:(double)minLongitude maxLat:(double)maxLatitude maxLong:(double)maxLongitude { MKCoordinateRegion region; region.center.latitude = (minLatitude + maxLatitude) / 2; region.center.longitude = (minLongitude + maxLongitude) / 2; region.span.latitudeDelta = (maxLatitude - minLatitude); region.span.longitudeDelta = (maxLongitude - minLongitude); // MKMapView BUG: this snaps to the nearest whole zoom level, which is wrong- it doesn''t respect the exact region you asked for. See http://stackoverflow.com/questions/1383296/why-mkmapview-region-is-different-than-requested [self.mapView setRegion:region animated:YES]; }

Estoy trabajando con MKMapView y he trazado varios puntos en el mapa. He usado MKCoordinateRegion y MKCoordinateSpan para habilitar el zoom, etc. en uno de los puntos, pero eso no es lo que quiero ...

Estoy tratando de usar algo similar a la función de zoom a límites de Javascript. por lo que todos mis puntos deben ser visibles para el usuario. (Habrá alrededor de 10 puntos en todo el Reino Unido) Me gustaría mostrarlos todos, o si la mayoría de ellos estuvieran en el área de Londres, acérquese.

¿Hay una manera de resolver esto programáticamente?


Es una pregunta antigua y sé que puede que no necesites ayuda. Pero solo lo estoy MKMapView para cualquiera que esté buscando una manera de hacerlo ahora, ya que hay un nuevo método en MKMapView partir de iOS 7 que se puede usar. Es limpio y fácil a la vez.

Declaración

RÁPIDO

func showAnnotations(_ annotations: [AnyObject]!, animated animated: Bool)

C OBJETIVO

- (void)showAnnotations:(NSArray *)annotations animated:(BOOL)animated

Parámetros

anotaciones Las anotaciones que desea que sean visibles en el mapa. animado SÍ si desea que la región del mapa cambie a animada, o NO si desea que el mapa muestre la nueva región inmediatamente sin animaciones.

Discusión

Al llamar a este método, se actualiza el valor en la propiedad de la región y potencialmente otras propiedades para reflejar la nueva región del mapa.


Por supuesto. Desea encontrar los valores de latitud y longitud más grandes y más pequeños entre sus anotaciones (lo que puede hacer iterando sobre map.annotations), luego configure el mapa para que muestre todos ellos.

// pad our map by 10% around the farthest annotations #define MAP_PADDING 1.1 // we''ll make sure that our minimum vertical span is about a kilometer // there are ~111km to a degree of latitude. regionThatFits will take care of // longitude, which is more complicated, anyway. #define MINIMUM_VISIBLE_LATITUDE 0.01 MKCoordinateRegion region; region.center.latitude = (minLatitude + maxLatitude) / 2; region.center.longitude = (minLongitude + maxLongitude) / 2; region.span.latitudeDelta = (maxLatitude - minLatitude) * MAP_PADDING; region.span.latitudeDelta = (region.span.latitudeDelta < MINIMUM_VISIBLE_LATITUDE) ? MINIMUM_VISIBLE_LATITUDE : region.span.latitudeDelta; region.span.longitudeDelta = (maxLongitude - minLongitude) * MAP_PADDING; MKCoordinateRegion scaledRegion = [map regionThatFits:region]; [map setRegion:scaledRegion animated:YES];


Respuesta modificada con todo el código de trabajo perfecto.

//Zooming the ploted Area - (void)zoomToAnnotationsBounds:(NSArray *)latLongArray { __block CLLocationDegrees minLatitude = DBL_MAX; __block CLLocationDegrees maxLatitude = -DBL_MAX; __block CLLocationDegrees minLongitude = DBL_MAX; __block CLLocationDegrees maxLongitude = -DBL_MAX; [latLongArray enumerateObjectsUsingBlock:^(NSString *latLongObj, NSUInteger latLongIdx, BOOL *stop) { latLongObj = [latLongArray objectAtIndex:latLongIdx]; NSArray *latLongPoint = [latLongObj componentsSeparatedByString:@","]; double annotationLat = [[latLongPoint objectAtIndex:0] doubleValue]; double annotationLong = [[latLongPoint objectAtIndex:1] doubleValue]; minLatitude = fmin(annotationLat, minLatitude); maxLatitude = fmax(annotationLat, maxLatitude); minLongitude = fmin(annotationLong, minLongitude); maxLongitude = fmax(annotationLong, maxLongitude); }]; [self setMapRegionForMinLat:minLatitude minLong:minLongitude maxLat:maxLatitude maxLong:maxLongitude]; } -(void) setMapRegionForMinLat:(double)minLatitude minLong:(double)minLongitude maxLat:(double)maxLatitude maxLong:(double)maxLongitude { // pad our map by 10% around the farthest annotations // we''ll make sure that our minimum vertical span is about a kilometer // there are ~111km to a degree of latitude. regionThatFits will take care of // longitude, which is more complicated, anyway. MKCoordinateRegion region; region.center.latitude = (minLatitude + maxLatitude) / 2; region.center.longitude = (minLongitude + maxLongitude) / 2; region.span.latitudeDelta = (maxLatitude - minLatitude) * MAP_PADDING; region.span.latitudeDelta = (region.span.latitudeDelta < MINIMUM_VISIBLE_LATITUDE) ? MINIMUM_VISIBLE_LATITUDE : region.span.latitudeDelta; region.span.longitudeDelta = (maxLongitude - minLongitude) * MAP_PADDING; MKCoordinateRegion scaledRegion = [regionsMapView regionThatFits:region]; [regionsMapView setRegion:scaledRegion animated:YES]; }


Si solo apuntas a iOS 7 o superior, ahora puedes usar:

- (void)showAnnotations:(NSArray *)annotations animated:(BOOL)animated