0

Hello I am using CoreLocation to get latitude and longitude in my map application to get latitude and longitude by using following method

- (void)viewDidLoad {
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];
    [super viewDidLoad];
   }

The _ (void)locationManager.... method is not getting call

 - (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
       fromLocation:(CLLocation *)oldLocation
  {
int degrees = newLocation.coordinate.latitude;
double decimal = fabs(newLocation.coordinate.latitude - degrees);
int minutes = decimal * 60;
double seconds = decimal * 3600 - minutes * 60;
NSString *lat = [NSString stringWithFormat:@"%d° %d' %1.4f\"", 
                 degrees, minutes, seconds];

degrees = newLocation.coordinate.longitude;
decimal = fabs(newLocation.coordinate.longitude - degrees);
minutes = decimal * 60;
seconds = decimal * 3600 - minutes * 60;
NSString *longt = [NSString stringWithFormat:@"%d° %d' %1.4f\"", 
                   degrees, minutes, seconds];

   }

I want to call lati & longi on Button click event to URL. But it is not working. it passes 0000.0 lati & longi to my url. Can some one tell me how to rectify this code

Navnath Memane
  • 265
  • 1
  • 8
  • 25

4 Answers4

3

I don't see any obvious problem with your init of CLLocationManager. If you are not getting callbacks perhaps it doesn't yet have a location available. You should also implement the error callback and see if it is getting called instead.

I suggest you try one of the CLLocationManager examples provided by Apple and see if you can get the example to run.

progrmr
  • 75,956
  • 16
  • 112
  • 147
2

I have did this to pass latitude and longitude in to web service URL on button click...

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

    if((fabs(newLocation.coordinate.latitude) > 0.001) ||    (fabs(newLocation.coordinate.longitude) > 0.001)) {
    NSLog(@"Got location %f,%f", newLocation.coordinate.latitude,   newLocation.coordinate.longitude);
    if (currentLocation != nil) {
        [currentLocation release];
    }
    currentLocation = newLocation;
    [currentLocation retain];
}}

and On click event code I have just pass locationmanager.location.coordinate.latitude.....

NSString *url = [NSString stringWithFormat:@"http://.....Url..../hespdirectory/phpsqlsearch_genxml.php?lat=%f&lng=%f&radius=%f",locationManager.location.coordinate.latitude,locationManager.location.coordinate.longitude,radius];
Navnath Memane
  • 265
  • 1
  • 8
  • 25
1

U can directly change the CLLocation to NSString

NSString *lat=[NSString stringWithFormat:@"%f", newLocation.latitude];
NSString *lon=[NSString stringWithFormat:@"%f", newLocation.longitude];
EXC_BAD_ACCESS
  • 2,699
  • 2
  • 21
  • 25
  • The above code I have pasted in my Button click event to pass coordinates in URL .....it showing me newLocation undeclare – Navnath Memane Oct 13 '11 at 13:10
  • You don get the newLocation here.U add this inside of - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { method.And declare lat and lon as global and use them – EXC_BAD_ACCESS Oct 13 '11 at 13:12
  • my .....(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation is not getting called. I checked by setting breakpoint. what should I do – Navnath Memane Oct 13 '11 at 13:36
  • R u checking in simulator.If so it will not called.These delegates called oly device – EXC_BAD_ACCESS Oct 13 '11 at 13:37
  • Right on device the method getting called. Now what should I writ code on button click event to pass lati in url .....I gave NSString * lati = [[[NSString alloc] initWithFormat:@"%f", coordinate.latitude]autorelease]; it throwing EXC_BAD_AcCess – Navnath Memane Oct 13 '11 at 13:47
  • still getting lati = 0.0000 with EXC bad access. Can I mail you my code. I am really stuck here. I am following http://www.switchonthecode.com/tutorials/getting-your-location-in-an-iphone-application tutorial for my Map app. But not getting result – Navnath Memane Oct 13 '11 at 14:00
0

The answers don't show it directly, but you should have assigned your controller as the CLLocationManagerDelegate to recieve the callbacks correct?

Bill Burgess
  • 14,054
  • 6
  • 49
  • 86