I’m writing an app that has a “get directions from here to the selected address” function at some point.
When the user invokes that function, the app creates a “Google Map” URL and opens the official Maps app with the user current location as source and the selected address as destination.
In situations like this, you have two options to determine the current location of the user:
- use the CoreLocation framework;
- use the string “Current Location” as the source address.
The first option requires you to code all the CoreLocation related stuff and requires the user to accept being geolocated by the app; the second option is much simpler to implement, because it’s Maps that takes care of everything concerning the geolocation.
Besides code writing considerations, remember also that continuous use of GPS in your app consumes a lot of battery power.
Why would anybody use the first option, then? Unfortunately, the “Current Location” trick works only if the user’s iPhone is set to use English or British English (see this question on Stack Overflow).
Every language that can be set on the iPhone has its own localized version of this string (in Italian is “Posizione attuale”).
Please note also that simply translating “Current Location” doesn’t necessarily do: you must match the exact string that Apple chose for the localization in use.
So even if you speak all the 34 currently supported languages , you can’t be sure that your translations will properly work everywhere…
With some patience and some sniffing into the iPhone firmware, I managed to discover the exact translation of “Current Location” in all the languages supported by the iPhone (and Maps thus) and I wrote the helper class LocalizedCurrentLocation that can provide your app with the correct localized version of “Current Location”.
The class is very simple and has only one class method:
+ (NSString *)currentLocationStringForCurrentLanguage;
To get the localized string you simply call:
NSString *destinationAddress = [NSString stringWithFormat:@"%@+%@+%@", [address street], [address city], [address country]]; NSString *sourceAddress = [LocalizedCurrentLocation currentLocationStringForCurrentLanguage]; NSString *url = [NSString stringWithFormat:@"http://maps.google.com/maps?saddr=%@&daddr=%@", [sourceAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], [destinationAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
Of course this trick is not guaranteed to work forever; it doesn’t use any particular “forbidden” piece of code, but it relies on the assumption that everything is as is now.
I tested the class with a couple of languages and everything seems to work seamlessly; I don’t have time/patience to test all the 34 languages so let me know if you find any issues.
You can download the class here: LocalizedCurrentLocation.zip
EDIT: You can now download the class from GitHub: https://github.com/Sjors/currentLocationStringForCurrentLanguage (Thank you Sjors!)
EDIT: Please read the comment by Alexey below, if you plan to use the Russian language!
EDIT: Thanks to Brad, the class is available for MonoTouch as well: https://github.com/ytn3rd/CurrentLocationStringForCurrentLanguage.