reverse geocoding and localization

Discussion in 'iOS Programming' started by franium, Nov 25, 2010.

  1. franium macrumors member

    Joined:
    Nov 18, 2010
    #1
    Hi,
    I use this code to set a label with a location string
    Code:
    locationString = [[NSString alloc] initWithFormat:@"%@%@ - %@ %@%@",
    						thoroughfare,subThoroughfare,postalCode,
    						locality,countryCode];
    	
    locationLabel.text = locationString;
    
    where thoroughfare, subThoroughfare, postalCode, locality,countryCode are obtained from a placemark.
    Now, I'd like to visualize this string according the current locale. Have I specify a string format for each locale in which I'm interested or there is a simpler way to obtain this?
    Thanks,
    Fran
     
  2. ianray macrumors 6502

    Joined:
    Jun 22, 2010
    Location:
    @
    #2
    Do you mean, that you want to adjust for different locales where, for example, the order of thoroughfare and subThoroughfare is different? Or something else?
     
  3. franium thread starter macrumors member

    Joined:
    Nov 18, 2010
    #3
    Exactly... I know that I can do it controlling the locale and changing accordingly the ordering of thoroughfare, subThoroughfare, postalCode, locality and countryCode in
    Code:
    locationString = [[NSString alloc] initWithFormat:@"%@%@ - %@ %@%@",
    						thoroughfare,subThoroughfare,postalCode,
    						locality,countryCode];
    
    but this becomes hard if I have to do it for each locale, so I want to know if there is a method that considers the locale.
     
  4. ianray macrumors 6502

    Joined:
    Jun 22, 2010
    Location:
    @
    #4
    I dealt with this in my App by dumping the MKPlacemark properties -- including addressDictionary; using appropriate test data you should be able to develop a useful heuristic which uses all of the available information.

    Good luck :)
     
  5. franium thread starter macrumors member

    Joined:
    Nov 18, 2010
    #5
    I can't figure out how AddressDictionary could help me, really I don't understand very well what an AddressDictionary object represents :)
    I'm working on not mine code and the reverse geocoder is created only so:
    Code:
    self.reverseGeocoder = [[[MKReverseGeocoder alloc] initWithCoordinate:coordinate] autorelease];
    
    I'll search more info about Address Dictionary.
    Thanks
     
  6. ianray macrumors 6502

    Joined:
    Jun 22, 2010
    Location:
    @
    #6
    Use the debugger or NSLog to inspect the placemark object you receive when MKReverseGeocoder succeeds. The adressDictionary property is particularly interesting... ;)
     
  7. franium thread starter macrumors member

    Joined:
    Nov 18, 2010
    #7
    I tried to access it, using for example
    Code:
    NSString *kABPersonAddressCityKey;
    NSString *city = [placemark.addressDictionary objectForKey:(NSString*) kABPersonAddressCityKey];
    NSLog(@"%s", city);
    
    in
    Code:
    - (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark;
    
    but the app crashes.
    Moreover, my doubt is this: if I create the reverseGeocoder with
    Code:
    self.reverseGeocoder = [[[MKReverseGeocoder alloc] initWithCoordinate:coordinate] autorelease];
    
    can this cause a problem or has the placemark always an AddressDictionary not null?
     
  8. ianray macrumors 6502

    Joined:
    Jun 22, 2010
    Location:
    @
    #8
    The crash is because you have not initialized 'kABPersonAddressCityKey'.
     
  9. franium thread starter macrumors member

    Joined:
    Nov 18, 2010
    #9
    Now, I can see what there is in the addressDictionary, with
    Code:
    NSLog(@"The geocoder has returned: %@", [placemark addressDictionary]);
    
    and I see the interesting field "FormattedAddressLines".
    Now, if I change the locale and language I see only a traduction of city and country, but I suppose that for example with american adresses I'll see first the street number and then the remain, won't I? But also in this case, my question is still there... that is... in the case of an american address I think it needs the amministrative area and sub-amministrative area, which are useless in an italian one, for example. Do I miss something?
     
  10. ianray macrumors 6502

    Joined:
    Jun 22, 2010
    Location:
    @
    #10
    I suggest that you use a representative sample of coordinates, located within various cities and various countries, and with that the reverse-geocode data you will be able to devise an appropriate heuristic.

    There really is no right or wrong answer. Good luck!
     
  11. franium thread starter macrumors member

    Joined:
    Nov 18, 2010
    #11

    Thank you for your patience :) For now I'll use only a combination of the FormattedAddressLines, State and Country, then with a bit of time I'll try your solution
    Ciao :)
     
  12. ianray macrumors 6502

    Joined:
    Jun 22, 2010
    Location:
    @
    #12
    You're welcome :) I look forward to seeing your App in the AppStore!
     
  13. franium thread starter macrumors member

    Joined:
    Nov 18, 2010
    #13
    Hi,
    another thing: did you succeeded in doing some tries to see how the reverse geocoding string is printed? I try to explain... I noted that without adding the "State", the location of Apple is printed
    Code:
    1 Infinite Loop - Cupertino, California 95014, US
    
    I use
    Code:
    [formattedAddressLines objectAtIndex:0]; 
    
    for print then "1 Infinite Loop"
    Code:
    [formattedAddressLines objectAtIndex:1]; 
    
    for "Cupertino, California 95014"
    Do you know if in USA, the State is always before zipcode?

    I don't know if add also the state returned by:
    Code:
    NSString *state = [placemark.addressDictionary objectForKey:@"State"];
    
    because I could have a state repetition
    Thanks in advance.
     

Share This Page