0

我可以使用以下代码在 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 循环内的块只执行一次!:( 不确定,但是否因为块变量没有得到更新?

4

2 回答 2

1

这里有几个问题:

  • i 是数据库地址数组中的索引,它不能用于计算从地理编码一个地址返回的所有地标。仅仅因为您位于第 49 个地址,该地址不太可能从地理编码中生成 50 个地标。
  • detail变量从何而来,为什么在循环的每次迭代中都得到相同的值?
  • web数组已被用作 for 循环的目标,您不能期望在循环中重新声明它。

如果你解决了这些问题,你也许可以自己解决剩下的代码。CLGeocoder 也将您一次限制为 50 个请求。如果你做了那么多,你应该考虑将坐标与地址字符串一起存储在数据库中。

于 2012-10-09T22:34:04.813 回答
0

问题解决了:

MKCoordinateRegion newRegion;
newRegion.center.latitude = [[[detail valueForKey:@"Latitude"] objectAtIndex:i] doubleValue];
newRegion.center.longitude = [[[detail valueForKey:@"Longitude"] objectAtIndex:i] doubleValue];

在哪里 :

NSDictionary *detail;

所以基本上,[doubleValue];

于 2012-10-31T06:18:04.603 回答