【问题标题】:iOS init MKMapView with user location and proper scale/spaniOS 使用用户位置和适当的比例/跨度初始化 MKMapView
【发布时间】:2013-12-05 10:03:18
【问题描述】:

我像这样初始化我的 mapviewControler:

- (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 *)locations

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

- (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) viewDidload 中调用 [self.cllmng startUpdatingLocation] 后,我无法立即接到 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 的电话。因此,地图视图刚开始显示用户位置,但显示了整个澳大利亚。即使didUpdateLocations 未被触发,我如何在地图视图的初始化中设置比例/跨度? THX!

【问题讨论】:

  • 为什么要使用 CLLocationManager 来获取用户位置?由于您在地图视图上将 showsUserLocation 设置为 YES,因此它将调用自己的委托方法 didUpdateUserLocation,该方法应与蓝点同步。
  • @安娜。只是为了获取当前位置并使用它在 didUpdateLocation 方法中创建适当的区域。克雷格给了我想要的。还是谢谢你。

标签: ios mkmapview


【解决方案1】:

而不是使用 CLLocation 管理器自己修改区域,您只需设置 mapView 的trackingMode,它就会自动居中并放大您的位置。如果用户开始拖动地图,则可以禁用跟踪模式,但如果您真的希望他们无法控制,则可以禁用用户交互。这是怎么做的

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

参考:https://developer.apple.com/library/ios/documentation/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html#//apple_ref/occ/instm/MKMapView/setUserTrackingMode:animated:

【讨论】:

    【解决方案2】:

    iOS 11.x Swift 4.0 Craigs 答案,已更新。

    mapView.showsUserLocation = true
    

    恐怕不是那么明显。

    【讨论】:

      猜你喜欢
      • 2010-11-29
      • 1970-01-01
      • 1970-01-01
      • 2013-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多