我可以使用以下代码在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];
}
}];
值是我通过的地址。
但是现在,我想使用相同的代码(可能在a中)for循环,这样我就可以从mysql数据库中对从数组中获取的地址的所有值进行地理编码。请指导我怎么做 所以
更新:
我从数据库中获取数据库中的值。
$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循环中的块只执行一次! :( 不确定,但是因为块变量没有得到更新吗?
答案 0 :(得分:1)
这里有几个问题:
detail
变量来自何处,为什么在循环的每次迭代中都会得到相同的值?web
数组已经被用作for循环的目标,你不能期望在循环中重新声明它。如果您解决了这些问题,您可以自己解决剩下的代码问题。此外,CLGeocoder一次限制您50个请求。如果你做了那么多,你应该考虑将坐标与地址字符串一起存储在数据库中。
答案 1 :(得分:0)
问题解决了:
MKCoordinateRegion newRegion;
newRegion.center.latitude = [[[detail valueForKey:@"Latitude"] objectAtIndex:i] doubleValue];
newRegion.center.longitude = [[[detail valueForKey:@"Longitude"] objectAtIndex:i] doubleValue];
其中:
NSDictionary *detail;
基本上,[doubleValue];