如何在MKMapView中居我当前的位置?

时间:2011-11-24 11:44:45

标签: iphone ios mkmapview

我使用current location启用,在MKMapView中显示showUserLocation。我还想将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];
}

4 个答案:

答案 0 :(得分:51)

对于以用户位置为中心,您可以使用以下代码:

[mapView setCenterCoordinate:mapView.userLocation.location.coordinate animated:YES];

要放大特殊位置,您应该研究区域(MKCoordinateRegion)的计数和工作方式,计算区域的值并使用调用显示它:

[mapView setRegion:myRegion animated:YES];

sample WorldCities显示区域显示的基础知识。

答案 1 :(得分:2)

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];

这有助于我在地图视图的中心显示位置。

答案 2 :(得分:0)

for MKMapView

self.mapView.showsUserLocation = YES;
self.mapView.userTrackingMode = MKUserTrackingModeFollow;

答案 3 :(得分:0)

-(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];

}