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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8256477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:33:43  来源:igfitidea点击:

How to center my current location in MKMapView?

iphoneiosmkmapview

提问by vipul

I am showing the current locationin MKMapViewby using showUserLocationenables. I also want to center the mapviewto the user current location and also the zoomed map of location. Please help me regarding this as I don't get any help from other similar questions on stackoverflow.

我通过使用使能显示current locationin 。我还想以用户当前位置以及放大的位置地图为中心。请帮助我解决这个问题,因为我没有从有关 stackoverflow 的其他类似问题中得到任何帮助。MKMapViewshowUserLocationmapview

My code is below:

我的代码如下:

- (void)viewDidLoad {
  [super viewDidLoad];
  [mapView setMapType:MKMapTypeStandard];
  [mapView setZoomEnabled:YES];
  [mapView setScrollEnabled:YES];
  [mapView setCenterCoordinate:mapView.userLocation.location.coordinate animated:YES];
  [mapView setDelegate:self];
}

回答by Denis

For centering on user location, you can use the following code:

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

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

For zooming on special locations, you should study how the regions (MKCoordinateRegion) are counted and work, count your values for the region and display it using call:

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

[mapView setRegion:myRegion animated:YES];

This sample WorldCitiesshows basics of region displaying.

示例 WorldCities显示了区域显示的基础知识。

回答by iamsult

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

This had help me to show the position at the center of the map view.

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

回答by Baryon Lee

for MKMapView

用于 MKMapView

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

回答by Chandni - Systematix

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

}