0

I need to return the longitude and latitude from this method. This is from the locationManager code. When I log the lat, longt they give the correct values.

What I have been unable to do is to work out you to export/return the variables out of this method. How should I go about changing it?

- (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; self.lat = [NSString stringWithFormat:@"%d° %d' %1.4f\"", degrees, minutes, seconds]; NSLog(@"Current Latitude : %@",self.lat); degrees = newLocation.coordinate.longitude; decimal = fabs(newLocation.coordinate.longitude - degrees); minutes = decimal * 60; seconds = decimal * 3600 - minutes * 60; self.longt = [NSString stringWithFormat:@"%d° %d' %1.4f\"", degrees, minutes, seconds]; NSLog(@"Current Longitude : %@",self.longt); [manager stopUpdatingLocation]; } 
3
  • Hmmm I might be thinking about this too simply but you could create your own custom delegate or call your own method to handle the updates? What I mean by this is when didUpdateToLocation: is called you then in turn call your own method. Can you give an indication on where you want to return the values to? Commented Aug 20, 2013 at 9:57
  • you can save it in two double variable, that declare in .h file Commented Aug 20, 2013 at 10:13
  • You could return a CGSize using the width value to store lat and the height to store long. Commented Aug 20, 2013 at 11:38

1 Answer 1

1

You don't need to return the location from that method because you're not going to call that method yourself -- the Location Manager will call that method to give you an updated location. What you should do is to store the new location so that your code can use it later. Usually, that just means updating your instance variables, but you can do whatever you want with the location -- update you data model, write it out to a text file, etc. Storing it in ivars makes it easy to access the location whenever you need it in your code.

Sign up to request clarification or add additional context in comments.

3 Comments

Many thanks - will try and implement this tomorrow and report back.
Have worked around this have got a mental black, can't just get it to work. If the method above generates the correct long/lat co-ordinates how do I export them from it, or make the calling procedure retrieve them. Being a void it expects to return zilch and been unable to modify it. Is there an example of what I am trying to achieve anywhere that OI can work off? Many thanks.
If the same object needs the location, it just uses self.lat and self.longt to get the latitude and longitude. Other objects that need the position would need a pointer to the object that has it. Usually, the location manager delegate will be some object that's accessible to the rest of the program -- part of the model, the root view controller, or maybe the app delegate. Your question isn't so much about location as about how one object can talk to another. Remember that for a to talk to b, a needs a reference to b so that it can say: lat = b.lat or lat = [b lat];

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.