Swift-定位
2015-12-30 10:07
645 查看
[objc] view
plaincopyprint?
//
// ViewController.swift
// JieCoreLocation
//
// Created by jiezhang on 14-10-4.
// Copyright (c) 2014年 jiezhang. All rights reserved.
//
import UIKit
import CoreLocation
class ViewController:
4000
UIViewController, CLLocationManagerDelegate{
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
@IBOutlet weak var longitudeTxt: UITextField!
@IBOutlet weak var latitudeTxt: UITextField!
@IBOutlet weak var HeightTxt: UITextField!
@IBOutlet weak var addressTxt: UILabel!
var currLocation : CLLocation!
//地址反编译出错误,不清楚什么问题,我是在模拟器上模拟的
@IBAction func reverseGeocode(sender: AnyObject) {
var geocoder = CLGeocoder()
var p:CLPlacemark?
geocoder.reverseGeocodeLocation(currLocation, completionHandler: { (placemarks, error) -> Void in
if error != nil {
println("reverse geodcode fail: \(error.localizedDescription)")
return
}
let pm = placemarks as [CLPlacemark]
if (pm.count > 0){
p = placemarks[0] as? CLPlacemark
println(p)
}else{
println("No Placemarks!")
}
})
}
//用于定位服务管理类,它能够给我们提供位置信息和高度信息,也可以监控设备进入或离开某个区域,还可以获得设备的运行方向
let locationManager : CLLocationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
//设备使用电池供电时最高的精度
locationManager.desiredAccuracy = kCLLocationAccuracyBest
//精确到1000米,距离过滤器,定义了设备移动后获得位置信息的最小距离
locationManager.distanceFilter = kCLLocationAccuracyKilometer
}
override func viewWillAppear(animated: Bool) {
locationManager.startUpdatingLocation()
println("定位开始")
}
override func viewWillDisappear(animated: Bool) {
locationManager.stopUpdatingLocation()
println("定位结束")
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!){
currLocation = locations.last as CLLocation
longitudeTxt.text = "\(currLocation.coordinate.longitude)"
latitudeTxt.text = "\(currLocation.coordinate.latitude)"
HeightTxt.text = "\(currLocation.altitude)"
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!){
println(error)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
plaincopyprint?
//
// ViewController.swift
// JieCoreLocation
//
// Created by jiezhang on 14-10-4.
// Copyright (c) 2014年 jiezhang. All rights reserved.
//
import UIKit
import CoreLocation
class ViewController:
4000
UIViewController, CLLocationManagerDelegate{
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
@IBOutlet weak var longitudeTxt: UITextField!
@IBOutlet weak var latitudeTxt: UITextField!
@IBOutlet weak var HeightTxt: UITextField!
@IBOutlet weak var addressTxt: UILabel!
var currLocation : CLLocation!
//地址反编译出错误,不清楚什么问题,我是在模拟器上模拟的
@IBAction func reverseGeocode(sender: AnyObject) {
var geocoder = CLGeocoder()
var p:CLPlacemark?
geocoder.reverseGeocodeLocation(currLocation, completionHandler: { (placemarks, error) -> Void in
if error != nil {
println("reverse geodcode fail: \(error.localizedDescription)")
return
}
let pm = placemarks as [CLPlacemark]
if (pm.count > 0){
p = placemarks[0] as? CLPlacemark
println(p)
}else{
println("No Placemarks!")
}
})
}
//用于定位服务管理类,它能够给我们提供位置信息和高度信息,也可以监控设备进入或离开某个区域,还可以获得设备的运行方向
let locationManager : CLLocationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
//设备使用电池供电时最高的精度
locationManager.desiredAccuracy = kCLLocationAccuracyBest
//精确到1000米,距离过滤器,定义了设备移动后获得位置信息的最小距离
locationManager.distanceFilter = kCLLocationAccuracyKilometer
}
override func viewWillAppear(animated: Bool) {
locationManager.startUpdatingLocation()
println("定位开始")
}
override func viewWillDisappear(animated: Bool) {
locationManager.stopUpdatingLocation()
println("定位结束")
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!){
currLocation = locations.last as CLLocation
longitudeTxt.text = "\(currLocation.coordinate.longitude)"
latitudeTxt.text = "\(currLocation.coordinate.latitude)"
HeightTxt.text = "\(currLocation.altitude)"
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!){
println(error)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
相关文章推荐
- Apple Swift学习教程
- CSS浮动和定位学习笔记
- 介绍 Fedora 上的 Swift
- C#实现txt定位指定行完整实例
- Flex中怎么给表格中的滚动条定位避免刷新回到原处
- 一个JavaScript操作元素定位元素的实例
- js中的如何定位固定层的位置
- JS弹出层单纯的绝对定位居中示例代码
- 讲解iOS开发中基本的定位功能实现
- Swift中实现点击、双击、捏、旋转、拖动、划动、长按手势的类和方法介绍
- Swift编程中的泛型解析
- Swift中定义二维数组的方法及遍历方法示例
- JavaScript网页定位详解
- 微信企业号开发之微信考勤百度地图定位
- 在Android里完美实现基站和WIFI定位
- android 定位的4种方式介绍
- 基于JavaScript实现根据手机定位获取当前具体位置(X省X市X县X街道X号)
- 基于Android实现百度地图定位过程详解
- Android判断服务是否运行及定位问题实例分析