地圖定位

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.
    }

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末劳殖,一起剝皮案震驚了整個濱河市毁靶,隨后出現(xiàn)的幾起案子辜限,更是在濱河造成了極大的恐慌揪垄,老刑警劉巖穷吮,帶你破解...
    沈念sama閱讀 218,451評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異饥努,居然都是意外死亡捡鱼,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,172評論 3 394
  • 文/潘曉璐 我一進店門酷愧,熙熙樓的掌柜王于貴愁眉苦臉地迎上來驾诈,“玉大人,你說我怎么就攤上這事溶浴≌” “怎么了?”我有些...
    開封第一講書人閱讀 164,782評論 0 354
  • 文/不壞的土叔 我叫張陵戳葵,是天一觀的道長就乓。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么生蚁? 我笑而不...
    開封第一講書人閱讀 58,709評論 1 294
  • 正文 為了忘掉前任噩翠,我火速辦了婚禮,結(jié)果婚禮上邦投,老公的妹妹穿的比我還像新娘伤锚。我一直安慰自己,他們只是感情好志衣,可當(dāng)我...
    茶點故事閱讀 67,733評論 6 392
  • 文/花漫 我一把揭開白布屯援。 她就那樣靜靜地躺著,像睡著了一般念脯。 火紅的嫁衣襯著肌膚如雪狞洋。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,578評論 1 305
  • 那天绿店,我揣著相機與錄音吉懊,去河邊找鬼。 笑死假勿,一個胖子當(dāng)著我的面吹牛借嗽,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播转培,決...
    沈念sama閱讀 40,320評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼恶导,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了浸须?” 一聲冷哼從身側(cè)響起惨寿,我...
    開封第一講書人閱讀 39,241評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎羽戒,沒想到半個月后缤沦,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,686評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡易稠,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,878評論 3 336
  • 正文 我和宋清朗相戀三年缸废,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片驶社。...
    茶點故事閱讀 39,992評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡企量,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出亡电,到底是詐尸還是另有隱情届巩,我是刑警寧澤,帶...
    沈念sama閱讀 35,715評論 5 346
  • 正文 年R本政府宣布份乒,位于F島的核電站恕汇,受9級特大地震影響腕唧,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜瘾英,卻給世界環(huán)境...
    茶點故事閱讀 41,336評論 3 330
  • 文/蒙蒙 一枣接、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧缺谴,春花似錦但惶、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,912評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至阳啥,卻和暖如春添谊,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背苫纤。 一陣腳步聲響...
    開封第一講書人閱讀 33,040評論 1 270
  • 我被黑心中介騙來泰國打工碉钠, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留纲缓,地道東北人卷拘。 一個月前我還...
    沈念sama閱讀 48,173評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像祝高,于是被迫代替她去往敵國和親栗弟。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,947評論 2 355

推薦閱讀更多精彩內(nèi)容