Quickly add single output to MKMapView?

I have a GPS coordinate (latitude, longitude), and I quickly want to place one pin on MKMapView, showing this position. Everything works fine, but since I only need one output without a callout, is there a faster way to do this, or is it lower than what I need to do?

- (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; } 

Note. I don’t need to check reuse annotation views, since I use only output to show the position in the detail view (which is destroyed and recreated the next time the detail view is requested).

+47
objective-c iphone cocoa-touch mkmapview
Mar 27 2018-12-12T00:
source share
5 answers

Instead of using the -mapView:viewForAnnotation: simply put the code for MKPointAnnotation in your -viewDidLoad method. This will not animate the fall, 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]; 

Quick version:

 let annotation = MKPointAnnotation() let centerCoordinate = CLLocationCoordinate2D(latitude: 41, longitude:29) annotation.coordinate = centerCoordinate annotation.title = "Title" mapView.addAnnotation(annotation) 
+106
Mar 27 '12 at 18:38
source share

You can set the point as well as the area as follows:

 CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(lat, lon); MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1); MKCoordinateRegion region = {coord, span}; MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init]; [annotation setCoordinate:coord]; [self.staticMapView setRegion:region]; [self.staticMapView addAnnotation:annotation]; 
+28
Mar 18 '14 at 22:40
source share

The easiest and easiest way to dump the output and display the current location in the map view is to simply add this code to your viewDidLoad method. (Assuming you have already selected your current location).

 NSString *display_coordinates=[NSString stringWithFormat:@"Latitude is %f and Longitude is %f",coordinate.longitude,coordinate.latitude]; MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init]; [annotation setCoordinate:coordinate]; [annotation setTitle:@"Click Labs"]; [annotation setSubtitle:display_coordinates]; [mapView addAnnotation:annotation]; 
+5
Jan 21 '14 at 6:38
source share

Since you want to animate the fall, you need to implement viewForAnnotation as you did, because by default it is a NO property.

If you don’t need to animate the fall, you can completely eliminate the implementation of the viewForAnnotation method and disable the callout, set the title annotation to nil or blank when adding the annotation instead.

If you need pin lowering animation and you also show the user's location (blue dot), you also need to check MKUserLocation in viewForAnnotation and return nil .

Otherwise, you can delete the entire viewForAnnotation method, and the red icon will appear by default without animation, the callout will show if the title not empty, and the user's location will be displayed as a blue dot.

+3
Mar 27 2018-12-12T00:
source share

Fast version of add buffer in MKMapView:

 func addPin() { let annotation = MKPointAnnotation() let centerCoordinate = CLLocationCoordinate2D(latitude: 20.836864, longitude:-156.874269) annotation.coordinate = centerCoordinate annotation.title = "Lanai, Hawaii" mapView.addAnnotation(annotation) } 

Fast version of focus area or pin

 func focusMapView() { let mapCenter = CLLocationCoordinate2DMake(20.836864, -156.874269) let span = MKCoordinateSpanMake(0.1, 0.1) let region = MKCoordinateRegionMake(mapCenter, span) mapView.region = region } 

For more information, visit " Download MKMapView with Swift ".

+2
Sep 25 '16 at 13:37
source share



All Articles