美文网首页
高德地图集成及基础功能介绍

高德地图集成及基础功能介绍

作者: hoggenWang | 来源:发表于2017-05-05 10:24 被阅读585次

集成时我们首先要申请本应用对应的key这个在官方文档里面介绍的很详细

在使用高德地图前,我们首先要在code中注册key

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

初始化

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

相关配置

        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);
        //旋转手势关闭
        mapView.isRotateEnabled = false;
        //相关代理
        mapView.delegate = self;
        //设置定位精度
        mapView.desiredAccuracy = kCLLocationAccuracyBest;
        //设置定位距离
        mapView.distanceFilter = 1.0;
        //普通样式
        mapView.mapType = .standard;
        //防止系统自动杀掉定位 -- 后台定位
        mapView.pausesLocationUpdatesAutomatically = false;
        //需要设置plist文件 Required background modes : App registers for location updates
        mapView.allowsBackgroundLocationUpdates = true;
        //确保定位和跟随设置不被覆盖
        mapView.showsUserLocation = true;
        mapView.userTrackingMode = .follow

截屏

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

地址正向解析和逆地址编码(具体结果需要代理实现)

        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 = "成都市天府广场";
        search?.aMapGeocodeSearch(request);

地址正向解析和逆地址编码代理实现(正向地址解析完成时,在解析地址处添加大头针)

//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)");
            //大头针。正向地址解析完成时,在解析地址处添加大头针
            //需要实现代理
            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);
        }
        
    }
}

添加大头针需要实现的代理

    //大头针代理
    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
    }

结果:


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");
            // 设置为NO,用以调用自定义的calloutView
            annotationView?.canShowCallout = false
            let button = UIButton(type: .detailDisclosure)
            annotationView?.rightCalloutAccessoryView = button;
           //选中当前自定义大头针
            annotationView?.setSelected(true, animated: true)
            // 设置中心点偏移,使得标注底部中间点成为经纬度对应点
            annotationView!.centerOffset = CGPoint(x: -10, y: 0)
tmp24f32b21.png

相关文章

  • 高德地图集成及基础功能介绍

    集成时我们首先要申请本应用对应的key这个在官方文档里面介绍的很详细 在使用高德地图前,我们首先要在code中注册...

  • iOS 高德地图的使用

    iOS:高德地图的使用 本人花了点时间集成了高德地图的几乎所有的功能,包含:地图的显示、地图的绘制、地图的定位、地...

  • kotlin撸一个天气预报app

    效果图 功能介绍: 支持定位当前所在城市(集成高德地图) 支持加载多个城市天气(用户配置尚未实现,可以在代码里加)...

  • 高德地图添加大头针和周边搜索

    1.集成高德地图: 在高德官网导入sdk,初始化地图 -(void)initMapView { ///地图需要...

  • iOS开发之百度地图

    现在的应用,几乎都含有地图功能,但是往往系统地图功能达不到需求,所以需要集成高德地图和百度地图,而大部分iOS...

  • vue中使用基础的高德地图

    这次介绍在vue里面使用基础的地图组件,用的是高德地图 先要在高德地图开发平台上注册一个应用,选JavaScrip...

  • iOS App内部调起百度地图、高德地图、腾讯地图

    公司App集成的是百度地图,然后调起百度地图、高德地图、腾讯地图、苹果地图进行导航的功能. 一.首先需要在info...

  • 百度地图web--拖拽选址

    实现地图拖拽选址功能,百度地图并未像高德地图拖拽选址功能 。由于项目需要,在百度地图的基础上实现简单的拖拽功能。大...

  • 2016.09.20日常记录

    项目修改地图功能组件,替换成高德的。高德地图库集成相对容易,库的版本管理比百度的清楚些,不会那么的混乱。 应用有A...

  • Android 高德地图 Polyline 实时绘制行动轨迹

    效果图 前言 项目需求,需要做一个绘制行动轨迹的功能,因为本身项目集成的是高德地图,所以在此处,就针对高德地图来简...

网友评论

      本文标题:高德地图集成及基础功能介绍

      本文链接:https://www.haomeiwen.com/subject/dqortxtx.html