我之前已经在这里问了一个类似的问题而未能得到答案,但当时我并不知道确切的问题,但现在我已经确定了它。
当我将视图留在我的应用程序中时,如进入另一个视图然后返回到我的主视图,有时我的animateToCameraPosition不会将我定位在我的位置,只是将用户留在默认位置。我已经尝试了所有内容并在网上进行了研究,似乎没有什么能解决这个问题。
代码如下。
感谢。
BOOL firstLocationUpdate;
- (void)viewDidLoad
{
[super viewDidLoad];
firstLocationUpdate= NO;
self.mapView.myLocationEnabled = YES;
self.mapView.mapType = kGMSTypeNormal;
self.mapView.settings.compassButton = NO;
self.mapView.settings.myLocationButton = NO;
self.mapView.delegate = self;
[self.mapView addObserver:self
forKeyPath:@"myLocation"
options:NSKeyValueObservingOptionNew
context:NULL];
self.mapView.userInteractionEnabled = YES;
self.mapView.settings.rotateGestures = NO;
self.mapView.settings.tiltGestures = NO;
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
if (!firstLocationUpdate) {
NSLog(@"LOCATION MASTER");
// If the first location update has not yet been recieved, then jump to that
// location.
firstLocationUpdate = YES;
CLLocation *location = [change objectForKey:NSKeyValueChangeNewKey];
// self.mapView.camera = [GMSCameraPosition cameraWithTarget:location.coordinate
// zoom:14];
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:location.coordinate.latitude
longitude:location.coordinate.longitude
zoom:17.0];
[self.mapView animateToCameraPosition:camera];
latitude = location.coordinate.latitude;
longitude = location.coordinate.longitude;
NSLog(@"latitude = %g", latitude);
NSLog(@"Resolving the Address");
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
if(placemarks.count){
self.address.text = [placemarks[0] name];
// NSLog(@"test");
//streetNumberLabel.text = [placemarks[0] subThoroughfare];
//streetLabel.text = [placemarks[0] thoroughfare];
//neighborhoodLabel.text = [placemarks[0] subLocality];
}
}];
}