2010-09-08 15 views
4

我有這樣的代碼來設置地圖在用戶的位置:爲什麼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=span; 
region.center=location; 

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

現在,當我做mapView.showsUserLocation=YES它顯示我的位置在正確的地方(我的設備上,並在模擬器蘋果總部)然而,上面的代碼都會讓我在非洲附近的海中!

任何想法?

+1

你檢查'mapView.userLocation.location'不是零? – FelixLam 2010-09-08 10:23:29

+0

不......我猜這個位置還沒有添加到地圖中呢? – 2010-09-08 10:53:28

回答

14

緯度/經度對0/0位於英格蘭格林威治以南的赤道上。那個地點位於非洲西海岸的幾內亞灣。如果這是你的pin被放置的地方,那麼你的mapView.userLocation沒有被設置。

可能你的代碼在MKMapKit有機會獲得它的位置之前運行。你真正想要做的是有你的viewController採用MKMapKitDelegate協議,您的地圖視圖的.delegate屬性設置爲self,然後實現方法:

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation 

,當地圖套件所在的方法會被調用用戶,您可以使用您通過的userLocation值在地圖上設置區域。儘管如此,在第一次被調用之前,MapView仍然在尋找自己的位置,而你對其位置的詢問還爲時過早。

此外,您的regionThatFits在那裏最終是一個無操作。該方法不會調整該區域的大小,它實際上會返回一個調整大小的區域,但您可以使用它。所以你想要將你的地圖視圖的區域設置爲該方法返回的內容。就像這樣:

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

太棒了。 :) 謝謝 – 2010-09-08 13:12:10

0

您可以選擇此方法..

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

}

0

我在回答這個問題,因爲我選擇的答案並沒有真正幫助我,而且我遇到了完全相同的問題。 didUpdateUserLocation不是很實用,因爲它使得每次更新位置都會更新應用程序。上述解決方案的另一個問題是,當地圖加載時,要求提供位置還爲時過早。那麼,在地圖加載之前,您如何要求用戶位置,以便縮放不會將您帶到非洲?

您需要通過使用viewDidLoad上的位置管理器來請求位置。之後,您可以設置該區域。

SWIFT 3

//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() 
} 
相關問題