2013-02-02 34 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

好的谢谢,会尝试 –

相关问题