IOS/ Objective-c: issue on MKMapView

623 views Asked by At

I'm new at objective-c/IOS and i'm following this tutorial about implementing user's current location on a map. Followed the instructions, but when I press the button to see my current location, nothing happens. Can anybody give me a hint? I'm using the latest version of Xcode and i'm not sure if the one in the tutorial is the same...The code:

ViewController.m:

 #import "ViewController.h"
 #import "Pin.h"


 @interface ViewController ()

 @end

 @implementation ViewController;

 @synthesize mapview;

 - (void)viewDidLoad {
 [super viewDidLoad];
MKCoordinateRegion region={{0.0,0.0},{0.0,0.0}};
region.center.latitude=38.711995;
region.center.longitude=-9.144932;
region.span.longitudeDelta=0.01f;
region.span.latitudeDelta=0.01f;
[mapview setRegion:region animated:YES];

Pin*vega =[[Pin alloc] init];
vega.title=@"Capela";
 vega.subtitle=@"Bar";
vega.coordinate= region.center;
[mapview addAnnotation:vega];

 }


- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

 -(IBAction)SetMap:(id)sender{

 switch (((UISegmentedControl *) sender).selectedSegmentIndex) {
    case 0:
        mapview.mapType=MKMapTypeStandard;
        break;

    case 1:
        mapview.mapType=MKMapTypeSatellite;
        break;

    case 2:
        mapview.mapType=MKMapTypeHybrid;
        break;


    default:
        break;
   }


 }


 -(IBAction)GetLocation:(id)sender{
  mapview.showsUserLocation=YES;

 }


 (IBAction)Directions:(id)sender{
  NSString * urlString=@"http://maps.apple.com/maps?daddr=38.711995,     -9.144932";
[[UIApplication sharedApplication] openURL:[NSURL    URLWithString:urlString]];

}

@end

VieController.h:

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface ViewController : UIViewController{

MKMapView * mapview;

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

-(IBAction)SetMap:(id)sender;

-(IBAction)GetLocation:(id)sender;

-(IBAction)Directions:(id)sender;

@end

And in the Connections Inspector everything (buttons) is set up..

1

There are 1 answers

0
Federico Malagoni On BEST ANSWER

You have to check/get the location permission :

add it to your plist file

<key>NSLocationWhenInUseUsageDescription</key>
<string></string>

and add that code to your VC:

if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
{
    [self.locationManager requestWhenInUseAuthorization];
}