高德地圖集成及基礎(chǔ)功能介紹

集成時(shí)我們首先要申請(qǐng)本應(yīng)用對(duì)應(yīng)的key這個(gè)在官方文檔里面介紹的很詳細(xì)

在使用高德地圖前奈搜,我們首先要在code中注冊(cè)key

AMapServices.shared().apiKey = "a75721f344a52d***************";

初始化

   //地圖初始化
     let mapView =  MAMapView(frame: UIScreen.main.bounds);
    //搜索初始化
     let search = AMapSearchAPI()

相關(guān)配置

        view.addSubview(mapView)
        //顯示地圖范圍(比例)
        mapView.zoomLevel = 14;
        //配置(可選)
        let config = MAUserLocationRepresentation();
        config.showsHeadingIndicator = true;
        config.locationDotBgColor = UIColor.green;
        config.image = UIImage(named: "test");
        mapView.update(config);
        // mapView.logoCenter = CGPoint(x: view.bounds.width + 155, y: 450);
        //指南針及其位置
        mapView.showsCompass = true;
        mapView.compassOrigin = CGPoint(x: view.bounds.width - 45, y: 64);
        //旋轉(zhuǎn)手勢(shì)關(guān)閉
        mapView.isRotateEnabled = false;
        //相關(guān)代理
        mapView.delegate = self;
        //設(shè)置定位精度
        mapView.desiredAccuracy = kCLLocationAccuracyBest;
        //設(shè)置定位距離
        mapView.distanceFilter = 1.0;
        //普通樣式
        mapView.mapType = .standard;
        //防止系統(tǒng)自動(dòng)殺掉定位 -- 后臺(tái)定位
        mapView.pausesLocationUpdatesAutomatically = false;
        //需要設(shè)置plist文件 Required background modes : App registers for location updates
        mapView.allowsBackgroundLocationUpdates = true;
        //確保定位和跟隨設(shè)置不被覆蓋
        mapView.showsUserLocation = true;
        mapView.userTrackingMode = .follow

截屏

//截屏
        let image =  mapView.takeSnapshot(in: self.view.bounds);

地址正向解析和逆地址編碼(具體結(jié)果需要代理實(shí)現(xiàn))

        let userLocation = mapView.userLocation.coordinate;
        NSLog("first latitude : %f , longitude : %f",userLocation.latitude,userLocation.longitude);
        let regeo = AMapReGeocodeSearchRequest();
        regeo.location = AMapGeoPoint.location(withLatitude: CGFloat(userLocation.latitude), longitude: CGFloat(userLocation.longitude));
        regeo.requireExtension = true;
        //逆地址編碼
        search?.delegate = self
        search?.aMapReGoecodeSearch(regeo);
        //正向地址解析
        let request = AMapGeocodeSearchRequest();
        request.address = "成都市天府廣場(chǎng)";
        search?.aMapGeocodeSearch(request);

地址正向解析和逆地址編碼代理實(shí)現(xiàn)(正向地址解析完成時(shí)杭跪,在解析地址處添加大頭針)

//MAKE: 逆地址編碼
extension MapViewController: AMapSearchDelegate{
    func onReGeocodeSearchDone(_ request: AMapReGeocodeSearchRequest!, response: AMapReGeocodeSearchResponse!) {
        guard response.regeocode != nil  else {
            return
        }
        let returnValue = response.regeocode.addressComponent.building;
        let city: String = response.regeocode.addressComponent.city;
        NSLog("%@",city );
        print("\(String(describing: returnValue))")
    }
    //MAKE: 地址正向解析
    func onGeocodeSearchDone(_ request: AMapGeocodeSearchRequest!, response: AMapGeocodeSearchResponse!) {
        guard response.geocodes.count != 0 else {
            return;
        }
        for geocode in response.geocodes {
            print("latitude: \(geocode.location.latitude) ,longitude: \(geocode.location.longitude)");
            //大頭針妥衣。正向地址解析完成時(shí)哑诊,在解析地址處添加大頭針
            //需要實(shí)現(xiàn)代理
            let pointAnnotation = MAPointAnnotation();
            pointAnnotation.coordinate = CLLocationCoordinate2D(latitude: CLLocationDegrees(geocode.location.latitude), longitude: CLLocationDegrees(geocode.location.longitude))
            pointAnnotation.title = geocode.building
            pointAnnotation.subtitle = geocode.formattedAddress
            mapView.addAnnotation(pointAnnotation);
        }
        
    }
}

