在MKAnnotation的幫助下,可以解決這個問題。感謝@Craig和Vishal Kurup!
創建一個註釋類作爲NSObject的子類。
在Annotation.h:
@interface Annotation : NSObject <MKAnnotation>
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, assign) NSString *title;
@property (nonatomic, assign) NSString *subtitle;
@end
應該符合MKAnnotation和三個屬性是符合MKAnnotation上課的時候都需要的人。聲明它們並將它們合成到.m文件中。
在MapHistoryViewController實現文件中,我們需要添加幾行代碼來查看所需座標處的註釋。
MapHistoryViewController.m:
@interface MapHistoryViewController(){
CLLocationCoordinate2D annotationCoord;
Annotation *annotation;
}
@end
@implementation MapHistoryViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// To define coordinate for the annotation
annotationCoord.latitude = mapHistoryLocation.coordinate.latitude;
annotationCoord.longitude = mapHistoryLocation.coordinate.longitude;
annotation = [Annotation alloc];
annotation.coordinate = annotationCoord;
annotation.title = streetAnnotation;
// to display the annotation
[self.mapHistoryView addAnnotation:annotation];
// where mapHistoryView is my MKMapView object
}
//You can set the region too, if you want the map to be focused on the coordinates you have provided
//Hope this will help the people with the same question :)
你有沒有看蘋果公司在其網站上的示例代碼? – Craig
嘿克雷格!我其實在Apple的網站上查看了iOS的核心數據教程。它實際上說如何保存位置數據並刪除它們。但是,我仍然在尋找一個處理核心位置和Mapkit的緊密例子。這可能會訣竅..謝謝:) – Raj0689
我懷疑你缺少的是,在地圖上顯示的東西被稱爲「註釋」,所以如果你搜索「如何顯示在MKMapView註釋」你會得到大量的結果。如果他們不夠清楚,你應該閱讀蘋果文檔中的「註釋地圖」部分(http://developer.apple.com/library/ios/#DOCUMENTATION/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html ) – Craig