我可以使用以下代码在 MKMapView 上放置一个注释:
[self.geocoder geocodeAddressString:value completionHandler:^(NSArray *placemarks, NSError *error) {
if ([placemarks count] > i) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
location = placemark.location;
coordinate = location.coordinate;
coordinate.latitude = location.coordinate.latitude;
coordinate.longitude = location.coordinate.longitude;
MKCoordinateRegion newRegion;
newRegion.center.latitude = coordinate.latitude;
newRegion.center.longitude = coordinate.longitude;
newRegion.span.latitudeDelta = 0.029321;
newRegion.span.longitudeDelta = 0.034589;
//newRegion.span.latitudeDelta = 0.579321;
//newRegion.span.longitudeDelta = 1.234589;
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
[annotation setCoordinate:coordinate];
// AppDelegate *dataCenter = (AppDelegate *) [[UIApplication sharedApplication] delegate];
NSArray *web = [detail valueForKeyPath:@"Name"];
NSString *value = [web objectAtIndex:0];
[annotation setTitle:value];
}
}];
值是我传递的地址。
但是现在,我想使用相同的代码(可能在一个)for 循环中,这样我就可以对从 mysql 数据库的数组中获得的所有地址值进行地理编码。请指导我如何做到这一点
更新:
我从数组中的 sql 数据库中获取值。
$count = [web count];
for (int i=0; i< count ; i++) {
NSString *value = [web objectAtIndex:i];
[self.geocoder geocodeAddressString:value completionHandler:^(NSArray *placemarks, NSError *error) {
if ([placemarks count] > i) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
location = placemark.location;
coordinate = location.coordinate;
coordinate.latitude = location.coordinate.latitude;
coordinate.longitude = location.coordinate.longitude;
MKCoordinateRegion newRegion;
newRegion.center.latitude = coordinate.latitude;
newRegion.center.longitude = coordinate.longitude;
newRegion.span.latitudeDelta = 0.029321;
newRegion.span.longitudeDelta = 0.034589;
//newRegion.span.latitudeDelta = 0.579321;
//newRegion.span.longitudeDelta = 1.234589;
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
[annotation setCoordinate:coordinate];
// AppDelegate *dataCenter = (AppDelegate *) [[UIApplication sharedApplication] delegate];
NSArray *web = [detail valueForKeyPath:@"Name"];
NSString *value = [web objectAtIndex:0];
[annotation setTitle:value];
}
}];
}
完整的方法::
-(void) showSourceDest {
if (!self.geocoder) {
self.geocoder = [[CLGeocoder alloc] init];
}
AppDelegate *dataCenter = (AppDelegate *) [[UIApplication sharedApplication] delegate];
NSLog(@"Opening: %@", dataCenter.data);
[self setTitle:@"Map"];
NSString *address = dataCenter.data;
[self.geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
if ([placemarks count] > 0) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
location = placemark.location;
coordinate = location.coordinate;
coordinate.latitude = location.coordinate.latitude;
coordinate.longitude = location.coordinate.longitude;
//for first co-ordinate :: SOURCE
MKCoordinateRegion newRegion;
newRegion.center.latitude = coordinate.latitude;
newRegion.center.longitude = coordinate.longitude;
newRegion.span.latitudeDelta = 0.029321;
newRegion.span.longitudeDelta = 0.034589;
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
[annotation setCoordinate:coordinate];
AppDelegate *dataCenter = (AppDelegate *) [[UIApplication sharedApplication] delegate];
[annotation setTitle:dataCenter.hotelname];
[self.mapView addAnnotation:annotation];
//second annotation for SDSU :: DESTINATION
location2 = placemark.location;
coordinate2 = location.coordinate;
coordinate2.latitude = 32.774774;
coordinate2.longitude = -117.072262;
MKCoordinateRegion collRegion;
collRegion.center.latitude = 32.774774;
collRegion.center.longitude = -117.072262;
collRegion.span.latitudeDelta = 0.029321;
collRegion.span.longitudeDelta = 0.044589;
MKPointAnnotation *annotation2 = [[MKPointAnnotation alloc] init];
[annotation2 setCoordinate:coordinate2];
[annotation2 setTitle:@"SDSU"];
[self.mapView addAnnotation:annotation2];
NSArray *web = [detail valueForKeyPath:@"Address"];
NSLog(@"NO OF VALUES:: %@", web);
int count = [web count];
//Other place of interest nearby
for (int i = 0; i < count; i++)
{
NSString *value = [web objectAtIndex:i];
[self.geocoder geocodeAddressString:value completionHandler:^(NSArray *placemarks, NSError *error) {
if ([placemarks count] > 0) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
location = placemark.location;
coordinate = location.coordinate;
coordinate.latitude = location.coordinate.latitude;
coordinate.longitude = location.coordinate.longitude;
MKCoordinateRegion newRegion;
newRegion.center.latitude = coordinate.latitude;
newRegion.center.longitude = coordinate.longitude;
newRegion.span.latitudeDelta = 0.029321;
newRegion.span.longitudeDelta = 0.034589;
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
[annotation setCoordinate:coordinate];
[annotation setTitle:value];
[self.mapView addAnnotation:annotation];
[self.mapView setRegion:collRegion animated:YES];
}
}];
}
}
}];
}
for 循环内的块只执行一次!:( 不确定,但是否因为块变量没有得到更新?