I have latitude and long values and I need to be able to drop a pin at this location.
Can anybody provide some advice on how to go about this?
You can do like this: import UIKit import MapKit class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate { let locationManager = CLLocationManager() // Here it is an object to get the user's location. @IBOutlet weak var mapView: MKMapView! // It is my map.
MapKit is a powerful API available on iOS devices that makes it easy to display maps, mark locations, enhance with custom data and even draw routes or other shapes on top.
Find the below very simple solution to drop the pin at given location define by CLLocationCoordinate2D
Drop Pin on MKMapView
Edited:
CLLocationCoordinate2D ctrpoint;
ctrpoint.latitude = 53.58448;
ctrpoint.longitude =-8.93772;
AddressAnnotation *addAnnotation = [[AddressAnnotation alloc] initWithCoordinate:ctrpoint];
[mapview addAnnotation:addAnnotation];
[addAnnotation release];
You should:
1. add the MapKit framework to your project.
2. create a class which implements the MKAnnotation protocol.
Sample:
@interface Annotation : NSObject <MKAnnotation> {
NSString *_title;
NSString *_subtitle;
CLLocationCoordinate2D _coordinate;
}
// Getters and setters
- (void)setTitle:(NSString *)title;
- (void)setSubtitle:(NSString *)subtitle;
@end
@implementation Annotation
#pragma mark -
#pragma mark Memory management
- (void)dealloc {
[self setTitle:nil];
[self setSubtitle:nil];
[super dealloc];
}
#pragma mark -
#pragma mark Getters and setters
- (NSString *)title {
return _title;
}
- (NSString *)subtitle {
return _subtitle;
}
- (void)setTitle:(NSString *)title {
if (_title != title) {
[_title release];
_title = [title retain];
}
}
- (void)setSubtitle:(NSString *)subtitle {
if (_subtitle != subtitle) {
[_subtitle release];
_subtitle = [subtitle retain];
}
}
- (CLLocationCoordinate2D)coordinate {
return _coordinate;
}
- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate {
_coordinate = newCoordinate;
}
@end
2. create an instance of this class and set the lat/lon property
3. add the instance to the MKMapView object with this method:
- (void)addAnnotation:(id<MKAnnotation>)annotation
4. You should set the delegate of the map and implement the following method:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
if ([annotation isKindOfClass:[MKUserLocation class]]) {
return nil;
}
static NSString* ShopAnnotationIdentifier = @"shopAnnotationIdentifier";
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:ShopAnnotationIdentifier];
if (!pinView) {
pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:ShopAnnotationIdentifier] autorelease];
pinView.pinColor = MKPinAnnotationColorRed;
pinView.animatesDrop = YES;
}
return pinView;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With