0

I am trying to show my current Location on a MapView. I added a Button to my Storyboard.

First problem: The Current Location is not shown on my screen.

Second problem: The MapView doesn't zoom to the Location if I click on the Button.

My ViewController.m : (The Button Methods Name are: getLocation )

#import "ViewController.h"
@import CoreLocation;


@interface ViewController () <CLLocationManagerDelegate>
@property (strong, nonatomic) CLLocationManager *locationManager;

@end

@implementation ViewController
@synthesize mapview = _mapview;

- (void)viewDidLoad {

    [super viewDidLoad];


    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;

    if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
        [self.locationManager requestWhenInUseAuthorization];
    }
    mapview.showsUserLocation = YES;
    [self.locationManager startUpdatingLocation];

}

- (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;
    }
}


// The sender here are a Button on my Storyboard, who should show the current device Location if i click on it.
-(IBAction)getLocation:(id)sender
{
    mapview.showsUserLocation = YES;
    //NSLog(@"Koordinaten des Geräts: %@", mapview.userLocation.description );
    //[mapview setCenterCoordinate:mapview.userLocation.coordinate animated:YES];
}
@end
Linus
  • 1
  • 2

2 Answers2

0

You might be missing NSLocationWhenInUseUsageDescription on your info.plist file and it is required iOS 8 onwards, you can optionally leave its value empty. In the past, one could optionally include a 'NSLocationUsageDescription' key , a string explaining to the user for what the app planning to use location services. And now it has been split up into two separate keys (NSLocationWhenInUseUsageDescription and NSLocationAlwaysUsageDescription), and is now mandatory; if you call requestWhenInUseAuthorization or requestAlwaysAuthorization without the corresponding key, the prompt simply won't be shown to the user.

Please bear in mind, if you specify requestAlwaysAuthorization key but your app didn't use the locator always, preferably in the background, then you can face rejection from the apple during app store submission.

ldindu
  • 4,270
  • 2
  • 20
  • 24
0

pragma mark - CLLocationManagerDelegate Methods

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
   CLLocation *newLocation = locations.lastObject;
}
- (void)locationManager: (CLLocationManager *)manager didFailWithError: (NSError *)error
{
}

And for zoom try this

@property (weak, nonatomic) IBOutlet MKMapView *mapAllLocations;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

mapAllLocations.ZoomEnabled = true;
mapAllLocations.ScrollEnabled = true;
CLLocationCoordinate2D zoomLocation;

zoomLocation.latitude =newLocation.latitude;
zoomLocation.longitude=newLocation.longitude;

MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation,1000.0, 1000.0);
[self.mapAllLocations setRegion:viewRegion animated:YES];
Community
  • 1
  • 1
Nikita Khandelwal
  • 1,741
  • 16
  • 25