1

I am trying to get current location of user using CLLocationCoordinate2D. I want the values in the format like

CLLocationCoordinate2D start = {-28.078694,153.382844 };

So that I can use them like following:

 NSString *urlAddress = [NSString 
       stringWithFormat:@"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f",  
       start.latitude, start.longitude, 
       destination.latitude, destination.longitude];  

I used

CLLocation *location = [[CLLocation alloc]init ];
CLLocationDegrees currentLatitude = location.coordinate.latitude;
CLLocationDegrees currentLongitude = location.coordinate.longitude;

to get current lat & long.

But I am getting 0.000 for both when I try to test. I am testing on iPhone 4s.

If there is any sample code, it will be great.

mmmmmm
  • 32,227
  • 27
  • 88
  • 117
iOSDev
  • 3,617
  • 10
  • 51
  • 91

6 Answers6

15

Add CoreLocation framework first and then use the following code...and your viewController must implement CLLocationManagerDelegate

-(void)findCurrentLocation
{

    CLLocationManager *locationManager = [[CLLocationManager alloc] init];
    if ([locationManager locationServicesEnabled])
    {
        locationManager.delegate = self; 
        locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
        locationManager.distanceFilter = kCLDistanceFilterNone; 
        [locationManager startUpdatingLocation];
    }


    CLLocation *location = [locationManager location];
    CLLocationCoordinate2D coordinate = [location coordinate];

    NSString *str=[[NSString alloc] initWithFormat:@" latitude:%f longitude:%f",coordinate.latitude,coordinate.longitude];
    NSLog(@"%@",str);


}
Mudit Bajpai
  • 3,010
  • 19
  • 34
  • 3
    `[locationManager location]` in this example may return `nil` or very old location. The best way to get current location is to wait while `locationManager` will send callback message to its delegate. But still location passed with callback message may be old, thus, you need always to check timestamp of location. – Aleksejs Mjaliks Mar 14 '12 at 05:47
  • Its not working for me latitude and longitude values coming like this 0.00000 – vijay Feb 27 '15 at 11:09
7

in AppDelegate.h declare following variable. and implement

CLLocationManagerDelegate delegate.

CLLocationManager *locationManager;
CLLocation *currentLocation;

in AppDelegate.h.m file write following code.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {       

    self.locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

    self.currentLocation = newLocation;
    self.currentLat =  newLocation.coordinate.latitude; 
    self.currentLong =  newLocation.coordinate.longitude; 
}
Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
Vibhooti
  • 1,193
  • 2
  • 9
  • 20
  • 1
    self.locationManager = [[CLLocationManager alloc] init]; the leak is there in the line. – Rams Mar 19 '12 at 05:58
2
CLLocationCoordinate2D location = [[[mapview userLocation] location] coordinate];  
NSLog(@"Location found from Map: %f %f",location.latitude,location.longitude);

and also refer the link(without mapview0

https://developer.apple.com/library/ios/#samplecode/LocateMe/Introduction/Intro.html

Rams
  • 1,721
  • 12
  • 22
  • I am not using mapview, just adding start and destination to google maps. And then displaying them in UIWebView. How can I get it without mapview? – iOSDev Mar 14 '12 at 05:37
  • Yes, you can go without `MKMapView`. It is little bit to hard to understand, why you use `UIWebView`. – Aleksejs Mjaliks Mar 14 '12 at 05:39
  • http://stackoverflow.com/questions/1449486/iphone-current-user-location-coordinates-showing-as-0-0 – Rams Mar 14 '12 at 05:56
  • Although this query has been solved, I would like to know how can I invoke actual directions page instead of page that has start and end locations added with google maps? – iOSDev Mar 14 '12 at 06:20
2

You need to use CLLocationManager to get a user's location. CLLocation is only a class of objects used to represent locations, it can't get user location by it self.

For more details and example, please follow to Apple docs on Location Awareness Programming.

Aleksejs Mjaliks
  • 8,647
  • 6
  • 38
  • 44
  • Please, check out Apple docs on [Location Awareness Programming](https://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html). – Aleksejs Mjaliks Mar 14 '12 at 05:41
1

use location manager to get lat and long, follow this tutorial for sample code http://www.edumobile.org/iphone/miscellaneous/how-to-get-current-latitude-and-longitude-in-iphone/

Manish Agrawal
  • 10,958
  • 6
  • 44
  • 76
0
-(void)findCurrentLocation
{
    if ([CLLocationManager locationServicesEnabled])
    {
        CLLocationManager *locationManager = [[CLLocationManager alloc] init];
        locationManager.delegate = self;
        locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        locationManager.distanceFilter = kCLDistanceFilterNone;
        [locationManager startUpdatingLocation];
    }
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    [self setLocation:[[manager location] coordinate]];
}
dominic
  • 143
  • 6