地圖定位

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
        // 開(kāi)啟更新位置服務(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
            
                //取得最后一個(gè)地標(biāo),地標(biāo)中存儲(chǔ)了詳細(xì)的地址信息,注意:一個(gè)地名可能搜索出多個(gè)地址
                let placemark:CLPlacemark = (pms!.last)!
                self.currentCoordinate = placemark.location?.coordinate
                let location = placemark.location;//位置
                let region = placemark.region;//區(qū)域
                let addressDic = placemark.addressDictionary;//詳細(xì)地址信息字典,包含以下部分信息
//                let name=placemark.name;//地名  廣匯花苑
//                let thoroughfare=placemark.thoroughfare;//街道 ---斜土路
//                let subThoroughfare=placemark.subThoroughfare; //街道相關(guān)信息,例如門牌等   ---1981號(hào)
//                let locality=placemark.locality; // 城市   ---上海市
//                let subLocality=placemark.subLocality; // 城市相關(guān)信息,例如標(biāo)志性建筑 ----徐匯區(qū)
//                let administrativeArea=placemark.administrativeArea; // 行政管理區(qū)域   ----上海市
//                let subAdministrativeArea=placemark.subAdministrativeArea; //其他行政區(qū)域信息  ---nil(因該是xxx省xxx市)
//                let postalCode=placemark.postalCode; //郵編
//                let ISOcountryCode=placemark.ISOcountryCode; //國(guó)家編碼   CN
//                let country=placemark.country; //國(guó)家
//                let inlandWater=placemark.inlandWater; //水源、湖泊
//                let ocean=placemark.ocean; // 海洋
//                let areasOfInterest=placemark.areasOfInterest; //關(guān)聯(lián)的或利益相關(guān)的地標(biāo)
//                    name =  "中國(guó)河南省鄭州市管城回族區(qū)北下街街道東太康路25號(hào)";
//
//                    administrativeArea = "河南省";
//                    country = "中國(guó)";
//                    countryCode = CN; -- ISOcountryCode
//                    locality = "鄭州市";
//                    subLocality = "管城回族區(qū)";
//                    subThoroughfare = "25號(hào)";
//                    thoroughfare = "東太康路";
//                    timezone =                             {
//                        identifier = "Asia/Shanghai";
//                    };
            
            print(region)
            print("====\(addressDic)")
            
            // 添加大頭針
            //創(chuàng)建一個(gè)大頭針對(duì)象
            let bigPin = MKPointAnnotation()
            //設(shè)置大頭針的顯示位置
            bigPin.coordinate = location!.coordinate
            //設(shè)置點(diǎn)擊大頭針之后顯示的標(biāo)題
            bigPin.title = "\(placemark.locality)--\(placemark.subLocality)"
            //設(shè)置點(diǎn)擊大頭針之后顯示的描述
            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("點(diǎn)擊定位")
        // 編碼
        CLGeocoder().geocodeAddressString("中國(guó)河南省鄭州市金水區(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)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 轉(zhuǎn)載、引用請(qǐng)標(biāo)明出處http://www.itdecent.cn/p/29ccac3e1e42本文出自zhh_ha...
    zhh_happig閱讀 26,460評(píng)論 18 99
  • 地圖 準(zhǔn)備工作 導(dǎo)入MapKit框架(iOS5之后不在需要程序員自己導(dǎo)入) 導(dǎo)入主頭文件#import MapKi...
    Joker_King閱讀 887評(píng)論 1 2
  • 前言 在開(kāi)發(fā)項(xiàng)目過(guò)程中有時(shí)候需要獲取用戶的精確位置信息,但是我們都知道,直接獲取的地址可能會(huì)有誤差的,這時(shí)候就需要...
    張叔叔閱讀 9,493評(píng)論 26 55
  • 如果你不開(kāi)心, 就發(fā)泄出來(lái)吧, 要取悅自己, 才不會(huì)成為自己的負(fù)擔(dān)。 如果有人告訴你, 舍己為人、忍讓寬容是美德,...
    燕三井閱讀 137評(píng)論 0 0
  • 葡萄上架, 黃瓜吊起, 無(wú)憂炎夏。 麻雀叫板, 燕燕呢喃, 庭院相見(jiàn)。 白云飄蕩, 山尖戴帽, 有福同享。 茶罐煎...
    木貞ma閱讀 211評(píng)論 1 1

友情鏈接更多精彩內(nèi)容