快速添加单引脚到MKMapView? [英] Quickly adding single pin to MKMapView?
问题描述
我有一个GPS坐标(纬度,经度),我很快想在MKMapView上显示一个位置。一切都很好,但因为我只需要一个针,没有callout是有更快的方式来做到这一点,或者是我下面需要做什么?
I have a GPS coordinate (latitude, longitude) and I quickly want to place a single pin on a MKMapView showing that position. Everything works just fine, but as I only need a single pin with no callout is there a quicker way to do this or is what I have below what needs to be done?
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id < MKAnnotation >)annotation {
MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"DETAILPIN_ID"];
[pinView setAnimatesDrop:YES];
[pinView setCanShowCallout:NO];
return pinView;
}
NB:我不需要检查可重复使用的注释视图只有使用该引脚在细节视图中显示位置(下一次请求详细视图时将其销毁并重新创建)。
NB: I don't need to check for reusable annotation views as I am only using the pin to show a position in a detail view (which is destroyed and recreated the next time a detail view is requested).
推荐答案
不要使用 -mapView:viewForAnnotation:
方法,只需将 MKPointAnnotation
code> -viewDidLoad 方法。
Instead of using the -mapView:viewForAnnotation:
method, just put the code for an MKPointAnnotation
into your -viewDidLoad
method. It won't animate the drop, but it is very easy.
// Place a single pin
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
[annotation setCoordinate:centerCoordinate];
[annotation setTitle:@"Title"]; //You can set the subtitle too
[self.mapView addAnnotation:annotation];
Swift版本:
let annotation = MKPointAnnotation()
let centerCoordinate = CLLocationCoordinate2D(latitude: 41, longitude:29)
annotation.coordinate = centerCoordinate
annotation.title = "Title"
mapView.addAnnotation(annotation)
这篇关于快速添加单引脚到MKMapView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!