代码在这里。 它基本上有效,但我已经迷失了方向,无法弄清楚如何从[geocoder]例程中获取“point.title”。 point.title是Mapkit注释事物的一部分。我可以注释掉,把一个简单的字符串放入point.title,但是我不能让它做的是返回完整的'locatedAT'或者确实是任何placemark.subLocality例如。项目
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800);
[self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
// Add an annotation
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = userLocation.coordinate;
//point.title = @"Where am I?";
//point.subtitle = @"I'm here!!!";
////////////////////////
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
CLLocation *loc = [[CLLocation alloc]initWithLatitude:userLocation.coordinate.latitude longitude:userLocation.coordinate.longitude];
[geocoder reverseGeocodeLocation: loc
completionHandler:^(NSArray* placemarks, NSError* error){
CLPlacemark *placemark = [placemarks objectAtIndex:0];
if (placemark) {
// NSLog(@"placemark %@",placemark);
//String to hold address
NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
// NSLog(@"addressDictionary %@", placemark.addressDictionary);
// NSLog(@"placemark %@",placemark.region);
// NSLog(@"placemark %@",placemark.country); // Give Country Name
// NSLog(@"placemark %@",placemark.locality); // Extract the city name
// NSLog(@"location %@",placemark.name);
// NSLog(@"location %@",placemark.ocean);
// NSLog(@"location %@",placemark.postalCode);
// NSLog(@"location %@",placemark.subLocality);
//Print the location to console
// NSLog(@"I am currently at %@",locatedAt);
point.title = locatedAt;
}
else {
NSLog(@"Could not locate");
}
}
];
///////////////////////////
NSLog(@"point.title %@",point.title);
[self.mapView addAnnotation:point];
[self.mapView selectAnnotation:point animated:YES]; }
答案 0 :(得分:0)
我通过简单地添加/修改以下内容来使我的代码正常工作。我无法理解它为什么会起作用,因为point.title = locatedAt会覆盖最终的point.title = @"我在这里&#34 ;;那是在牙箍之外。因为如果我取出那两个point.title和point.sustitle行,它就不再打印引脚上的实际地理位置。
//Print the location to console
//NSLog(@"I am currently at %@",locatedAt);
point.title = locatedAt;
}
else {
NSLog(@"Could not locate");
}
}
];
point.title = @"Where am I";
// NSLog(@"point.title %@",point.title);
point.subtitle = @"I'm here!!!";
[self.mapView addAnnotation:point];
[self.mapView selectAnnotation:point animated:YES]; }