码迷,mamicode.com
首页 > 编程语言 > 详细

swift定位

时间:2015-04-02 10:21:33      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:

 1 //
 2 //  ViewController.swift
 3 //  JieCoreLocation
 4 //
 5 //  Created by jiezhang on 14-10-4.
 6 //  Copyright (c) 2014年 jiezhang. All rights reserved.
 7 //
 8 
 9 import UIKit
10 import CoreLocation
11 
12 class ViewController: UIViewController, CLLocationManagerDelegate{
13 
14     required init(coder aDecoder: NSCoder) {
15         super.init(coder: aDecoder)
16     }
17     
18     @IBOutlet weak var longitudeTxt: UITextField!
19     @IBOutlet weak var latitudeTxt: UITextField!
20     @IBOutlet weak var HeightTxt: UITextField!
21     @IBOutlet weak var addressTxt: UILabel!
22     var currLocation : CLLocation!
23     
24     //地址反编译出错误,不清楚什么问题,我是在模拟器上模拟的
25     @IBAction func reverseGeocode(sender: AnyObject) {
26         var geocoder = CLGeocoder()
27         var p:CLPlacemark?
28         geocoder.reverseGeocodeLocation(currLocation, completionHandler: { (placemarks, error) -> Void in
29             if error != nil {
30                 println("reverse geodcode fail: \(error.localizedDescription)")
31                 return
32             }
33             let pm = placemarks as [CLPlacemark]
34             if (pm.count > 0){
35                 p = placemarks[0] as? CLPlacemark
36                 println(p)
37             }else{
38                 println("No Placemarks!")
39             }
40         })
41     }
42     //用于定位服务管理类,它能够给我们提供位置信息和高度信息,也可以监控设备进入或离开某个区域,还可以获得设备的运行方向
43     let locationManager : CLLocationManager = CLLocationManager()
44     
45     override func viewDidLoad() {
46         super.viewDidLoad()
47         locationManager.delegate = self
48         //设备使用电池供电时最高的精度
49         locationManager.desiredAccuracy = kCLLocationAccuracyBest
50         //精确到1000米,距离过滤器,定义了设备移动后获得位置信息的最小距离
51         locationManager.distanceFilter = kCLLocationAccuracyKilometer
52         
53     }
54     
55     override func viewWillAppear(animated: Bool) {
56         locationManager.startUpdatingLocation()
57         println("定位开始")
58     }
59     
60     override func viewWillDisappear(animated: Bool) {
61         locationManager.stopUpdatingLocation()
62         println("定位结束")
63     }
64     
65     func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!){
66         currLocation = locations.last as CLLocation
67         longitudeTxt.text = "\(currLocation.coordinate.longitude)"
68         latitudeTxt.text = "\(currLocation.coordinate.latitude)"
69         HeightTxt.text = "\(currLocation.altitude)"
70     }
71     
72     func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!){
73         println(error)
74     }
75 
76     override func didReceiveMemoryWarning() {
77         super.didReceiveMemoryWarning()
78         
79     }
80 
81 
82 }

 

swift定位

标签:

原文地址:http://www.cnblogs.com/lovecc/p/4386073.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!