使用CoreLocation來完成定位信息。 原本我還想完成一下經緯度轉換爲具體地理位置的,可是一直報錯,之後補充吧。不想拖進度。 注意,info.plist文件的權限添加。 本項目只有一個類,ViewController:git
// // ViewController.swift // MyLocation // // Created by luopan on 16/8/6. // Copyright © 2016年 Hust University. All rights reserved. //swift
import UIKit框架
//導入定位框架包 import CoreLocationide
class ViewController: UIViewController , CLLocationManagerDelegate{.net
@IBOutlet weak var showLabel: UILabel! //定位管理器 var locationManager: CLLocationManager! //按鈕點擊事件 [@IBAction](http://my.oschina.net/u/866341) func GetLocation(sender: UIButton) { locationManager = CLLocationManager() // 設置代理爲當前對象 locationManager.delegate = self // 設置定位的精確度 locationManager.desiredAccuracy = kCLLocationAccuracyBest //受權定位 locationManager.requestAlwaysAuthorization() if CLLocationManager.locationServicesEnabled(){ // 開啓定位服務 locationManager.startUpdatingLocation() }else{ showLabel.text = "請打開定位服務。" } } //定位失敗回掉方法 func locationManager(manager: CLLocationManager, didFailWithError error: NSError) { showLabel.text = error.localizedDescription print(error.localizedDescription) } // 定位更新地理信息調用的代理方法 func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { if locations.count > 0 { let newLocation = locations.last let longitude = newLocation?.coordinate.longitude let latitude = newLocation?.coordinate.latitude self.showLabel.text = "經度是:\(longitude!)----緯度是:\(latitude!)" } } override func viewDidLoad() { super.viewDidLoad() }
}代理
嘗試了不少種方法去轉換經度度到具體地理信息,可是不成功。時間好玩,明天在搞吧。code