MKMapview place pin at location (long/lat)
Asked Answered
A

6

18

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?

Alcock answered 18/9, 2011 at 6:10 Comment(0)
I
22

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];
Invidious answered 18/9, 2011 at 6:25 Comment(2)
Thanks for this. What is the syntax for using: initWithCoordinate:ctrpoint]; - i.e. how do I add my long/lat?Alcock
No need to create/use a custom class just for placing pins. You can use the apple provided MKPointAnnotation class.Penang
E
16

You should:
1. add the MapKit framework to your project. 2. create a class which implements the MKAnnotation protocol.
Sample:

Annotation.h

@interface Annotation : NSObject <MKAnnotation> {
    NSString *_title;
    NSString *_subtitle;

    CLLocationCoordinate2D _coordinate;
}

// Getters and setters
- (void)setTitle:(NSString *)title;
- (void)setSubtitle:(NSString *)subtitle;

@end

Annotation.m

@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;
}
Epigraphic answered 18/9, 2011 at 6:28 Comment(0)
O
5

This assumes that you have ARC enabled and that you have included the MapKit framework.

First create a class that implements the MKAnnotation protocol. We'll call it MapPinAnnotation.

MapPinAnnotation.h

@interface MapPinAnnotation : NSObject <MKAnnotation>

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, readonly) NSString* title;
@property (nonatomic, readonly) NSString* subtitle;

- (id)initWithCoordinates:(CLLocationCoordinate2D)location
                placeName:(NSString *)placeName
              description:(NSString *)description;

@end

MapPinAnnotation.m

@implementation MapPinAnnotation

@synthesize coordinate;
@synthesize title;
@synthesize subtitle;

- (id)initWithCoordinates:(CLLocationCoordinate2D)location
                placeName:(NSString *)placeName
              description:(NSString *)description;
{
  self = [super init];
  if (self)
  {    
    coordinate = location;
    title = placeName;
    subtitle = description;
  }

  return self;
}

@end

Then add the annotation to the map using the following:

CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(34.421496,
                                                              -119.70182);

MapPinAnnotation* pinAnnotation = 
  [[MapPinAnnotation alloc] initWithCoordinates:coordinate
                                      placeName:nil
                                    description:nil];
[mMapView addAnnotation:pinAnnotation];

The containing class will have to implement the MKMapViewDelegate protocol. In particular you will have to define the following function to draw the pin:

- (MKAnnotationView *)mapView:(MKMapView *)mapView 
            viewForAnnotation:(id <MKAnnotation>)annotation
{
  if ([annotation isKindOfClass:[MKUserLocation class]])
  {
    return nil;
  }

  static NSString* myIdentifier = @"myIndentifier";
  MKPinAnnotationView* pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:myIdentifier];

  if (!pinView)
  {
    pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:myIdentifier];
    pinView.pinColor = MKPinAnnotationColorRed;
    pinView.animatesDrop = NO;
  }
  return pinView;
}

In this example the MKAnnotation title and subtitle member variables are not used, but they can be displayed in the delegate function.

Ogawa answered 15/11, 2012 at 4:25 Comment(1)
I keep getting a -[MapPinAnnotation setCoordinate:]: unrecognized selector sent to instance error.Antimagnetic
P
3
-(MKPointAnnotation *)showClusterPoint:(CLLocationCoordinate2D)coords withPos:(NSString *)place
{
    float  zoomLevel = 0.5;
    region = MKCoordinateRegionMake (coords, MKCoordinateSpanMake (zoomLevel, zoomLevel));
    [mapView setRegion: [mapView regionThatFits: region] animated: YES];

    point = [[MKPointAnnotation alloc]init];
    point.coordinate = coords;
    point.title=place;

    [mapView addAnnotation:point];

    return point;
}
Preclinical answered 31/1, 2014 at 7:55 Comment(0)
D
2

Swift Version

        let location = CLLocationCoordinate2DMake(13.724362, 100.515342);
        let region = MKCoordinateRegionMakeWithDistance(location, 500.0, 700.0)
        self.mkMapView.setRegion(region, animated: true)
        // Drop a pin
        let dropPin = MKPointAnnotation();
        dropPin.coordinate = location;
        dropPin.title = "Le Normandie Restaurant";
        self.mkMapView.addAnnotation(dropPin);
Decrement answered 22/3, 2016 at 9:2 Comment(0)
R
0
Please use this code. its working fine.
-(void)addAllPinsOnMapView
{
MKCoordinateRegion region = mapViewOffer.region;
region.center = CLLocationCoordinate2DMake(23.0225, 72.5714);
region.span.longitudeDelta= 0.1f;
region.span.latitudeDelta= 0.1f;
[mapViewOffer setRegion:region animated:YES];
mapViewOffer.delegate=self;

MKPointAnnotation *mapPin = [[MKPointAnnotation alloc] init];
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(23.0225, 72.5714);
mapPin.title = @"Title";
mapPin.coordinate = coordinate;
[mapViewOffer addAnnotation:mapPin];
}
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:
(MKAnnotationView *)view
{
NSLog(@"%@",view.annotation.title);
NSLog(@"%f",view.annotation.coordinate.latitude);
NSLog(@"%f",view.annotation.coordinate.longitude);
}

- (MKAnnotationView *)mapView:(MKMapView *)theMapView 
viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
{
    ((MKUserLocation *)annotation).title = @"Current Location";
    return nil;
}
else
{
    MKAnnotationView *pinView = nil;
    static NSString *defaultPinID = @"annotationViewID";
    pinView = (MKAnnotationView *)[mapViewOffer dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
    if ( pinView == nil ){
        pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID];
    }
    pinView.canShowCallout = YES;
    pinView.image = [UIImage imageNamed:@"placeholder"];

    UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    //    [infoButton addTarget:self action:@selector(infoButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    pinView.rightCalloutAccessoryView = infoButton;
    pinView.rightCalloutAccessoryView.tag=1;
    return pinView;
}
}
- (MKPinAnnotationView*)myMap:(MKMapView*)myMap viewForAnnotation:
(id<MKAnnotation>)annotation{

MKPinAnnotationView *pin = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"CustomPin"];

UIImage *icon = [UIImage imageNamed:@"bustour.png"];
UIImageView *iconView = [[UIImageView alloc] initWithFrame:CGRectMake(8,0,32,37)];

if(icon == nil)
    NSLog(@"image: ");
else
    NSLog(@"image: %@", (NSString*)icon.description);

[iconView setImage:icon];
[pin addSubview:iconView];
pin.canShowCallout = YES;
pin.pinColor = MKPinAnnotationColorPurple;

return pin;
}

- (IBAction)btnLocateMe:(UIButton *)sender
{
[mapViewOffer setCenterCoordinate:mapViewOffer.userLocation.location.coordinate animated:YES];
}
Rejoice answered 24/11, 2017 at 3:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.