Reputation: 1288
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;
Upvotes: 1
Views: 1629
Reputation:
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.
Upvotes: 2
Reputation: 6286
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.
Upvotes: 0