在Map Annotation视图上设置标题和副标题

时间:2014-05-03 12:34:12

标签: ios mapkit mkannotation mkannotationview

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

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

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

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

2 个答案:

答案 0 :(得分:3)

改为使用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];

答案 1 :(得分:0)

您可以尝试以下代码。它可能对你有帮助。

//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];