Quickly adding single pin to MKMapView?

后端 未结 6 1172
孤城傲影
孤城傲影 2020-12-13 08:27

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

6条回答
  •  感情败类
    2020-12-13 09:07

    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 version:

    let annotation = MKPointAnnotation()
    let centerCoordinate = CLLocationCoordinate2D(latitude: 41, longitude:29)
    annotation.coordinate = centerCoordinate
    annotation.title = "Title"
    mapView.addAnnotation(annotation)
    

提交回复
热议问题