@@ -40,6 +40,7 @@ enum GeoCodingType{
40
40
case ReverseGeocoding
41
41
}
42
42
43
+
43
44
class LocationManager : NSObject , CLLocationManagerDelegate {
44
45
45
46
/* Private variables */
@@ -55,7 +56,7 @@ class LocationManager: NSObject,CLLocationManagerDelegate {
55
56
private let verboseMessageDictionary = [ CLAuthorizationStatus . NotDetermined: NSLocalizedString ( " You have not yet made a choice with regards to this application. " , comment: " " ) ,
56
57
CLAuthorizationStatus . Restricted: NSLocalizedString ( " This application is not authorized to use location services. Due to active restrictions on location services, the user cannot change this status, and may not have personally denied authorization. " , comment: " " ) ,
57
58
CLAuthorizationStatus . Denied: NSLocalizedString ( " You have explicitly denied authorization for this application, or location services are disabled in Settings. " , comment: " " ) ,
58
- CLAuthorizationStatus . AuthorizedAlways : NSLocalizedString ( " App is Authorized to always use location services. " , comment : " " ) , CLAuthorizationStatus . AuthorizedWhenInUse : NSLocalizedString ( " You have granted authorization to use your location only when the app is visible to you. " , comment : " " ) ]
59
+ ]
59
60
60
61
61
62
var delegate : LocationManagerDelegate ? = nil
@@ -160,7 +161,9 @@ class LocationManager: NSObject,CLLocationManagerDelegate {
160
161
if NSString ( string: UIDevice . currentDevice ( ) . systemVersion) . doubleValue >= 8 {
161
162
162
163
//locationManager.requestAlwaysAuthorization() // add in plist NSLocationAlwaysUsageDescription
163
- locationManager. requestWhenInUseAuthorization ( ) // add in plist NSLocationWhenInUseUsageDescription
164
+ if #available( iOS 8 . 0 , * ) {
165
+ locationManager. requestWhenInUseAuthorization ( )
166
+ }
164
167
}
165
168
166
169
startLocationManger ( )
@@ -192,7 +195,7 @@ class LocationManager: NSObject,CLLocationManagerDelegate {
192
195
isRunning = false
193
196
}
194
197
195
- internal func locationManager( manager: CLLocationManager ! , didFailWithError error: NSError ! ) {
198
+ internal func locationManager( manager: CLLocationManager , didFailWithError error: NSError ) {
196
199
197
200
stopLocationManger ( )
198
201
@@ -213,7 +216,7 @@ class LocationManager: NSObject,CLLocationManagerDelegate {
213
216
}
214
217
}
215
218
216
- internal func locationManager( manager: CLLocationManager ! , didUpdateLocations locations: [ AnyObject ] ! ) {
219
+ internal func locationManager( manager: CLLocationManager , didUpdateLocations locations: [ CLLocation ] ) {
217
220
218
221
let arrayOfLocation = locations as NSArray
219
222
let location = arrayOfLocation. lastObject as! CLLocation
@@ -252,10 +255,10 @@ class LocationManager: NSObject,CLLocationManagerDelegate {
252
255
}
253
256
}
254
257
255
- internal func locationManager( manager: CLLocationManager ! ,
258
+ internal func locationManager( manager: CLLocationManager ,
256
259
didChangeAuthorizationStatus status: CLAuthorizationStatus ) {
257
260
var hasAuthorised = false
258
- var verboseKey = status
261
+ let verboseKey = status
259
262
switch status {
260
263
case CLAuthorizationStatus . Restricted:
261
264
locationStatus = NSLocalizedString ( " Restricted Access " , comment: " " )
@@ -298,7 +301,7 @@ class LocationManager: NSObject,CLLocationManagerDelegate {
298
301
}
299
302
}
300
303
301
- func reverseGeocodeLocationWithLatLon( # latitude: Double, longitude: Double, onReverseGeocodingComplet ionHandler : LMReverseGeocodeCompletionHandler ) {
304
+ func reverseGeocodeLocationWithLatLon( latitude latitude: Double , longitude: Double , onReverseGeocodingCompletionHandler: LMReverseGeocodeCompletionHandler ) {
302
305
303
306
let location : CLLocation = CLLocation ( latitude: latitude, longitude: longitude)
304
307
@@ -319,11 +322,11 @@ class LocationManager: NSObject,CLLocationManagerDelegate {
319
322
geocoder. reverseGeocodeLocation ( location, completionHandler: { ( placemarks, error) -> Void in
320
323
321
324
if error != nil {
322
- self . reverseGeocodingCompletionHandler!( reverseGecodeInfo: nil , placemark: nil , error: error. localizedDescription)
325
+ self . reverseGeocodingCompletionHandler!( reverseGecodeInfo: nil , placemark: nil , error: error! . localizedDescription)
323
326
} else {
324
327
325
- if let placemark = placemarks ? [ 0 ] as? CLPlacemark {
326
- var address = AddressParser ( )
328
+ if let placemark = placemarks ? [ 0 ] {
329
+ let address = AddressParser ( )
327
330
address. parseAppleLocationData ( placemark)
328
331
let addressDict = address. getAddressDictionary ( )
329
332
self . reverseGeocodingCompletionHandler!( reverseGecodeInfo: addressDict, placemark: placemark, error: nil )
@@ -335,7 +338,7 @@ class LocationManager: NSObject,CLLocationManagerDelegate {
335
338
} )
336
339
}
337
340
338
- func geocodeAddressString( # address: NSString, onGeocodingComplet ionHandler : LMGeocodeCompletionHandler ) {
341
+ func geocodeAddressString( address address: NSString , onGeocodingCompletionHandler: LMGeocodeCompletionHandler ) {
339
342
340
343
self . geocodingCompletionHandler = onGeocodingCompletionHandler
341
344
@@ -345,14 +348,15 @@ class LocationManager: NSObject,CLLocationManagerDelegate {
345
348
private func geoCodeAddress( address: NSString ) {
346
349
347
350
let geocoder = CLGeocoder ( )
348
- geocoder. geocodeAddressString ( address as String , completionHandler: { ( placemarks: [ AnyObject ] ! , error: NSError ! ) -> Void in
351
+
352
+ geocoder. geocodeAddressString ( address as String , completionHandler: { ( placemarks, error) -> Void in
349
353
350
354
if error != nil {
351
355
352
- self . geocodingCompletionHandler!( gecodeInfo: nil , placemark: nil , error: error. localizedDescription)
356
+ self . geocodingCompletionHandler!( gecodeInfo: nil , placemark: nil , error: error! . localizedDescription)
353
357
} else {
354
- if let placemark = placemarks ? [ 0 ] as? CLPlacemark {
355
- var address = AddressParser ( )
358
+ if let placemark = placemarks ? [ 0 ] {
359
+ let address = AddressParser ( )
356
360
address. parseAppleLocationData ( placemark)
357
361
let addressDict = address. getAddressDictionary ( )
358
362
self . geocodingCompletionHandler!( gecodeInfo: addressDict, placemark: placemark, error: nil )
@@ -363,7 +367,7 @@ class LocationManager: NSObject,CLLocationManagerDelegate {
363
367
} )
364
368
}
365
369
366
- func geocodeUsingGoogleAddressString( # address: NSString, onGeocodingComplet ionHandler : LMGeocodeCompletionHandler ) {
370
+ func geocodeUsingGoogleAddressString( address address: NSString , onGeocodingCompletionHandler: LMGeocodeCompletionHandler ) {
367
371
368
372
self . geocodingCompletionHandler = onGeocodingCompletionHandler
369
373
@@ -379,7 +383,7 @@ class LocationManager: NSObject,CLLocationManagerDelegate {
379
383
performOperationForURL ( urlString, type: GeoCodingType . Geocoding)
380
384
}
381
385
382
- func reverseGeocodeLocationUsingGoogleWithLatLon( # latitude: Double, longitude: Double, onReverseGeocodingComplet ionHandler : LMReverseGeocodeCompletionHandler ) {
386
+ func reverseGeocodeLocationUsingGoogleWithLatLon( latitude latitude: Double , longitude: Double , onReverseGeocodingCompletionHandler: LMReverseGeocodeCompletionHandler ) {
383
387
384
388
self . reverseGeocodingCompletionHandler = onReverseGeocodingCompletionHandler
385
389
@@ -391,7 +395,7 @@ class LocationManager: NSObject,CLLocationManagerDelegate {
391
395
reverseGeocodeLocationUsingGoogleWithLatLon ( latitude: coord. coordinate. latitude, longitude: coord. coordinate. longitude, onReverseGeocodingCompletionHandler: onReverseGeocodingCompletionHandler)
392
396
}
393
397
394
- private func reverseGocodeUsingGoogle( # latitude: Double, longitude: Double) {
398
+ private func reverseGocodeUsingGoogle( latitude latitude: Double , longitude: Double ) {
395
399
396
400
var urlString = " http://maps.googleapis.com/maps/api/geocode/json?latlng= \( latitude) , \( longitude) " as NSString
397
401
@@ -412,7 +416,7 @@ class LocationManager: NSObject,CLLocationManagerDelegate {
412
416
413
417
if error != nil {
414
418
415
- self . setCompletionHandler ( responseInfo: nil , placemark: nil , error: error. localizedDescription, type: type)
419
+ self . setCompletionHandler ( responseInfo: nil , placemark: nil , error: error! . localizedDescription, type: type)
416
420
} else {
417
421
418
422
let kStatus = " status "
@@ -422,10 +426,8 @@ class LocationManager: NSObject,CLLocationManagerDelegate {
422
426
let kRequestDenied = " REQUEST_DENIED "
423
427
let kInvalidRequest = " INVALID_REQUEST "
424
428
let kInvalidInput = " Invalid Input "
425
-
426
- let dataAsString : NSString ? = NSString ( data: data, encoding: NSUTF8StringEncoding)
427
-
428
- let jsonResult : NSDictionary = NSJSONSerialization . JSONObjectWithData ( data, options: NSJSONReadingOptions . MutableContainers, error: nil ) as! NSDictionary
429
+
430
+ let jsonResult : NSDictionary = ( try ! NSJSONSerialization . JSONObjectWithData ( data!, options: NSJSONReadingOptions . MutableContainers) ) as! NSDictionary
429
431
430
432
var status = jsonResult. valueForKey ( kStatus) as! NSString
431
433
status = status. lowercaseString
@@ -453,7 +455,7 @@ class LocationManager: NSObject,CLLocationManagerDelegate {
453
455
} )
454
456
}
455
457
456
- private func setCompletionHandler( # responseInfo: NSDictionary? , placemark: CLPlacemark? , error: String? , type: GeoCodingType) {
458
+ private func setCompletionHandler( responseInfo responseInfo: NSDictionary ? , placemark: CLPlacemark ? , error: String ? , type: GeoCodingType ) {
457
459
458
460
if type == GeoCodingType . Geocoding {
459
461
self . geocodingCompletionHandler!( gecodeInfo: responseInfo, placemark: placemark, error: error)
@@ -499,7 +501,7 @@ private class AddressParser: NSObject{
499
501
500
502
private func getAddressDictionary( ) -> NSDictionary {
501
503
502
- var addressDict = NSMutableDictionary ( )
504
+ let addressDict = NSMutableDictionary ( )
503
505
504
506
addressDict. setValue ( latitude, forKey: " latitude " )
505
507
addressDict. setValue ( longitude, forKey: " longitude " )
@@ -516,17 +518,17 @@ private class AddressParser: NSObject{
516
518
517
519
private func parseAppleLocationData( placemark: CLPlacemark ) {
518
520
519
- var addressLines = placemark. addressDictionary [ " FormattedAddressLines " ] as! NSArray
521
+ var addressLines = placemark. addressDictionary ? [ " FormattedAddressLines " ] as! NSArray
520
522
521
523
//self.streetNumber = placemark.subThoroughfare ? placemark.subThoroughfare : ""
522
- self . streetNumber = placemark. thoroughfare != nil ? placemark. thoroughfare : " "
523
- self . locality = placemark. locality != nil ? placemark. locality : " "
524
- self . postalCode = placemark. postalCode != nil ? placemark. postalCode : " "
525
- self . subLocality = placemark. subLocality != nil ? placemark. subLocality : " "
526
- self . administrativeArea = placemark. administrativeArea != nil ? placemark. administrativeArea : " "
527
- self . country = placemark. country != nil ? placemark. country : " "
528
- self . longitude = placemark. location. coordinate. longitude. description;
529
- self . latitude = placemark. location. coordinate. latitude. description
524
+ self . streetNumber = ( placemark. thoroughfare != nil ? placemark. thoroughfare : " " ) !
525
+ self . locality = ( placemark. locality != nil ? placemark. locality : " " ) !
526
+ self . postalCode = ( placemark. postalCode != nil ? placemark. postalCode : " " ) !
527
+ self . subLocality = ( placemark. subLocality != nil ? placemark. subLocality : " " ) !
528
+ self . administrativeArea = placemark. administrativeArea != nil ? ( placemark. administrativeArea) ! : " "
529
+ self . country = placemark. country != nil ? placemark. country! : " "
530
+ self . longitude = placemark. location! . coordinate. longitude. description;
531
+ self . latitude = placemark. location! . coordinate. latitude. description
530
532
if addressLines. count> 0 {
531
533
self . formattedAddress = addressLines. componentsJoinedByString ( " , " )
532
534
} else {
@@ -569,8 +571,8 @@ private class AddressParser: NSObject{
569
571
570
572
let index : NSInteger = inArray. indexOfObjectPassingTest { ( obj, idx, stop) -> Bool in
571
573
572
- var objDict : NSDictionary = obj as! NSDictionary
573
- var types : NSArray = objDict. objectForKey ( " types " ) as! NSArray
574
+ let objDict : NSDictionary = obj as! NSDictionary
575
+ let types : NSArray = objDict. objectForKey ( " types " ) as! NSArray
574
576
let type = types. firstObject as! NSString
575
577
return type. isEqualToString ( component as String )
576
578
}
@@ -583,7 +585,7 @@ private class AddressParser: NSObject{
583
585
return " "
584
586
}
585
587
586
- var type = ( ( inArray. objectAtIndex ( index) as! NSDictionary ) . valueForKey ( ofType as String ) !) as! NSString
588
+ let type = ( ( inArray. objectAtIndex ( index) as! NSDictionary ) . valueForKey ( ofType as String ) !) as! NSString
587
589
588
590
if type. length > 0 {
589
591
@@ -632,7 +634,7 @@ private class AddressParser: NSObject{
632
634
var lng = self . longitude. doubleValue
633
635
var coordinate = CLLocationCoordinate2D ( latitude: lat, longitude: lng)
634
636
635
- var placemark = MKPlacemark ( coordinate: coordinate, addressDictionary: addressDict as [ NSObject : AnyObject ] )
637
+ var placemark = MKPlacemark ( coordinate: coordinate, addressDictionary: addressDict as! [ String : AnyObject ] )
636
638
637
639
return ( placemark as CLPlacemark )
638
640
}
0 commit comments