【问题标题】:Show address in annotation when pin is dropped on map在地图上放置图钉时在注释中显示地址
【发布时间】:2013-12-17 22:07:56
【问题描述】:

目前我可以在地图周围放置图钉。现在我希望注释标题显示图钉放置的地址。

我看过这个,但无法让我的工作:
Set annotation's title as current address

我的 ViewController.m 中的代码

已更新。

- (void)addPinToMap:(UIGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer.state != UIGestureRecognizerStateBegan)
        return;

    CGPoint touchPoint = [gestureRecognizer locationInView:self.map];
    CLLocationCoordinate2D touchMapCoordinate =
    [self.map convertPoint:touchPoint toCoordinateFromView:self.map];

    CLLocation *currentLocation = [[CLLocation alloc]
                                   initWithLatitude:touchMapCoordinate.latitude
                                   longitude:touchMapCoordinate.longitude];

    [self.geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemark, NSError *error) {

        //initialize the title to "unknown" in case geocode has failed...
        NSString *annTitle = @"Address unknown";

        //set the title if we got any placemarks...
        if (placemark.count > 0)
        {
            CLPlacemark *topResult = [placemark objectAtIndex:0];
            annTitle = [NSString stringWithFormat:@"%@ %@ %@ %@", topResult.country, topResult.locality, topResult.subLocality, topResult.thoroughfare];
        }

        //now create the annotation...
        MapAnnotation *toAdd = [[MapAnnotation alloc]init];

        toAdd.coordinate = touchMapCoordinate;
        toAdd.title = annTitle;
        //toAdd.title = @"Title";
        toAdd.subtitle = @"Subtitle";

        [self.map addAnnotation:toAdd];
    }];
}

【问题讨论】:

  • 我目前正在尝试做同样的事情!!

标签: ios mkmapview mkannotation mkannotationview clgeocoder


【解决方案1】:

首先,在addPinToMap: 方法中,addressLocation 使用currentLocation 调用,但从未设置currentLocation。它声明了几行,但没有设置任何值。

所以改变:

CLLocation *currentLocation;

到:

CLLocation *currentLocation = [[CLLocation alloc] 
                                initWithLatitude:touchMapCoordinate.latitude 
                                longitude:touchMapCoordinate.longitude];


第二,即使有了这个修复,它仍然无法正常工作。注释的title 不会被设置,因为reverseGeocodeLocation 方法的完成处理程序块将在注释已经添加后完成(该块是异步的——addPinToMap: 中的代码不会等待它完成)。

当您实际获得地理编码器结果(无论是成功还是失败)时,您需要稍微更改代码并在完成块中添加注释

reverseGeocodeLocation 调用移至addPinToMap: 方法:

CLLocation *currentLocation = [[CLLocation alloc] 
                                initWithLatitude:touchMapCoordinate.latitude 
                                longitude:touchMapCoordinate.longitude];

[self.geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemark, NSError *error) {

    //initialize the title to "unknown" in case geocode has failed...
    NSString *annTitle = @"Address unknown";

    //set the title if we got any placemarks...
    if (placemark.count > 0)
    {
        CLPlacemark *topResult = [placemark objectAtIndex:0];
        annTitle = [NSString stringWithFormat:@"%@ %@ %@ %@", topResult.country, topResult.locality, topResult.subLocality, topResult.thoroughfare];
    }

    //now create the annotation...
    MapAnnotation *toAdd = [[MapAnnotation alloc]init];

    toAdd.coordinate = touchMapCoordinate;
    toAdd.title = annTitle;
    toAdd.subtitle = @"Subtitle";

    [self.map addAnnotation:toAdd];
}];

【讨论】:

  • 太好了,但我仍然无法显示图钉,移动代码也无济于事,如果我用正常的标题和副标题注释屏蔽了“reverseGeocodeLocation”调用,图钉会掉落像之前一样。有什么建议吗?
  • 您将答案中显示的代码放在 addPinToMap 方法中 convertPoint 行之后,对吗?请将更新后的代码添加到问题中。
  • 看起来不错。要解决此问题,请执行以下操作: 1) 在调用 reverseGeocodeLocation 之前输入NSLog(@"self.geocoder=%@", self.geocoder);。 2) 将NSLog(@"geocoder error=%@", error); 放在NSString *annTitle = ... 正上方的完成块中。让我知道 NSLog 打印出来的内容。
  • NSLog 打印:2013-12-18 22:03:07.567 Track[20881:70b] self.geocoder=(null)
  • Iv 现在添加了 'self.geocoder = [[CLGeocoder alloc] init];'进入'-(void)viewDidLoad'并且它的工作很好。您的帮助非常好,谢谢。
猜你喜欢
  • 2016-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多