单个应用中的正向和反向地理编码:需要方向

时间:2016-05-16 09:25:07

标签: ios objective-c google-maps geocoding core-location

我使用CLLocationManagerDelegate在单击按钮时进行了反向地理编码。但是现在我拿了一个UISearchBar从用户那里取名,按钮点击我想显示用户输入的地方和标记。 由于地理编码需要一些时间没有值存储在位置,但应用程序完成其工作。 所以有人能告诉我该怎么做吗?如果我在任何地方都错了,请纠正我。

   - (IBAction)searchLocation:(id)sender {

        NSString *address = self.searchBar.text;
        NSLog(@"%@",address);

        [forwardGeocoder geocodeAddressString:address completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {

            if ([placemarks count]>0) {

                CLPlacemark *placeMarkForward = [placemarks objectAtIndex:0];

                locationValue = placeMarkForward.location;
                NSLog(@"Location : :%@",locationValue);

            }
        }];

        [reverseGeocoder reverseGeocodeLocation:locationValue completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {

            if (error == nil && [placemarks count]>0) {
                CLPlacemark *placeMarkReverse = [placemarks lastObject];
                CLLocation *currentLocation;
                marker.position = CLLocationCoordinate2DMake(currentLocation.coordinate.latitude,currentLocation.coordinate.longitude);
                marker.title = [NSString stringWithFormat:@"%@",placeMarkReverse.country];

                marker.snippet = [NSString stringWithFormat:@"%@\n%@", placeMarkReverse.locality,placeMarkReverse.postalCode];
                marker.map = mapView_;
            }
        }];


    }

1 个答案:

答案 0 :(得分:0)

我找到了一种方法,现在正在运作。如果我以凌乱的方式做这件事,请纠正我。 如果有人想要这样做,这就是答案:

#pragma mark - Serach Location -
- (IBAction)searchLocation:(id)sender {

    NSString *address = self.searchBar.text;
    NSLog(@"%@",address);



    [forwardGeocoder geocodeAddressString:address completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {

        if ([placemarks count]>0) {

            CLPlacemark *placeMarkForward = [placemarks objectAtIndex:0];

            locationValue = placeMarkForward.location;
            NSLog(@"Location : :%@",locationValue);

            [reverseGeocoder reverseGeocodeLocation:locationValue completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {

                if (error == nil && [placemarks count]>0) {
                    CLPlacemark *placeMarkReverse = [placemarks lastObject];

                    NSLog(@"Testing");
                    [mapView_ animateToLocation:CLLocationCoordinate2DMake(locationValue.coordinate.latitude,locationValue.coordinate.longitude)];
                    marker.position = CLLocationCoordinate2DMake(locationValue.coordinate.latitude,locationValue.coordinate.longitude);
                    marker.title = [NSString stringWithFormat:@"%@",placeMarkReverse.locality];

                    marker.snippet = [NSString stringWithFormat:@"%@\n%@", placeMarkReverse.postalCode,placeMarkReverse.country];
                    marker.map = mapView_;
                }
            }];

        }

    }];


}