简体   繁体   中英

MapView only shows correct location after 2nd load

Im struggling with a mapview on my app. When loading the screen with the mapview on, the map just opens to the default location for mapviews . However, when I return to the previous scren and then launch the map for a 2nd time, the correct location is displayed.

Obviously, this isn't ideal.

Any suggestions please?

My code is:

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

Thank you!

I set the current location like this:
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];
}

Hope its giving you an idea!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM