Skip to content

Commit

Permalink
第六天
Browse files Browse the repository at this point in the history
  • Loading branch information
qianyb committed Dec 22, 2018
1 parent 02923b9 commit 6250030
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 48 deletions.
7 changes: 7 additions & 0 deletions Project 06 - FindMyLocation/Where.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
141553AC21CE9168003A3632 /* CLLocationDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 141553AB21CE9168003A3632 /* CLLocationDelegate.swift */; };
9F0553631C44CD3D00204668 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F0553621C44CD3D00204668 /* AppDelegate.swift */; };
9F0553651C44CD3D00204668 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F0553641C44CD3D00204668 /* ViewController.swift */; };
9F0553681C44CD3D00204668 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 9F0553661C44CD3D00204668 /* Main.storyboard */; };
Expand All @@ -16,6 +17,7 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
141553AB21CE9168003A3632 /* CLLocationDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CLLocationDelegate.swift; sourceTree = "<group>"; };
9F05535F1C44CD3D00204668 /* Where.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Where.app; sourceTree = BUILT_PRODUCTS_DIR; };
9F0553621C44CD3D00204668 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
9F0553641C44CD3D00204668 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -59,6 +61,7 @@
isa = PBXGroup;
children = (
9F0553621C44CD3D00204668 /* AppDelegate.swift */,
141553AB21CE9168003A3632 /* CLLocationDelegate.swift */,
9F0553641C44CD3D00204668 /* ViewController.swift */,
9F0553661C44CD3D00204668 /* Main.storyboard */,
9F05536C1C44CD3D00204668 /* Assets.xcassets */,
Expand Down Expand Up @@ -100,6 +103,7 @@
TargetAttributes = {
9F05535E1C44CD3D00204668 = {
CreatedOnToolsVersion = 7.2;
DevelopmentTeam = F3YT8BWBYJ;
LastSwiftMigration = 0920;
};
};
Expand Down Expand Up @@ -141,6 +145,7 @@
buildActionMask = 2147483647;
files = (
9F0553651C44CD3D00204668 /* ViewController.swift in Sources */,
141553AC21CE9168003A3632 /* CLLocationDelegate.swift in Sources */,
9F0553631C44CD3D00204668 /* AppDelegate.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -269,6 +274,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = F3YT8BWBYJ;
INFOPLIST_FILE = Where/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = me.appkitchen.Where;
Expand All @@ -282,6 +288,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = F3YT8BWBYJ;
INFOPLIST_FILE = Where/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = me.appkitchen.Where;
Expand Down
67 changes: 67 additions & 0 deletions Project 06 - FindMyLocation/Where/CLLocationDelegate.swift
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!)
}

}
}
10 changes: 6 additions & 4 deletions Project 06 - FindMyLocation/Where/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Where are you?</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Allow to use location and can send it to me</string>
<key>LSApplicationCategoryType</key>
<string></string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>让我定个位吧</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Where are you?</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Allow to use location and can send it to me</string>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
Expand Down
48 changes: 4 additions & 44 deletions Project 06 - FindMyLocation/Where/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
import UIKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {
class ViewController: UIViewController {

@IBOutlet weak var locationLabel: UILabel!

// 强制自动解包,可以赋值为nil,为nil后再调用会报错
// 建议定义为:
// var locationManager: CLLocationManager
var locationManager: CLLocationManager!

override func viewDidLoad() {
Expand All @@ -38,49 +41,6 @@ class ViewController: UIViewController, CLLocationManagerDelegate {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

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(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!)
}

}




}

0 comments on commit 6250030

Please sign in to comment.