博客> ios-Swift版高德地图的基本功能
ios-Swift版高德地图的基本功能
2019-10-21 00:32 评论:0 阅读:1015 可可可可丶可乐
ios swift 高德地图

1.首先代理不能少

   MAMapViewDelegate ,AMapSearchDelegate,AMapNaviWalkManagerDelegate 

2.地图-检索-定位

  //  地图
    mapView = MAMapView(frame:view.bounds)
    view.addSubview(mapView)
    mapView.delegate = self
    mapView.zoomLevel = 17
    mapView.showsUserLocation = true;
    mapView.userTrackingMode = .follow;

    //  检索
    search = AMapSearchAPI()
    search.delegate = self
    walkingManger = AMapNaviWalkManager()
    walkingManger.delegate = self

    self.locationBtn.frame =  CGRect(x:10, y:150, width:100, height:30)
    self.locationBtn.setTitle("定位", for:.normal)
    self.locationBtn.backgroundColor = UIColor.red
    self.view.addSubview(self.locationBtn)
    view.bringSubview(toFront: self.locationBtn)

3.搜索周边

func onPOISearchDone(_ request: AMapPOISearchBaseRequest!, response: AMapPOISearchResponse!) {

    /*
     if response.count == 0 {
     return
     }
     */
    guard response.count > 0 else {
        print("周边没有老师")
        return
    }
    /*
     //解析response获取POI信息,具体解析见 Demo  dump 可以把一个里面的属性可以看出来 print 打印
     for poi  in response.pois {
     print(poi.name)
     }
     */
    var annotions : [MAPointAnnotation] = []

    annotions = response.pois.map{
        let annotion = MAPointAnnotation()
        annotion.coordinate = CLLocationCoordinate2D(latitude: CLLocationDegrees($0.location.latitude), longitude: CLLocationDegrees($0.location.longitude))
        if $0.distance < 400 annotion.title = "coco老师" annotion.subtitle = "奥数天王" annotion.title =  "正常接课" xss=removed xss=removed xss=removed xss=removed timeDesc  = "一分钟以内"> 0 {
        timeDesc = walkMinte.description + "分钟"
    }

    let hintTitle = "步行" + timeDesc
    let hintSubtile = "距离" + walkingManger.naviRoute!.routeLength.description + "米"

    FTIndicator.setIndicatorStyle(.dark)
    FTIndicator.showNotification(with: UIImage(named:"StudentTeacher"), title: hintTitle, message: hintSubtile)

}

5.自定义大头针

func mapView(_ mapView: MAMapView!, viewFor annotation: MAAnnotation!) -> MAAnnotationView! {
    if annotation is MyPinannotion{
        let reuseid = "anchor"
        var av = mapView.dequeueReusableAnnotationView(withIdentifier: reuseid)
        if av == nil {
            av = MAPinAnnotationView(annotation: annotation, reuseIdentifier: reuseid)
        }
        av?.image =  UIImage(named:"StudentTeacher")
        av?.canShowCallout = false
        pinView = av as! MAPinAnnotationView!
        return av

    }
    if annotation.isKind(of: MAPointAnnotation.self) {
        let pointReuseIndetifier = "pointReuseIndetifier"
        var annotationView: MAPinAnnotationView? = mapView.dequeueReusableAnnotationView(withIdentifier: pointReuseIndetifier) as! MAPinAnnotationView?

        if annotationView == nil {
            annotationView = MAPinAnnotationView(annotation: annotation, reuseIdentifier: pointReuseIndetifier)
        }

        if annotation.title == "正常可用"{
            annotationView?.image = UIImage(named:"StudentTeacher")
        }else{
            annotationView?.image = UIImage(named:"StudentTeacher_s")
        }
        annotationView!.canShowCallout = true
        annotationView!.animatesDrop = true
        annotationView!.isDraggable = true

        return annotationView!
    }

    return nil
}

func mapView(_ mapView: MAMapView!, didSelect view: MAAnnotationView!) {
    print("点击了我")
    start = pin.coordinate
    end = view.annotation.coordinate
    let startPoint = AMapNaviPoint.location(withLatitude: CGFloat(start.latitude) , longitude:  CGFloat (start.longitude))!
    let endPoint = AMapNaviPoint.location(withLatitude: CGFloat(end.latitude), longitude: CGFloat(end.longitude))!
    walkingManger.calculateWalkRoute(withStart: [startPoint], end: [endPoint])

}

6.绘制路线

func mapView(_ mapView: MAMapView!, rendererFor overlay: MAOverlay!) -> MAOverlayRenderer! {
    if overlay.isKind(of: MAPolyline.self) {

        pin.isLockedToScreen = false

        mapView.visibleMapRect = overlay.boundingMapRect

        let renderer: MAPolylineRenderer = MAPolylineRenderer(overlay: overlay)
        renderer.lineWidth = 8.0
        renderer.strokeColor = UIColor.blue

        return renderer
    }

    return nil
}

好了,以上就是swift版简单的高德地图的功能使用,当然还有很多别的功能这里不一一列举了!共同学习共同进步!

收藏
1
sina weixin mail 回到顶部