Question

I have a handful of points-of-interest and want to hard-code them but there doesn't seem to be a clean facility in objective-c to do so.

I want to associate two numbers to a string: @"cool spot", 37.77794,-122.41933

Do I need an NSDictionary of NSStrings and convert each float to a string? It just seems like too much overhead to group all this? Should I make a c-struct? This situation HAS to come up a lot but cannot think of what a clean solution for this might be.

Thanks

Was it helpful?

Solution

How are you intending on storing it? If you're using something like core-data then your easiest option is to use 2 separate numbers for longitude and latitude, and then simply create a coordinate when you access the data (CLLocationCoordinate2D as tom mentioned)

It is very dependent on your storage method as iOS only allows saving of certain types of objects for different methods.

If you're not actually storing it to file and just using it in memory, then as tom said, something like CLLocationCoordinate2D would work, which is a struct rather than an object. If you want an actual object, then use CLLocation which contains a CLLocationCoordinate2D object internally.

You can create a CLLocationCoordinate2D object with :

CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(latitude, longitude);

Or if you need the object wrapper, then create a CLLocation object with :

CLLocation *loc = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];

OTHER TIPS

Take a look at CLLocationCoordinate2D (CLLocationCoordinate2D - Apple Developer Docs), which has a latitude field and a longitude field.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top