地图绘制路线

前面我介绍了如何调起第三方地图导航,那么我今天再介绍一下如何使用系统MapKit在地图上绘制路线,这个功能可能在地图上不经常使用,但是还是有必要和打家分享一下,希望可以帮助有需要的开发者!(swift)

看代码:

import UIKit
import MapKit

class MapVC: UIViewController,MKMapViewDelegate,CLLocationManagerDelegate {

    @IBOutlet var mapView: MKMapView!
    var geocoder : CLGeocoder = CLGeocoder()//编码器
    var startPlacemark : CLPlacemark!//起始位置信息
    var endPlacemark : CLPlacemark!//终点位置信息

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.mapView.delegate = self
        self.mapView.zoomLevel = 15
        self.setSpecialRegion(22.648838, longitude: 114.026178)

    }
    /**  显示特定区域  */
    func setSpecialRegion(latitude:CLLocationDegrees, longitude:CLLocationDegrees) {
        let coordinate = CLLocationCoordinate2DMake(latitude, longitude)
        let span = MKCoordinateSpanMake(0.01, 0.01)
        let region =  MKCoordinateRegionMake(coordinate, span)
        self.mapView.setRegion(region, animated: true)


    }
    /**  MKMapViewDelegate  */
    func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer {

        let gradientColors = [UIColor.redColor()]
        let polylineRenderer = JLTGradientPathRenderer(polyline: overlay as! MKPolyline, colors: gradientColors)
        polylineRenderer.lineWidth = 4
        return polylineRenderer
    }
    /**  根据位置名称编码获取位置信息  */
    func getStartPlacemark(latitude:CLLocationDegrees, longtitude:CLLocationDegrees) {
        let location = CLLocation.init(latitude: latitude, longitude: longtitude)
        CLGeocoder().reverseGeocodeLocation(location) { (placemarks, error) in
            if (error != nil || placemarks!.count == 0) {
                print("对不起,位置信息错误")
            }else{
                self.startPlacemark = placemarks?.first
                self.mapView.addAnnotation(self.setAnnotationView((self.startPlacemark.location?.coordinate.latitude)!, longtitude: (self.startPlacemark.location?.coordinate.longitude)!, text: self.startPlacemark.name!))

            }
        }
    }
    func getEndPlacemark(latitude:CLLocationDegrees, longtitude:CLLocationDegrees) {
        let location = CLLocation.init(latitude: latitude, longitude: longtitude)
        CLGeocoder().reverseGeocodeLocation(location) { (placemarks, error) in
            if (error != nil || placemarks!.count == 0) {
                print("对不起,位置信息错误")
            }else{
                self.endPlacemark = placemarks?.first
                self.mapView.addAnnotation(self.setAnnotationView((self.endPlacemark.location?.coordinate.latitude)!, longtitude: (self.endPlacemark.location?.coordinate.longitude)!, text: self.endPlacemark.name!))
            }
        }
    }
    ///设置大头针
    func setAnnotationView(latitude:CLLocationDegrees,longtitude:CLLocationDegrees,text:String) -> MKPointAnnotation{
        let coordinate = CLLocationCoordinate2DMake(latitude, longtitude)
        let annotation = MKPointAnnotation()
        annotation.coordinate = coordinate
        annotation.title = text
        return annotation
    }

    @IBAction func action(sender: UIButton) {
        getStartPlacemark(22.648838, longtitude: 114.026178)
        getEndPlacemark(22.658838, longtitude: 114.036178)

    }
    /**  画线方法  */
    func drawLine() {
        let request = MKDirectionsRequest()
        let startMKP = MKPlacemark.init(placemark: startPlacemark)
        request.source = MKMapItem.init(placemark: startMKP)
        let endMKP = MKPlacemark.init(placemark: endPlacemark)
        request.destination = MKMapItem.init(placemark: endMKP)
        let directions = MKDirections.init(request: request)
        directions.calculateDirectionsWithCompletionHandler { (response, error) in
            if error == nil {
                //计算正确
                for route:MKRoute in (response?.routes)! {
                    self.mapView.addOverlay(route.polyline)
                }
            }else{
                print("计算路线错误")
            }
        }




    }
    @IBAction func drawLineAction(sender: UIButton) {
        if self.startPlacemark != nil && self.endPlacemark != nil {
            drawLine()
        }
    }

效果图:
这里写图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值