I'm learning to use map and my objective is to display current location on the map when the application first load, instead of showing the map of the world. But I can't get it to focus on the location as wanted as coordinates that I've got are all 0.
Note:
- I've set in Xcode the default location as Tokyo, Japan.
- All codes are in viewDidLoad
- I'm using Simulator
I've tried using 2 approaches.
Approach A) I set showsUserLocation as YES. Then set center of the map to value from map's userLocation. But the user location is null, latitutude and longitude are 0.000000.
Approach B) I create location manager and get latitude and longitude from it. Also getting latitude and longitude are 0.000000.
Below is the code:
ViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>
@property (weak, nonatomic) IBOutlet MKMapView *myMap;
@property CLLocationManager *locationManager;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//Approach A)
self.myMap.showsUserLocation = YES; //tokyo Japan, blue pin with ripple displays on Default Location that I set in XCode = Tokyo, Japan
NSLog(@"using MapView.userLocation, user location = %@", self.myMap.userLocation.location); //output = null
NSLog(@"using MapView.userLocation, latitude = %f", self.myMap.userLocation.location.coordinate.latitude); //output = 0.000000
NSLog(@"using MapView.userLocation, longitude = %f", self.myMap.userLocation.location.coordinate.longitude); //output = 0.000000
CLLocationCoordinate2D centerCoord = {self.myMap.userLocation.location.coordinate.latitude, self.myMap.userLocation.location.coordinate.longitude};
[self.myMap setCenterCoordinate:centerCoord animated:TRUE]; //nothing happens, as latitude and longitude = 0.000000
//Approach B)
// create the location manager
CLLocationManager *locationManager;
if (nil == self.locationManager) {
self.locationManager = [[CLLocationManager alloc] init];
}
self.locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
self.locationManager.delegate = self; //add as suggested
[self.locationManager startUpdatingLocation];
NSLog(@"using LocationManager:current location latitude = %f, longtitude = %f", self.locationManager.location.coordinate.latitude, self.locationManager.location.coordinate.longitude);
//if getting latitude and longitude, will call "setCenterCoordinate"
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//add as suggested
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"didUpdateToLocation:Latitude : %f", newLocation.coordinate.latitude);
NSLog(@"didUpdateToLocation:Longitude : %f", newLocation.coordinate.longitude);
[self.locationManager stopUpdatingLocation];
}
@end
Output:
xxxx using MapView.userLocation, user location = (null)
xxxx using MapView.userLocation, latitude = 0.000000
xxxx using MapView.userLocation, longitude = 0.000000
xxxx using LocationManager:current location latitude = 0.000000, longtitude = 0.000000
Now the Output:
xxx using MapView.userLocation, user location = (null)
xxx using MapView.userLocation, latitude = 0.000000
xxx using MapView.userLocation, longitude = 0.000000
xxx using LocationManager:current location latitude = 0.000000, longtitude = 0.000000
xxx didUpdateToLocation:Latitude : 35.702069
xxx didUpdateToLocation:Longitude : 139.775327
Best Answer
Add
CoreLocation.framework
to you project and add to.h
file and alsoAnd also add Delegate method such like,