0
votes

what i need for my app is, to display my current location on my mapview by using location service, here is my code

.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface MapDirectoryViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>


@property (weak, nonatomic) IBOutlet MKMapView *myMapView;

- (IBAction)backAction:(id)sender;
@end

.m

@interface MapDirectoryViewController ()

@end

@implementation MapDirectoryViewController
{
    MKCoordinateRegion myRegion;
    CLLocationCoordinate2D center;
    MKCoordinateSpan span;
    NSMutableArray *locations;
    CLLocationCoordinate2D location;
    Annotation *myAnnoation;
}

@synthesize myMapView;

- (void)viewDidLoad {
    [super viewDidLoad];

    myMapView.showsUserLocation = YES;
    [myMapView setShowsUserLocation:YES];

    [myMapView setCenterCoordinate:myMapView.userLocation.location.coordinate animated:YES];

}


#delegation

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    //what do i need to do in here?
}

what i need excatly is to get blue dot to show up on map and make the dot as center of the map when app loads,

as you can see in my viewdidload, i tried many way to active the user current location, but none of them works...

and i realise one more thing in my actual iphone6, under general, my app doesnt have 'location' option there so i cant phycially go to active it.... e.g google map or facebook app they all have 'location' option under general app config...

any advise? any code example would be really helpful, thank you very much.

2

2 Answers

0
votes

If you set location permission i.e.

[LocationManager requestWhenInUseAuthorization]

or

[LocationManager requestAlwaysAuthorization]

then set

[self.mapLocation setShowsUserLocation:YES];

and also write code in map annotation delegate like this:

- (MKAnnotationView *)mapView:(MKMapView *)mapViewIn viewForAnnotation:(id <MKAnnotation>)annotation {
    //Your stuff
     if (annotation == _mapLocation.userLocation) {
        return nil;
     }


}
-1
votes

solution: 1, edit the source code of the Info.Plist with Right click > open as > Source code and add these lines:

<!-- for requestAlwaysAuthorization -->
<key>NSLocationAlwaysUsageDescription</key>
<string>Explain for what are you using the user location</string>
<!-- for requestWhenInUseAuthorization -->
<key>NSLocationWhenInUseUsageDescription</key>
<string>Explain for what are you using the user location</string>

2,

- (void)viewDidLoad {
    [super viewDidLoad];


    serviceConnector = [[ServiceConnector alloc] init];
    serviceConnector.delegate = self;
    [serviceConnector retrieveDataFromWebservice:ALL_MOSQUE_ADDRESS_JSON_WS];

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

    [myMapView setShowsUserLocation:YES];
    [myMapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];

    [myMapView setCenterCoordinate:myMapView.userLocation.location.coordinate animated:YES];
  }


-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    NSLog(@"location delegate called");
}

now it get me the current location with blue dot , and calling deleagation method correctly in ios 8