Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS init MKMapView,具有用户位置和适当的比例/跨度_Ios_Mkmapview - Fatal编程技术网

iOS init MKMapView,具有用户位置和适当的比例/跨度

iOS init MKMapView,具有用户位置和适当的比例/跨度,ios,mkmapview,Ios,Mkmapview,我像这样初始化MapViewController: - (void)viewDidLoad { [super viewDidLoad]; self.firstShow=TRUE; self.mapView.delegate=self; self.mapView.showsUserLocation=YES; self.cllmng=[[CLLocationManager alloc]init]; self.cllmng.delegate=self;

我像这样初始化MapViewController:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.firstShow=TRUE;
    self.mapView.delegate=self;
    self.mapView.showsUserLocation=YES;
    self.cllmng=[[CLLocationManager alloc]init];
    self.cllmng.delegate=self;
    self.cllmng.desiredAccuracy=kCLLocationAccuracyNearestTenMeters;
    self.cllmng.distanceFilter=50;
    [self.cllmng startUpdatingLocation];
}
我让mapviewControler实现CLLocationManagerDelegate,并在我的代码中实现
-(void)locationManager:(CLLocationManager*)manager didUpdateLocations:(NSArray*)位置

我遇到的问题是,我想以适当的比例初始化以当前用户位置为中心的地图视图。我打算这样做

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
CLLocation * currentLoci=[locations lastObject];
MKCoordinateRegion r;
MKCoordinateSpan span;
span.latitudeDelta = 1;
span.longitudeDelta = 1;
r.span = span;
CLLocationCoordinate2D c;
c.longitude=currentLoci.coordinate.longitude;
c.latitude=currentLoci.coordinate.latitude;
r.center = c;
[self.mapView setRegion:r animated:YES];
}

但是有时,在-(void)viewdiload中调用[self.cllmng startUpdatingLocation]后,我无法立即调用
-(void)locationManager:(CLLocationManager*)manager diddupdatelocations:(NSArray*)locations
。因此,地图视图只显示用户位置,但显示整个澳大利亚。即使未触发
didUpdateLocations
,如何在地图视图的初始中设置比例/span?谢谢

不用使用CLLocation manager自己修改区域,您只需设置mapView的
跟踪模式
,它就会自动居中并放大您的位置。如果用户开始拖动地图,则可以禁用跟踪模式,但如果您确实希望用户无法控制,则可以禁用用户交互。下面是如何做到这一点

[self.mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];

ref:

您可以只设置地图视图的
跟踪模式,而不是自己使用CLLocation manager修改区域,它将自动居中并放大您的位置。如果用户开始拖动地图,则可以禁用跟踪模式,但如果您确实希望用户无法控制,则可以禁用用户交互。下面是如何做到这一点

[self.mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];

参考:iOS 11.x Swift 4.0 Craigs answer,已更新

mapView.showsUserLocation = true

恐怕不太明显。

iOS 11.x Swift 4.0 Craigs answer,更新版

mapView.showsUserLocation = true

恐怕不太明显。

为什么要使用CLLocationManager获取用户位置?由于您在地图视图中将showsUserLocation设置为YES,它将调用自己的委托方法didUpdateUserLocation,该方法应与蓝点同步。@Anna。只是为了在didUpdateLocation方法中获得当前位置并使用它生成适当的区域。克雷格已经给了我想要的。无论如何,谢谢。为什么要使用CLLocationManager获取用户位置?由于您在地图视图中将showsUserLocation设置为YES,它将调用自己的委托方法didUpdateUserLocation,该方法应与蓝点同步。@Anna。只是为了在didUpdateLocation方法中获得当前位置并使用它生成适当的区域。克雷格已经给了我想要的。无论如何,谢谢你。