Swift基础--定位

//
//  ViewController.swift
//  JieCoreLocation
//
//  Created by jiezhang on 14-10-4.
//  Copyright (c) 2014年 jiezhang. All rights reserved.
//

import UIKit
import CoreLocation

class ViewController: 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()
        
    }


}





评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值