2015-09-04 93 views
0

我一直在試圖讓用戶的當前位置顯示在我的地圖上,但一直未能。我是MapKit的新手,所以我可能錯過了一些東西。iOS地圖顯示用戶的當前位置

我確實得到彈出警報詢問是否允許使用我的當前位置,但是實際上沒有顯示位置。

下面的代碼:

頁眉.H:

@interface TeachersMapViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate> 

#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) 

@property (nonatomic,retain) CLLocationManager *locationManager; 
@property(nonatomic, retain) IBOutlet MKMapView *mapView; 


@end 

實施

- (void)viewDidLoad 
{ 

#ifdef __IPHONE_8_0 
    if(IS_OS_8_OR_LATER) 
    { 
     [self.locationManager requestWhenInUseAuthorization]; 
    } 
#endif 

    self.locationManager.delegate=self; 
    [self.locationManager startUpdatingLocation]; 

    [self.mapView.userLocation addObserver:self 
           forKeyPath:@"location" 
            options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld) 
            context:NULL];  

} 

任何幫助,這將是非常讚賞。

+0

在MapView上,您需要將showUserCurrentLocation屬性設置爲YES。 mapView.showsUserLocation = YES; –

回答

1

您可以通過此使用戶的位置:

self.mapView.showsUserLocation = YES; 

如果你正在使用模擬器,它不會顯示你當前的位置,也可以是在美國。 「模擬器默認你會在美國顯示一些位置,如果你想改變這個位置,在iOS模擬器菜單中,進入Debug -> Location -> Custom Location。你可以設置經緯度並相應地測試應用程序,這與mapkit以及與CLLocationManager

+0

謝謝修復它,儘管它縮小了,請問有沒有一個快速的指令來將視圖放大到用戶位置周圍的區域? – Kitcc

+0

你的意思是你想放大用戶位置區域嗎? –

+0

是的,這就是我的意思是 – Kitcc

相關問題