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 ループ内のブロックは 1 回だけ実行されます。:(よくわかりませんが、ブロック変数が更新されていないためですか?

4

2 に答える 2

1

ここにいくつかの問題があります:

  • iは、データベースの住所の配列内のインデックスであり、1つの住所のジオコーディングから返される目印全体をカウントするために使用することはできません。49番目の住所にいるという理由だけで、その住所がジオコーディングされた場所から50個の目印を生成する可能性はほとんどありません。
  • 変数はどこdetailから来て、ループのすべての反復で同じ値を取得するのはなぜですか?
  • 配列はすでにforループのwebターゲットとして使用されているため、ループ内で再宣言することは期待できません。

これらの問題を解決すれば、残りのコードを自分で解決できる可能性があります。また、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 に答える