import UIKit
import MapKit
class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
let locateMannage:CLLocationManager = CLLocationManager()
var btn:UIButton = UIButton(type: .System)
var mapView:MKMapView = MKMapView()
var currentCoordinate:CLLocationCoordinate2D?
override func viewDidLoad() {
super.viewDidLoad()
// 添加地圖視圖
mapView = MKMapView.init(frame: self.view.frame)
mapView.mapType = MKMapType.Standard
//mapView.scaleOrigin = CGPointMake(100, mapView.frame.size.height-20)
self.view.addSubview(mapView)
// 按鈕
btn.frame = CGRectMake(10, 30, 100, 50)
btn.setTitle("定位", forState: .Normal)
btn.addTarget(self, action: "setLocation:", forControlEvents: .TouchUpInside)
self.view.addSubview(btn)
self.locateMannage.delegate = self
// 發(fā)送授權(quán)
if self.locateMannage.respondsToSelector(Selector("requestAlwaysAuthorization")) {
self.locateMannage.requestAlwaysAuthorization()
}
// 精度
self.locateMannage.desiredAccuracy = kCLLocationAccuracyBest
// 更新距離
locateMannage.distanceFilter = 100
// 開啟更新位置服務(wù)
//self.locateMannage.startUpdatingHeading()
}
// CLLocationManager代理方法
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// 獲取最后的位置信息
self.locateMannage.stopUpdatingHeading()
let newLocation:CLLocation = locations.last!
// 設(shè)置大小(經(jīng)緯度)1緯度約等于111千米
let currentLocationSpan:MKCoordinateSpan = MKCoordinateSpanMake(0.03, 0.03)
let currentRegion:MKCoordinateRegion = MKCoordinateRegion(center: newLocation.coordinate, span: currentLocationSpan)
self.mapView.setRegion(currentRegion, animated: true)
// 反編碼
CLGeocoder().reverseGeocodeLocation(newLocation, completionHandler: { (pms, err) -> Void in
//取得最后一個地標圾叼,地標中存儲了詳細的地址信息,注意:一個地名可能搜索出多個地址
let placemark:CLPlacemark = (pms!.last)!
self.currentCoordinate = placemark.location?.coordinate
let location = placemark.location;//位置
let region = placemark.region;//區(qū)域
let addressDic = placemark.addressDictionary;//詳細地址信息字典,包含以下部分信息
// let name=placemark.name;//地名 廣匯花苑
// let thoroughfare=placemark.thoroughfare;//街道 ---斜土路
// let subThoroughfare=placemark.subThoroughfare; //街道相關(guān)信息,例如門牌等 ---1981號
// let locality=placemark.locality; // 城市 ---上海市
// let subLocality=placemark.subLocality; // 城市相關(guān)信息嗓袱,例如標志性建筑 ----徐匯區(qū)
// let administrativeArea=placemark.administrativeArea; // 行政管理區(qū)域 ----上海市
// let subAdministrativeArea=placemark.subAdministrativeArea; //其他行政區(qū)域信息 ---nil(因該是xxx省xxx市)
// let postalCode=placemark.postalCode; //郵編
// let ISOcountryCode=placemark.ISOcountryCode; //國家編碼 CN
// let country=placemark.country; //國家
// let inlandWater=placemark.inlandWater; //水源狰晚、湖泊
// let ocean=placemark.ocean; // 海洋
// let areasOfInterest=placemark.areasOfInterest; //關(guān)聯(lián)的或利益相關(guān)的地標
// name = "中國河南省鄭州市管城回族區(qū)北下街街道東太康路25號";
//
// administrativeArea = "河南省";
// country = "中國";
// countryCode = CN; -- ISOcountryCode
// locality = "鄭州市";
// subLocality = "管城回族區(qū)";
// subThoroughfare = "25號";
// thoroughfare = "東太康路";
// timezone = {
// identifier = "Asia/Shanghai";
// };
print(region)
print("====\(addressDic)")
// 添加大頭針
//創(chuàng)建一個大頭針對象
let bigPin = MKPointAnnotation()
//設(shè)置大頭針的顯示位置
bigPin.coordinate = location!.coordinate
//設(shè)置點擊大頭針之后顯示的標題
bigPin.title = "\(placemark.locality)--\(placemark.subLocality)"
//設(shè)置點擊大頭針之后顯示的描述
bigPin.subtitle = "\(placemark.thoroughfare)--\(placemark.subThoroughfare)"
//添加大頭針
self.mapView.addAnnotation(bigPin)
})
print("經(jīng)緯度\(newLocation.coordinate.longitude)====\(newLocation.coordinate.latitude)")
}
func setLocation(sender: UIButton) {
// if self.currentCoordinate != nil {
// self.mapView.setCenterCoordinate(self.currentCoordinate!, animated: true)
// }
self.locateMannage.startUpdatingLocation()
mapView.showsUserLocation = true
print("點擊定位")
// 編碼
CLGeocoder().geocodeAddressString("中國河南省鄭州市金水區(qū)") { (pms, err) -> Void in
let placemark:CLPlacemark = (pms?.last)!
print(placemark.location!)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}