【问题标题】:Forward and Reverse geocoding in single app : Need Direction单个应用程序中的正向和反向地理编码:需要方向
【发布时间】:2016-05-16 09:25:07
【问题描述】:

我在单击按钮时进行了反向地理编码,使用 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_;
            }
        }];


    }

【问题讨论】:

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


    【解决方案1】:

    我找到了一种方法,它现在可以工作了。如果我以凌乱的方式进行操作,请纠正我。 如果有人想这样做,这就是答案:

    #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_;
                    }
                }];
    
            }
    
        }];
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 2011-04-26
      • 2015-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多