2013-02-02 57 views
0

Im在我的應用上與mapview掙扎。當加載屏幕mapview時,地圖只會打開到默認位置mapviews。但是,當我返回到之前的scren並再次啓動地圖時,會顯示正確的位置。MapView只顯示第二次加載後的正確位置

顯然,這並不理想。

有什麼建議嗎?

我的代碼是:

CLLocation *mapLocation = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude]; 
[[self map] setCenterCoordinate:[mapLocation coordinate]]; 
[[self map] setRegion: MKCoordinateRegionMakeWithDistance([mapLocation coordinate], 1000, 1000)];   
MapAnnotation *annotation = [[MapAnnotation alloc] init]; 
[annotation setCoordinate:[mapLocation coordinate]]; 
[[self map] addAnnotation:annotation]; 

謝謝!

+0

您應該發佈加載地圖視圖的代碼,因爲此代碼可以正常工作。 – rptwsthi

+0

你什麼時候這樣做?和耶...顯示地圖查看代碼 –

+0

此代碼是在viewViewidController上viewDidLoad ... 對不起,即時通訊仍在學習。我應該發佈什麼代碼? –

回答

0

我設定的當前位置是這樣的:
Location.h:

@interface WhereamiViewController : UIViewController <CLLocationManagerDelegate, MKMapViewDelegate, UITextFieldDelegate> { 
CLLocationManager *locationManager;  
MKMapView *worldView; 
IBOutlet UIActivityIndicatorView *activityIndicator; 
IBOutlet UITextField *locationTextField; 
} 

- (void)findLocation; 
- (void)foundLocation:(CLLocation *)loc; 
- (IBAction)setMapTyp:(id)sender; 

@property MKMapType mapType; 
@property (nonatomic, retain) IBOutlet MKMapView *worldView; 
@property (nonatomic, readonly) NSDate *currentDate; 

@end 

Location.m:

- (void)locationManager:(CLLocationManager *)manager 
didUpdateToLocation:(CLLocation *)newLocation 
     fromLocation:(CLLocation *)oldLocation { 
NSLog(@"%@", newLocation); 

NSTimeInterval t = [[newLocation timestamp] timeIntervalSinceNow]; 
if (t < -180) { 
    return; 
} 

[self foundLocation:newLocation]; 
} 

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation { 
CLLocationCoordinate2D loc = [userLocation coordinate]; 
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 250, 250); 
[worldView setRegion:region animated:YES]; 
} 



- (void)locationManager:(CLLocationManager *)manager 
    didFailWithError:(NSError *)error { 
NSLog(@"Could not find location: %@", error); 
} 

- (void)findLocation { 
[locationManager startUpdatingLocation]; 
[activityIndicator startAnimating]; 
[locationTextField setHidden:YES]; 
} 

- (void)foundLocation:(CLLocation *)loc { 
CLLocationCoordinate2D coord = [loc coordinate]; 
NSDateFormatter *format = [[NSDateFormatter alloc] init]; 
[format setDateFormat:@"HH:mm, dd. MMM. yyyy "]; 

NSDate *now = [[NSDate alloc] init]; 

NSString *dateString = [format stringFromDate:now]; 

MyOwnMapPoint *mp = [[MyOwnMapPoint alloc] initWithCoordinate:coord 
                title:[locationTextField text] 
               subtitle:dateString]; 
NSLog(@"Die Uhrzeit ist: %@", dateString); 
[worldView addAnnotation:mp]; 
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coord, 250, 250); 
[worldView setRegion:region animated:YES]; 

//Reset the UI 
[locationTextField setText:@""]; 
[activityIndicator stopAnimating]; 
[locationTextField setHidden:NO]; 
[locationManager stopUpdatingLocation]; 
} 

希望它給你的想法!

+0

對不起,我對客觀的C很陌生,我沒有真正理解這一點。 –

+0

嘗試使用'mapView:didUpdateUserLocation:'在啓動時顯示您的位置! – zero3nna

+0

好的謝謝,會嘗試 –

相關問題