iOS 5:基于mysql数据库的地址进行地理编码

时间:2012-10-09 07:15:59

标签: ios annotations mkmapview geocoding

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

2 个答案:

答案 0 :(得分:1)

这里有几个问题:

  • i是数据库地址数组中的索引,它不能用于统计从一个地址进行地理编码返回的地标。仅仅因为你在第49个地址,该地址不太可能从地理编码产生50个地标。
  • 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];