Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MKMapview place pin at location (long/lat)

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?

like image 428
3sl Avatar asked Sep 18 '11 06:09

3sl


People also ask

How can I get current location pin in Swift?

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.

What is MapKit in iOS?

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.


2 Answers

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];
like image 171
Jhaliya - Praveen Sharma Avatar answered Oct 20 '22 09:10

Jhaliya - Praveen Sharma


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;
}
like image 38
Infinite Possibilities Avatar answered Oct 20 '22 09:10

Infinite Possibilities