如何在MKMapView中居中我的当前位置?
我通过使用showUserLocation
启用显示MKMapView
的current location
。 我也想要将mapview
居中放置到用户当前的位置以及位置的缩放地图。 请帮助我,因为我没有得到任何其他类似的问题上stackoverflow的帮助。
我的代码如下:
- (void)viewDidLoad { [super viewDidLoad]; [mapView setMapType:MKMapTypeStandard]; [mapView setZoomEnabled:YES]; [mapView setScrollEnabled:YES]; [mapView setCenterCoordinate:mapView.userLocation.location.coordinate animated:YES]; [mapView setDelegate:self]; }
为了以用户位置为中心,您可以使用以下代码:
[mapView setCenterCoordinate:mapView.userLocation.location.coordinate animated:YES];
要放大特殊位置,您应该研究区域( MKCoordinateRegion
)的计数和工作方式,计算您的区域值并使用call来显示:
[mapView setRegion:myRegion animated:YES];
这个示例WorldCities显示区域显示的基础知识。
MKCoordinateRegion region; MKCoordinateSpan span; span.latitudeDelta = 0.2; // 0.0 is min value u van provide for zooming span.longitudeDelta= 0.2; CLLocationCoordinate2D location = [self addressLocation]; region.span=span; region.center =location; // to locate to the center if(addAnnotation != nil) { [mapView removeAnnotation:addAnnotation]; [addAnnotation release]; addAnnotation = nil; } addAnnotation = [[AddressANnotation alloc] initWithCoordinate:location]; [mapView addAnnotation:addAnnotation]; [mapView setRegion:region animated:TRUE]; [mapView regionThatFits:region];
这有助于我在地图视图的中心显示位置。
为MKMapView
self.mapView.showsUserLocation = YES; self.mapView.userTrackingMode = MKUserTrackingModeFollow;
-(void) removeZoom:(float)deltaValue { MKCoordinateRegion region; region.center.latitude = self.locationManager.location.coordinate.latitude; region.center.longitude = self.locationManager.location.coordinate.longitude; [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.5]; region.span.latitudeDelta = deltaValue; region.span.longitudeDelta = deltaValue; region = [mapViewSelf regionThatFits:region]; [mapViewSelf setRegion:region animated:YES]; [UIView commitAnimations]; }