MapKit绘制地图路线

68 篇文章 1 订阅

https://www.ioscreator.com/tutorials/draw-route-mapkit-tutorial

在本教程中,在纽约的两个着名地点之间绘制了一条路线。使用折线绘制路线作为Map顶部的叠加视图。本教程使用Xcode 7.2制作,并针对iOS 9.2构建打开Xcode并创建一个新的单视图应用程序。对于产品名称,请使用IOS9DrawRouteMapKitTutorial,然后使用您的常规值填写组织名称和组织标识符。输入Swift作为语言,并确保在“设备”中仅选择了iPhone。
image

在Project Navigator窗格中,选择Prokect Settings图标。单击Capabilities选项卡并启用Map框架。
image

转到Storyboard,选择View Controller,转到Editor菜单并选择Embed in - > Navigation Controller。双击View Controller中的导航栏,然后输入“Route Tutorial”。接下来,将Map Kit视图从对象库拖动到主视图。使用调整大小处理程序使其全屏显示。Storyboard将如下所示
image
打开Assistant Editor并确保ViewController.swift文件可见。按住Ctrl并从Map Kit视图拖动到ViewController类以创建以下Outlet。
image
选择Map Kit View并选择Interface Builder右下角的Pin图标(左起第3个按钮)。选择所有引脚方向并取消选择“约束到边距”。单击“添加4个约束”按钮。这会将Map Kit视图固定到主视图边缘的所有方向。

image

选择Assistant Editor并确保ViewController.swift文件可见。选择Map Kit视图。按住Ctrl并从Map Kit视图拖动到ViewController类以创建以下Outle打开

ViewController.swift 文件,添加 MapKit 头文件

import MapKit

ViewController需要实现MKMapViewDelegate协议才能绘制路径。将类声明行更改为

class ViewController: UIViewController, MKMapViewDelegate {

下一步,更改 viewDidLoad 如下

override func viewDidLoad() {
        super.viewDidLoad()
        
        // 1.
        mapView.delegate = self
        
        // 2.
        let sourceLocation = CLLocationCoordinate2D(latitude: 40.759011, longitude: -73.984472)
        let destinationLocation = CLLocationCoordinate2D(latitude: 40.748441, longitude: -73.985564)
        
        // 3.
        let sourcePlacemark = MKPlacemark(coordinate: sourceLocation, addressDictionary: nil)
        let destinationPlacemark = MKPlacemark(coordinate: destinationLocation, addressDictionary: nil)
        
        // 4.
        let sourceMapItem = MKMapItem(placemark: sourcePlacemark)
        let destinationMapItem = MKMapItem(placemark: destinationPlacemark)
        
        // 5.
        let sourceAnnotation = MKPointAnnotation()
        sourceAnnotation.title = "Times Square"
        
        if let location = sourcePlacemark.location {
            sourceAnnotation.coordinate = location.coordinate
        }
        
        
        let destinationAnnotation = MKPointAnnotation()
        destinationAnnotation.title = "Empire State Building"
        
        if let location = destinationPlacemark.location {
            destinationAnnotation.coordinate = location.coordinate
        }
        
        // 6.
        self.mapView.showAnnotations([sourceAnnotation,destinationAnnotation], animated: true )
        
        // 7.
        let directionRequest = MKDirectionsRequest()
        directionRequest.source = sourceMapItem
        directionRequest.destination = destinationMapItem
        directionRequest.transportType = .Automobile
        
        // Calculate the direction
        let directions = MKDirections(request: directionRequest)
        
        // 8.
        directions.calculateDirectionsWithCompletionHandler {
            (response, error) -> Void in
            
            guard let response = response else {
                if let error = error {
                    print("Error: \(error)")
                }
                
                return
            }
            
            let route = response.routes[0]
            self.mapView.addOverlay((route.polyline), level: MKOverlayLevel.AboveRoads)
            
            let rect = route.polyline.boundingMapRect
            self.mapView.setRegion(MKCoordinateRegionForMapRect(rect), animated: true)
        }
    }

The ViewController is the delegate of the MKMapViewDelegate protocol
Set the latitude and longtitude of the locations
Create placemark objects containing the location’s coordinates
MKMapitems are used for routing. This class encapsulates information about a specific point on the map
Annotations are added which shows the name of the placemarks
The annotations are displayed on the map
The MKDirectionsRequest class is used to compute the route.
The route will be drawn using a polyline as a overlay view on top of the map. The region is set so both locations will be visible

  1. 在ViewController实现MKMapViewDelegate协议的委托
  2. 设置位置的纬度和经度
  3. 创建包含位置坐标的地标对象
  4. MKMapitems用于路线的绘制。 此类封装有关地图上特定点的信息
  5. 添加注释,显示地标的名称
  6. 注释显示在地图上
  7. MKDirectionsRequest类用于计算路线。
  8. 将使用折线作为地图顶部的叠加视图绘制路线。区域设置为两个位置都可见

下一步实现mapView(rendererForrOverlay:)代理方法

func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer {
        let renderer = MKPolylineRenderer(overlay: overlay)
        renderer.strokeColor = UIColor.redColor()
        renderer.lineWidth = 4.0
    
        return renderer
    }

此方法返回将用于在地图上绘制路线的渲染器对象。使用红色,线宽为4。
构建并运行项目,设备将显示位置,包括它们之间的绘制路径。
image

项目用到,找到这篇文章,随便翻译一下。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值