添加大頭針需要實(shí)現(xiàn)的代理

    //大頭針代理
    func mapView(_ mapView: MAMapView!, viewFor annotation: MAAnnotation!) -> MAAnnotationView! {
        if annotation.isKind(of: MAPointAnnotation.self) {
            let pointReuseIndetifier = "pointReuseIndetifier";
            var annottationView: MAPinAnnotationView? = mapView.dequeueReusableAnnotationView(withIdentifier: pointReuseIndetifier) as! MAPinAnnotationView?
            if  annottationView == nil {
                annottationView = MAPinAnnotationView(annotation: annotation, reuseIdentifier: pointReuseIndetifier);
            }
            annottationView?.canShowCallout = true;
            annottationView?.animatesDrop = true;
            annottationView?.isDraggable = true;
            annottationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
            return annottationView
        }
        
        return nil
    }

結(jié)果:


9C813AC3-9E16-4283-9EF1-85F3A5A4BEEE.png

自定義大頭針視圖

class CustomCalloutView: UIView {
    
    public var portraitView: UIImageView!
    public var subtitleLabel: UILabel!
    public var titlelabel: UILabel!
    
    override init(frame: CGRect) {
        super.init(frame: frame);
        self.backgroundColor = UIColor.clear;
        initSubViews();
        
    }
    func initSubViews() {
        self.portraitView = UIImageView(frame: CGRect(x: 5, y: 5, width: 70, height: 50))
        portraitView.backgroundColor = UIColor.black;
        self.addSubview(portraitView);
        titlelabel = UILabel(frame: CGRect(x: 10 + 70, y: 5, width: 120, height: 20));
        titlelabel.font = UIFont.boldSystemFont(ofSize: 14);
        titlelabel.textColor = UIColor.white;
        titlelabel.text = "something say title";
        self.addSubview(titlelabel)
        subtitleLabel = UILabel(frame: CGRect(x: 10 + 70, y: 30, width: 120, height: 20));
        subtitleLabel.font = UIFont.systemFont(ofSize: 12);
        subtitleLabel.textColor = UIColor.lightGray;
        subtitleLabel.text = "afhkshfksahflaskfsdnlkasjflaks rubish";
        self.addSubview(subtitleLabel);
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    override func draw(_ rect: CGRect) {
        self.drawInContext(context: UIGraphicsGetCurrentContext()!);
        self.layer.shadowColor = UIColor.black.cgColor;
        self.layer.shadowOpacity = 1.0;
        self.layer.shadowOffset = CGSize(width: 0, height: 0)
        
    }
    
    func drawInContext(context: CGContext) {
        context.setLineWidth(2.0);
        context.setFillColor(UIColor.coloreWithRGB(red: 77, green: 77, blue: 77, alpha: 0.3).cgColor);
        self.getDrawPath(context: context);
        context.fillPath();

    }
    
    func getDrawPath(context: CGContext){
        let rrect = self.bounds;
        let radius:CGFloat = 6.0;
        let minx:CGFloat = rrect.minX
        let midx:CGFloat = rrect.midX;
        let maxx:CGFloat = rrect.maxX;
        let miny:CGFloat = rrect.minY;
        let maxy:CGFloat = rrect.maxY - 10;
        context.move(to: CGPoint(x: midx + 10, y: maxy));
        context.addLine(to:  CGPoint(x: midx + 10, y: maxy + 10))
        context.addLine(to:  CGPoint(x: midx - 10, y: maxy));
        
        context.addArc(tangent1End:  CGPoint(x: minx, y: maxy), tangent2End:  CGPoint(x: midx, y: miny), radius: radius);
        context.addArc(tangent1End:  CGPoint(x: minx, y:minx), tangent2End:  CGPoint(x: maxx, y: miny), radius: radius);
        context.addArc(tangent1End:  CGPoint(x: maxx, y: miny), tangent2End:  CGPoint(x: maxx , y: maxx), radius: radius);
        context.addArc(tangent1End:  CGPoint(x: maxx, y: maxy), tangent2End:  CGPoint(x: midx, y: maxy), radius: radius);
        context.closePath();
    }
}
class CustomAnnotationView: MAAnnotationView {
    public var calloutView: CustomCalloutView?
    
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        
        if selected {
            if calloutView == nil {
                calloutView = CustomCalloutView(frame: CGRect(x: 0, y: 0, width: 200, height: 70));
                calloutView!.center = CGPoint(x: self.bounds.width / 2 + self.calloutOffset.x, y: -self.bounds.height / 2 + self.calloutOffset.y);
                calloutView!.portraitView.image = UIImage(named: "test");
                calloutView!.titlelabel.text = self.annotation.title;
                calloutView!.subtitleLabel.text = self.annotation.subtitle;
                self.addSubview(calloutView!)
            }
        }else {
            calloutView?.removeFromSuperview();
        }
        super.setSelected(selected, animated: animated);
    }

}
            let pointReuseIndetifier = "pointReuseIndetifier";
            //自定義
            var annotationView: CustomAnnotationView? = mapView.dequeueReusableAnnotationView(withIdentifier: pointReuseIndetifier) as! CustomAnnotationView?
            if annotationView == nil {
                annotationView = CustomAnnotationView(annotation: annotation, reuseIdentifier: pointReuseIndetifier);
            }
            annotationView?.image = UIImage(named: "test");
            // 設(shè)置為NO吠冤,用以調(diào)用自定義的calloutView
            annotationView?.canShowCallout = false
            let button = UIButton(type: .detailDisclosure)
            annotationView?.rightCalloutAccessoryView = button;
           //選中當(dāng)前自定義大頭針
            annotationView?.setSelected(true, animated: true)
            // 設(shè)置中心點(diǎn)偏移雕沉,使得標(biāo)注底部中間點(diǎn)成為經(jīng)緯度對(duì)應(yīng)點(diǎn)
            annotationView!.centerOffset = CGPoint(x: -10, y: 0)
