forked from allenwong/30DaysofSwift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
qianyb
committed
Dec 22, 2018
1 parent
02923b9
commit 6250030
Showing
4 changed files
with
84 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
Project 06 - FindMyLocation/Where/CLLocationDelegate.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// | ||
// CLLocationDelegate.swift | ||
// Where | ||
// | ||
// Created by qianyb on 2018/12/22. | ||
// Copyright © 2018 Allen. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import CoreLocation | ||
|
||
extension ViewController : CLLocationManagerDelegate { | ||
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { | ||
|
||
self.locationLabel.text = "Error while updating location " + error.localizedDescription | ||
|
||
} | ||
|
||
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { | ||
CLGeocoder().reverseGeocodeLocation(locations.first!) { (placemarks, error) in | ||
guard error == nil else { | ||
self.locationLabel.text = "Reverse geocoder failed with error" + error!.localizedDescription | ||
return | ||
} | ||
if placemarks!.count > 0 { | ||
let pm = placemarks!.first | ||
self.displayLocationInfo(pm) | ||
} else { | ||
self.locationLabel.text = "Problem with the data received from geocoder" | ||
} | ||
} | ||
} | ||
|
||
// func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { | ||
// CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: {(placemarks, error)->Void in | ||
// | ||
// if (error != nil) { | ||
// self.locationLabel.text = "Reverse geocoder failed with error" + error!.localizedDescription | ||
// return | ||
// } | ||
// | ||
// if placemarks!.count > 0 { | ||
// let pm = placemarks![0] | ||
// self.displayLocationInfo(pm) | ||
// } else { | ||
// self.locationLabel.text = "Problem with the data received from geocoder" | ||
// } | ||
// }) | ||
// } | ||
|
||
func displayLocationInfo(_ placemark: CLPlacemark?) { | ||
if let containsPlacemark = placemark { | ||
//stop updating location to save battery life | ||
locationManager.stopUpdatingLocation() | ||
|
||
let locality = (containsPlacemark.locality != nil) ? containsPlacemark.locality : "" | ||
let postalCode = (containsPlacemark.postalCode != nil) ? containsPlacemark.postalCode : "" | ||
let administrativeArea = (containsPlacemark.administrativeArea != nil) ? containsPlacemark.administrativeArea : "" | ||
let country = (containsPlacemark.country != nil) ? containsPlacemark.country : "" | ||
|
||
self.locationLabel.text = postalCode! + " " + locality! | ||
|
||
self.locationLabel.text?.append("\n" + administrativeArea! + ", " + country!) | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters