#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController<MKMapViewDelegate,CLLocationManagerDelegate>{
MKPointAnnotation *point;
}
@property (nonatomic,strong)CLLocationManager *locationManager;
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@end
-----------------------------------
- (void)viewDidLoad {
[super viewDidLoad];
_locationManager = [[CLLocationManager alloc]init];
[_locationManager requestWhenInUseAuthorization];
_locationManager.delegate=self;
[_locationManager startUpdatingLocation];
_mapView.showsUserLocation = YES;
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressHandler:)];
longPress.minimumPressDuration = 1.5;
[self.mapView addGestureRecognizer:longPress];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)showCurrentLocation{
}
-(void)moveMapToUserLocation{
//***********Move Map to the Center using SerCenter Method
// [_mapView setCenterCoordinate:_mapView.userLocation.location.coordinate animated:YES];
//**********Alternate Way Using Region
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(_mapView.userLocation.location.coordinate, 100, 100);
[_mapView setRegion:region];
}
-(void)addAnnotaionCoordinate:(CLLocationCoordinate2D)locationCoordinate{
if(point){
[point setCoordinate:locationCoordinate];
return;
}
point = [[MKPointAnnotation alloc] init];
point.coordinate = locationCoordinate;
point.title = @"Where am I?";
point.subtitle = @"I'm here!!!";
[_mapView addAnnotation:point];
}
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
[self moveMapToUserLocation];
}
-(void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views{
}
-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
NSLog(@"Annotation did Selected : %@ At location : %f,%f",view.reuseIdentifier,view.annotation.coordinate.latitude,view.annotation.coordinate.longitude);
}
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView *pinView = nil;
if(annotation != _mapView.userLocation) {
static NSString *defaultPinID = @"myLocation";
pinView = (MKAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil )
pinView = [[MKAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:defaultPinID];
pinView.canShowCallout = YES;
pinView.image = [UIImage imageNamed:@"pin.png"];
}
return pinView;
}
//Called when Loading of Map is completed
-(void)mapViewDidFinishLoadingMap:(MKMapView *)mapView{
[self addAnnotaionCoordinate:_mapView.userLocation.location.coordinate];
}
-(void)longPressHandler:(UILongPressGestureRecognizer*)sender{
CGPoint pntLongPress = [sender locationInView:_mapView];
//Conversion of CGPoint to CLLocationCoordinate2D
CLLocationCoordinate2D locationOfLongPress = [_mapView convertPoint:pntLongPress toCoordinateFromView:_mapView];
[self addAnnotaionCoordinate:locationOfLongPress];
}
@end
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController<MKMapViewDelegate,CLLocationManagerDelegate>{
MKPointAnnotation *point;
}
@property (nonatomic,strong)CLLocationManager *locationManager;
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@end
-----------------------------------
- (void)viewDidLoad {
[super viewDidLoad];
_locationManager = [[CLLocationManager alloc]init];
[_locationManager requestWhenInUseAuthorization];
_locationManager.delegate=self;
[_locationManager startUpdatingLocation];
_mapView.showsUserLocation = YES;
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressHandler:)];
longPress.minimumPressDuration = 1.5;
[self.mapView addGestureRecognizer:longPress];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)showCurrentLocation{
}
-(void)moveMapToUserLocation{
//***********Move Map to the Center using SerCenter Method
// [_mapView setCenterCoordinate:_mapView.userLocation.location.coordinate animated:YES];
//**********Alternate Way Using Region
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(_mapView.userLocation.location.coordinate, 100, 100);
[_mapView setRegion:region];
}
-(void)addAnnotaionCoordinate:(CLLocationCoordinate2D)locationCoordinate{
if(point){
[point setCoordinate:locationCoordinate];
return;
}
point = [[MKPointAnnotation alloc] init];
point.coordinate = locationCoordinate;
point.title = @"Where am I?";
point.subtitle = @"I'm here!!!";
[_mapView addAnnotation:point];
}
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
[self moveMapToUserLocation];
}
-(void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views{
}
-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
NSLog(@"Annotation did Selected : %@ At location : %f,%f",view.reuseIdentifier,view.annotation.coordinate.latitude,view.annotation.coordinate.longitude);
}
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView *pinView = nil;
if(annotation != _mapView.userLocation) {
static NSString *defaultPinID = @"myLocation";
pinView = (MKAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil )
pinView = [[MKAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:defaultPinID];
pinView.canShowCallout = YES;
pinView.image = [UIImage imageNamed:@"pin.png"];
}
return pinView;
}
//Called when Loading of Map is completed
-(void)mapViewDidFinishLoadingMap:(MKMapView *)mapView{
[self addAnnotaionCoordinate:_mapView.userLocation.location.coordinate];
}
-(void)longPressHandler:(UILongPressGestureRecognizer*)sender{
CGPoint pntLongPress = [sender locationInView:_mapView];
//Conversion of CGPoint to CLLocationCoordinate2D
CLLocationCoordinate2D locationOfLongPress = [_mapView convertPoint:pntLongPress toCoordinateFromView:_mapView];
[self addAnnotaionCoordinate:locationOfLongPress];
}
@end