【问题标题】:Setting the title and subtitle on a Map Annotation view在地图注释视图上设置标题和副标题
【发布时间】:2014-06-20 02:26:45
【问题描述】:

我关注了这个问题:iOS - MKMapView place annotation by using address instead of lat / long - 为邮政编码创建地图注释,而不是直接使用 long/lat 值。

这很好,但是我想设置anno的标题和副标题

CLPlacemark *topResult = [placemarks objectAtIndex:0];
MKPlacemark *placemark = [[MKPlacemark alloc] 
placemark.title = self.business.businessName;
placemark.subtitle = self.business.phoneNumber;

这不起作用,因为标题和副标题是只读的。如何更改上述内容以便我能够设置标题和副标题?

【问题讨论】:

标签: ios mapkit mkannotation mkannotationview


【解决方案1】:

请改用MKPointAnnotation

示例代码:

CLPlacemark *topresult = [placemarks objectAtIndex:0];
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
annotation.coordinate = topresult.location.coordinate;
annotation.title = self.business.businessName;
annotation.subtitle = self.business.phoneNumber;
[self.mapView addAnnotation:annotation];

【讨论】:

  • 您不必“必须”专门使用 MKPointAnnotation - 只是一些实现 MKAnnotation 并具有可设置标题和副标题的类(如 MKPointAnnotation,但您可以创建自己的类)。另外,locationManager.location.coordinate 应该是 topResult.location.coordinate
  • @安娜:是的。更新。谢谢。
【解决方案2】:

你可以试试下面的代码。它可能会帮助你。

//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 = @"Address";
toAdd.subtitle = @"Sub Address";

[self.map addAnnotation:toAdd];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 2012-12-27
    • 1970-01-01
    • 1970-01-01
    • 2016-02-09
    相关资源
    最近更新 更多