     }
     
     
-    
     private override init(){
         
         super.init()

            
            if(error){
                
                self.geocodingCompletionHandler!(address: nil,error: error.localizedDescription)
                
            }else{
                
                var dataAsString: NSString = NSString(data: data, encoding: NSUTF8StringEncoding)
                
                // Convert the retrieved data in to an object through JSON deserialization
                var err: NSError
                var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
                
                var status = jsonResult.valueForKey("status") as NSString
                if((status.lowercaseString as NSString).isEqualToString("ok")){
                    var address = AddressParser()
                    address.parseGoogleLocationData(jsonResult)
                    let addressDict = address.addressDictionary()
                    self.geocodingCompletionHandler!(address: addressDict,error: nil)
                    
                }else{
                    
                    self.geocodingCompletionHandler!(address: nil,error: "invalid address: \(address)")
                    
                }
                
            }
            }
            
            
        )
        
        
    }
    
    func reverseGeocodeLocationUsingGoogleWithLatLon(#latitude:Double, longitude: Double,onReverseGeocodingCompletionHandler:((address:NSDictionary?, error:String?)->Void)?){
        
        self.reverseGeocodingCompletionHandler = onReverseGeocodingCompletionHandler
        
        reverseGocodeUsingGoogle(latitude: latitude, longitude: longitude)
        
    }
    
    func reverseGeocodeLocationUsingGoogleWithCoordinates(coord:CLLocation, onReverseGeocodingCompletionHandler:((address:NSDictionary?, error:String?)->Void)?){
        
        
        reverseGeocodeLocationUsingGoogleWithLatLon(latitude: coord.coordinate.latitude, longitude: coord.coordinate.longitude, onReverseGeocodingCompletionHandler: onReverseGeocodingCompletionHandler)
        
        
        
    }
    
    
    
    
    private func reverseGocodeUsingGoogle(#latitude:Double, longitude: Double){
        
        var urlString = "http://maps.googleapis.com/maps/api/geocode/json?latlng=\(latitude),\(longitude)&sensor=true" as NSString
        
        urlString = urlString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
        
        
        let url:NSURL = NSURL(string:urlString)
        
        let request:NSURLRequest = NSURLRequest(URL:url)
        
        let queue:NSOperationQueue = NSOperationQueue()
        
        /*
        let url = NSURL(string:urlString)
        var session = NSURLSession.sharedSession()

        var task:NSURLSessionDataTask = session.dataTaskWithURL(url, completionHandler:apiHandler)
        task.resume()
        */
        
        NSURLConnection.sendAsynchronousRequest(request,queue:queue,completionHandler:{response,data,error in
            
            if(error){
                
                self.reverseGeocodingCompletionHandler!(address: nil,error: error.localizedDescription)
                
            }else{
                
                var dataAsString: NSString = NSString(data: data, encoding: NSUTF8StringEncoding)
                
                // Convert the retrieved data in to an object through JSON deserialization
                var err: NSError
                var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
                
                var status = jsonResult.valueForKey("status") as NSString
                if((status.lowercaseString as NSString).isEqualToString("ok")){
                    var address = AddressParser()
                    address.parseGoogleLocationData(jsonResult)
                    let addressDict = address.addressDictionary()
                    self.reverseGeocodingCompletionHandler!(address: addressDict,error: nil)
                    
                }else{
                    
                    self.reverseGeocodingCompletionHandler!(address: nil,error: "invalid latitude: \(latitude) & longitude: \(longitude)")
                    
                }
                
            }
            }
            
            
        )
        
        
    }
    
    
    
    func apiHandler(data:NSData!, response:NSURLResponse!, error:NSError!) {
        
        if(error){
            
            self.reverseGeocodingCompletionHandler!(address: nil,error: error.localizedDescription)
            
        }else{
            
            var dataAsString: NSString = NSString(data: data, encoding: NSUTF8StringEncoding)
            
            // Convert the retrieved data in to an object through JSON deserialization
            var err: NSError
            var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
            
            var status = jsonResult.valueForKey("status") as NSString
            if((status.lowercaseString as NSString).isEqualToString("ok")){
                var address = AddressParser()
                address.parseGoogleLocationData(jsonResult)
                let addressDict = address.addressDictionary()
                self.reverseGeocodingCompletionHandler!(address: addressDict,error: nil)
                
            }else{
                
                self.reverseGeocodingCompletionHandler!(address: nil,error: "invalid latitude: \(latitude) & longitude: \(longitude)")
                
            }
            
        }
    }
    
    
}



import UIKit
import CoreLocation


typealias LMReverseGeocodeCompletionHandler = ((address:NSDictionary?, error:String?)->Void)?
typealias LMGeocodeCompletionHandler = ((address:NSDictionary?, error:String?)->Void)?
typealias LMLocationCompletionHandler = ((latitude:Double, longitude:Double, status:String, verboseMessage:String, error:String?)->())?

// Todo: Keep completion handler differerent for all services, otherwise only one will work
enum GeoCodingType{
    
    case Geocoding
    case ReverseGeocoding
}

class LocationManager: NSObject,CLLocationManagerDelegate {
    
    /* Private variables */
    private var completionHandler:LMLocationCompletionHandler
    
    private var reverseGeocodingCompletionHandler:LMReverseGeocodeCompletionHandler
    private var geocodingCompletionHandler:LMGeocodeCompletionHandler
    
    private var locationStatus : NSString = "Calibrating"// to pass in handler
    private var locationManager: CLLocationManager!
    private var verboseMessage = "Calibrating"
    
    private let verboseMessageDictionary = [CLAuthorizationStatus.NotDetermined:"You have not yet made a choice with regards to this application.",
