2012-07-24 27 views
0

好吧,這是我在使用CLLoactionManager下落銷

嘗試
- (void)viewDidLoad { 

    [super viewDidLoad]; 
    mapView=[[MKMapView alloc] initWithFrame:self.view.frame]; 
    //mapView.showsUserLocation=TRUE; 
    mapView.delegate=self; 
    [self.view insertSubview:mapView atIndex:0]; 


    CLLocationManager *locationManager=[[CLLocationManager alloc] init]; 
    locationManager.delegate=self; 
    locationManager.desiredAccuracy=kCLLocationAccuracyNearestTenMeters; 

    [locationManager startUpdatingLocation]; 
} 

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { 

    mStoreLocationButton.hidden=FALSE; 
    location=newLocation.coordinate; 
    //One location is obtained.. just zoom to that location 

    MKCoordinateRegion region; 
    region.center=location; 
    MKCoordinateSpan span; 
    span.latitudeDelta=0.01; 
    span.longitudeDelta=0.01; 
    region.span=span; 

    [mapView setRegion:region animated:TRUE];  
} 

我的問題是[locationManager startUpdatingLocation];似乎不火的一個方法。我錯過了什麼?我已經嘗試在第二種方法中設置斷點,但他們從未捕獲。顯然它沒有被使用。

+0

如果你不想要一個pin,那麼你可以直接使用[yourMapView setShowsUserLocation:YES]; – RileyE 2012-07-24 15:01:31

回答

3

你應該看看使用CLLocationManager來獲取當前位置,以正確的座標給你的MKMapView。

CLLocationManager *locationManager = [[CLLocationManager alloc] init]; 
MKMapView *map = [[MKMapView alloc] init]; 

[locationManager startUpdatingLocation]; 

CLLocationCoordinate2D _coordinate = locationManager.location.coordinate; 
MKCoordinateRegion extentsRegion = MKCoordinateRegionMakeWithDistance(_coordinate, 800, 800); 

[map setRegion:extentsRegion animated:YES]; 
+0

我是否需要創建一個新類來實現這個?或者這將在我的上面的代碼? – Coltrane 2012-07-25 03:46:04

+0

這段代碼只是.m的實現。顯然你也需要頭文件。我會研究一個更徹底的例子,並在第二天左右發佈。 – radesix 2012-07-25 20:12:03

0

This看起來是一個很好的開始。爲了放下一個別針,你需要知道座標。使用- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation將幫助您獲取用戶的位置,以便您可以創建一個MKAnnotationMKPinAnnotationViews。一旦你開始,它非常簡單。

+0

我試着跟着指南一起學習,但'initWithCoordinate:coordinate和Title:_title andSubtitle:_title'給了我一個錯誤,即沒有聲明這個選擇器的@interface。 – Coltrane 2012-07-25 04:14:12

+0

您是否已經導入了所需的框架和頭文件? – RileyE 2012-07-25 17:22:44

+0

啊,是的..我忘了註釋代碼..謝謝你指出這一點。我將在我的修訂示例中加入這個內容,以便儘快發佈。 – radesix 2012-07-25 21:19:39