Iphone 为什么MKMapView要把我放在海里?

Iphone 为什么MKMapView要把我放在海里?,iphone,mapkit,mkmapview,Iphone,Mapkit,Mkmapview,我有以下代码来设置用户位置的地图: MKCoordinateSpan span; span.latitudeDelta=0.2; span.longitudeDelta=0.2; CLLocationCoordinate2D location=mapView.userLocation.coordinate; location = mapView.userLocation.location.coordinate; MKCoordinateRegion region; region.span=

我有以下代码来设置用户位置的地图:

MKCoordinateSpan span;
span.latitudeDelta=0.2;
span.longitudeDelta=0.2; 

CLLocationCoordinate2D location=mapView.userLocation.coordinate;
location = mapView.userLocation.location.coordinate;

MKCoordinateRegion region;
region.span=span;
region.center=location;

[mapView setRegion:region animated:TRUE];
[mapView regionThatFits:region];
现在,当我执行
mapView.showsUserLocation=YES
时,它会将我的位置显示在正确的位置(在我的设备上,以及模拟器中的苹果总部),然而,上面的两次代码都将我置于非洲附近的某个海洋中


有什么想法吗?

纬度/经度对0/0位于英格兰格林威治正南的赤道上。该地点位于几内亚湾,远离非洲西海岸。如果pin位于该位置,则不会设置mapView.userLocation

可能在MKMapKit有机会获得其位置之前,这里的代码正在运行。您真正想要做的是让viewController采用
MKMapKitDelegate
协议,将地图视图的
.delegate
属性设置为
self
,然后实现以下方法:

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
当地图工具包找到用户时,将调用该方法,您可以使用在那里传递的userLocation值在地图上设置区域。不过,在第一次调用之前,MapView仍在定位自身,而您询问其位置还为时过早

此外,您的区域在末尾的位置是不可操作的。该方法不会在适当的位置调整区域大小,它实际上返回一个调整大小的区域,供您使用。因此,您希望将地图视图的区域设置为该方法返回的区域。像这样:

[mapView setRegion:[mapView regionThatFits:region] animated:TRUE];

我所做的是在
info.plist
上添加以下条目:

NSLocationAlwaysUsageDescription
将此信息输入本(非常好)教程:

您可以选择此方法

- (void)mapView:(MKMapView *)_mapView regionDidChangeAnimated:(BOOL)animated{

CLGeocoder *ceo = [[CLGeocoder alloc]init];
CLLocation *loc = [[CLLocation alloc]initWithLatitude:_mapView.region.center.latitude longitude:_mapView.region.center.longitude];
CLLocationAccuracy accuracy = _mapView.userLocation.location.horizontalAccuracy;

if (accuracy) {
    current_pickup_location = loc;
    [_currentStreetSpinner startAnimating];
    [ceo reverseGeocodeLocation:loc
              completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error){
                  CLPlacemark *placemark = [placemarks objectAtIndex:0];
                  NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
                  //                  NSLog(@"location %@",placemark.location);
                  NSLog(@"I am currently at %@",locatedAt);
                  _currentStreetLabel.text = locatedAt;
                  static_iVar = _currentStreetLabel.text;
                  [_currentStreetSpinner stopAnimating];
                  [self listDoctorsWithSpeciality_id:currentSpeciality_id
                               withProfessionalityId:currentProfessionality_id];
              }];
}
-(void)地图视图:(MKMapView*)\u地图视图区域IDChangeAnimated:(BOOL)动画{
CLGeocoder*ceo=[[CLGeocoder alloc]init];
CLLocation*loc=[[CLLocation alloc]initWithLatitude:_-mapView.region.center.latitude经度:_-mapView.region.center.Length];
CLLocationAccurance精度=_mapView.userLocation.location.HorizontalAccurance;
if(精度){
当前拾取位置=loc;
[_CurrentStreetSpinnerStartAnimating];
[首席执行官撤销任命:loc
completionHandler:^(NSArray*_可为空的位置标记,NSError*_可为空的错误){
CLPlacemark*placemark=[placemarks objectAtIndex:0];
NSString*locatedAt=[[placemark.addressDictionary值forkey:@“FormattedAddressLines”]组件通过字符串连接:@“,”];
//NSLog(@“位置%@”,placemark.location);
NSLog(@“我目前在%@”,位置DAT);
_currentStreetLabel.text=locatedAt;
static_iVar=_currentStreetLabel.text;
[_currentStreetSpinner停止设置动画];
[具有专业id的自列医生:currentSpeciality\u id
withProfessionalityId:currentProfessionality_id];
}];
}

}

我之所以回答这个问题,是因为选择的答案对我没有帮助,我也遇到了同样的问题。didUpdateUserLocation不太实用,因为它使应用程序在每次位置更改时都进行更新。上述解决方案的另一个问题是,当地图加载时,询问位置还为时过早。那么,在地图加载之前,您如何询问用户位置,以便缩放不会将您带到非洲

您需要使用viewDidLoad上的位置管理器请求位置。然后,您可以设置区域

SWIFT 3


您是否检查了
mapView.userLocation.location
是否为零?否。。。我猜这个位置还没有添加到地图上?
   //top of your class
   let locManger  = CLLocationManager()


   override func viewDidLoad()   {
        super.viewDidLoad()


         //request user location
         locManager.startUpdatingLocation()


        //this is the code that sets the region based on the location

        let span = MKCoordinateSpanMake(0.5, 0.5)

        let location = CLLocationCoordinate2DMake((locManager.location?.coordinate.latitude)!, (locManager.location?.coordinate.longitude)!)

        let region = MKCoordinateRegionMake(location, span)

        yourMap.setRegion(region, animated: false)


        //stop location updating 
        locManager.stopUpdatingLocation()
 }