Encode and Decode GPS coordinates into a String

Encode GPS location into a name.
Or do the reverse.
GeoEncode.h

//
//  GeoEncode.h
//  Open Industries
//
//  Created by Utopia on 11/21/11.
//  Copyright (c) 2011 code.toemaar.nl. All rights reserved.
//
 
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
 
@interface GeoEncode : NSObject {
    NSString *_apiUrl;
}
 
- (NSDictionary *) encodeString :(NSString *) locationStr;
- (NSString *) decodeLatLng :(CLLocationCoordinate2D) location;
- (void) setNetworkActivity :(BOOL) show;
@end

GeoEncode.m

//
//  GeoEncode.m
//  Open Industries
//
//  Created by Utopia on 11/21/11.
//  Copyright (c) 2011 code.toemaar.nl. All rights reserved.
//
 
#import "GeoEncode.h"
#import "SBJson.h"
 
@implementation GeoEncode
 
- (id) init {
    if ( self = [super init] ) {
        _apiUrl = @"http://maps.googleapis.com/maps/api/geocode/json?sensor=false";
    }
    return self;
}
 
- (NSDictionary *) encodeString :(NSString *) locationStr {
    NSDictionary *googleResult;
    NSMutableDictionary *result, *addressDict, *locationDict;
    NSError *error = nil;
    CGFloat lng, lat;
    NSString *address, *status;
 
    [self setNetworkActivity :YES];
 
    NSString *urlStr = [[
                            [NSString alloc]
                                initWithFormat:@"%@%@%@", _apiUrl, @"&amp;address=", locationStr]
                                    stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
 
    NSString *jsonStr = [NSString stringWithContentsOfURL:[NSURL URLWithString: urlStr] encoding:NSUTF8StringEncoding error:&amp;error ];
    googleResult = [jsonStr JSONValue];
    if(0 == error.code){
        if([@"OK" isEqualToString: [googleResult objectForKey: @"status"]]){
            addressDict = [[googleResult objectForKey: @"results"] objectAtIndex:0];
            locationDict = [[addressDict objectForKey:@"geometry"] objectForKey:@"location"];
            address = [addressDict objectForKey: @"formatted_address"];
            lng = [[locationDict objectForKey: @"lng"] floatValue];
            lat = [[locationDict objectForKey: @"lat"] floatValue];
            status = [googleResult objectForKey: @"status"];
        }else{
            lng = 0.0;
            lat = 0.0;
            address = @"";
            status = [googleResult objectForKey: @"status"];
        }
    } else {
        lng = 0.0;
        lat = 0.0;
        address = @"";
        status = @"";
    }
 
    CLLocation *location = [[CLLocation alloc] initWithLatitude:lat longitude:lng];
    result = [[NSMutableDictionary alloc] init];
    [result setObject:location forKey:@"location"];
    [result setObject:address forKey:@"address"];
    [result setObject:status forKey:@"status"];
    [self setNetworkActivity :NO];    
 
    return [NSDictionary dictionaryWithDictionary:result];
}
 
- (NSString *) decodeLatLng :(CLLocationCoordinate2D) location {
    NSString *result = @"";
    NSError *error = nil;
    NSDictionary *googleResult, *addressDict;    
 
    NSString *urlStr = [[NSString alloc] initWithFormat:@"%@%@%f,%f",  _apiUrl, @"&amp;latlng=", location.latitude, location.longitude];
    NSString *jsonStr = [NSString stringWithContentsOfURL:[NSURL URLWithString: urlStr] encoding:NSUTF8StringEncoding error:&amp;error ];     
 
    if(0 == error.code){
        googleResult = [jsonStr JSONValue];
        if([@"OK" isEqualToString: [googleResult objectForKey: @"status"]]){
            addressDict = [[googleResult objectForKey: @"results"] objectAtIndex:0];
            result = [addressDict objectForKey: @"formatted_address"];
        }
    }
 
    return result;
}
 
- (void) setNetworkActivity :(BOOL) show {
    UIApplication* app = [UIApplication sharedApplication];
    app.networkActivityIndicatorVisible = show;
}
 
- (void)dealloc {
    [_apiUrl release];
    _apiUrl = nil;
}
 
@end

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>