tmp24f32b21.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末芭届,一起剝皮案震驚了整個(gè)濱河市蛔钙,隨后出現(xiàn)的幾起案子锌云,更是在濱河造成了極大的恐慌,老刑警劉巖吁脱,帶你破解...
    沈念sama閱讀 212,884評(píng)論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件桑涎,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡兼贡,警方通過(guò)查閱死者的電腦和手機(jī)攻冷,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,755評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)遍希,“玉大人等曼,你說(shuō)我怎么就攤上這事》醢啵” “怎么了涉兽?”我有些...
    開封第一講書人閱讀 158,369評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)篙程。 經(jīng)常有香客問(wèn)我枷畏,道長(zhǎng),這世上最難降的妖魔是什么虱饿? 我笑而不...
    開封第一講書人閱讀 56,799評(píng)論 1 285
  • 正文 為了忘掉前任拥诡,我火速辦了婚禮,結(jié)果婚禮上氮发,老公的妹妹穿的比我還像新娘渴肉。我一直安慰自己,他們只是感情好爽冕,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,910評(píng)論 6 386
  • 文/花漫 我一把揭開白布仇祭。 她就那樣靜靜地躺著,像睡著了一般颈畸。 火紅的嫁衣襯著肌膚如雪乌奇。 梳的紋絲不亂的頭發(fā)上没讲,一...
    開封第一講書人閱讀 50,096評(píng)論 1 291
  • 那天,我揣著相機(jī)與錄音礁苗,去河邊找鬼爬凑。 笑死,一個(gè)胖子當(dāng)著我的面吹牛试伙,可吹牛的內(nèi)容都是我干的嘁信。 我是一名探鬼主播,決...
    沈念sama閱讀 39,159評(píng)論 3 411
  • 文/蒼蘭香墨 我猛地睜開眼疏叨,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼潘靖!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起考廉,我...
    開封第一講書人閱讀 37,917評(píng)論 0 268
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤秘豹,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后昌粤,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體既绕,經(jīng)...
    沈念sama閱讀 44,360評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,673評(píng)論 2 327
  • 正文 我和宋清朗相戀三年涮坐,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了凄贩。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,814評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡袱讹,死狀恐怖疲扎,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情捷雕,我是刑警寧澤椒丧,帶...
    沈念sama閱讀 34,509評(píng)論 4 334
  • 正文 年R本政府宣布,位于F島的核電站救巷,受9級(jí)特大地震影響壶熏,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜浦译,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,156評(píng)論 3 317
  • 文/蒙蒙 一棒假、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧精盅,春花似錦帽哑、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,882評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至,卻和暖如春佳头,著一層夾襖步出監(jiān)牢的瞬間鹰贵,已是汗流浹背晴氨。 一陣腳步聲響...
    開封第一講書人閱讀 32,123評(píng)論 1 267
  • 我被黑心中介騙來(lái)泰國(guó)打工康嘉, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人籽前。 一個(gè)月前我還...
    沈念sama閱讀 46,641評(píng)論 2 362
  • 正文 我出身青樓亭珍,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親枝哄。 傳聞我的和親對(duì)象是個(gè)殘疾皇子肄梨,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,728評(píng)論 2 351

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