Search code examples
iosmapkitmapkitannotation

Default location in map-kit in ios


I am new to IOS i need to show default location with my latitude and longitude(12.940358, 80.208647). with annotation pin.

.h file:

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
@interface Map_view : UIViewController<MKMapViewDelegate,CLLocationManagerDelegate>{
    CLLocationManager *locationmgr;
    CLGeocoder *geocode;
    CLPlacemark *placemark;

}
@property(nonatomic,retain)IBOutlet MKMapView *map11;

.m file:

@synthesize map11;

I don't no where to put my latitude and longitude and how to use please help me.


Solution

  • If you want to show your location with your lat & long with Annotation With Name.

    Use this code.

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        mapVW.showsUserLocation = YES;
        CLLocation *loc = [[CLLocation alloc]initWithLatitude:12.940358 longitude:80.208647];
    
        [mapVW setCenterCoordinate:loc.coordinate animated:YES];
    
        CLGeocoder *ceo = [[CLGeocoder alloc]init];
        [ceo reverseGeocodeLocation:loc
                  completionHandler:^(NSArray *placemarks, NSError *error) {
                      CLPlacemark *placemark = [placemarks objectAtIndex:0];
    
                      //String to hold address
                     NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
                     NSString *cityName = placemark.locality;
    
                      MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
                      annotationPoint.coordinate = CLLocationCoordinate2DMake(12.940358, 80.208647);
                      annotationPoint.title = locatedAt;
    
                      MKCoordinateRegion region;
    
                      MKCoordinateSpan span1;
                      span1.latitudeDelta =  0.08;
                      span1.longitudeDelta = 0.08;
                      region.span = span1;
                      region.center = annotationPoint.coordinate;
                      [mapVW setRegion:region animated:TRUE];
                      [mapVW regionThatFits:region];
    
                      [mapVW addAnnotation:annotationPoint];
                      [mapVW selectAnnotation:annotationPoint animated:NO];
                  }
         ];
    
    }