1

How can I determine the coordinates of a certain spot? How can I create a pin for 1 specific location after knowing it's coordinates? Is creating a new class for that necessary?

ex- PIN to : latitude = 37.786996; longitude = -122.440100;

Dani
  • 1,228
  • 1
  • 16
  • 26

2 Answers2

2

To add a basic pin at a given coordinate, the simplest way in iOS 4+ is to use the pre-defined MKPointAnnotation class (no need to define your own class) and call addAnnotation:.

If you need to define your own annotation class because you need some custom properties, don't use the classes shown in the MapCallouts sample app as a basis. They give the false impression that you need to define a separate class for each unique coordinate. Instead, create a class that implements the MKAnnotation protocol but with a settable coordinate property.

Edit:
An example with MKPointAnnotation:

MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
annotation.coordinate = CLLocationCoordinate2DMake(33, 35);
annotation.title = @"Title";
annotation.subtitle = @"Subtitle";
[mapView addAnnotation:annotation];
[annotation release];

If you need to easily access the annotation in other methods in the class, you can make it an ivar instead.

  • how can i create a MKPointAnnotation of coordinates 33 , 35 to be able to use the addAnnotation later on? – Dani Jul 24 '11 at 18:53
  • How to add multiple pins in an MKMapView? – Pradeep Reddy Kypa Oct 04 '12 at 14:47
  • @PradeepReddyKypa, You should ask a new question with info on where your multiple pin data is (plist, sql, array, dictionary, other) and what you've tried. Basically, call addAnnotation repeatedly for each annotation (eg. in a loop). Searching on SO should give you numerous examples. –  Oct 04 '12 at 14:58
  • @AnnaKarenina : Thanks for the response. I have asked my question at "http://stackoverflow.com/questions/12730400/how-to-display-and-connect-multiple-locations-with-a-route-with-annotations-in-a" . My data is hardcoded. I cant repeat that inside a loop as the location details(lat and long) are different. – Pradeep Reddy Kypa Oct 04 '12 at 15:16
0

You implement an object that supports the MKMapAnnotation protocol. This object you add to your mkmapview with the "addAnnotation:" call. This will give you the default pin on the map.

If you want to customize the pin you need to implement the MKMapViewDelegate protocol to return an MKAnnotationView with a custom image.

Joris Mans
  • 6,024
  • 6
  • 42
  • 69