ios - 如何将路线时间显示为谷歌地图

标签 ios mkmapview mkannotation mkpolyline

enter image description here我创建了带有多个注释的路线。 我想在注释之间显示与所附屏幕截图完全相同的文本。

有人可以帮忙吗?

谢谢

最佳答案

我尝试了一些会显示两个注释之间的距离的东西,但当您点击 MKPolylineOverlay 时不会。另一件重要的事情是我没有维护任何标准。

这是我的 Controller 结构。

import UIKit
import MapKit

class RouteViewController: UIViewController, MKMapViewDelegate {

    @IBOutlet weak var mapView: MKMapView!

    //Rest of the code see below
}

首先,我将在 viewDidLoad 委托(delegate)方法中添加一些注释以映射,如下所示。

override func viewDidLoad() {
    super.viewDidLoad()
    self.mapView.delegate = self

    let annotation1 = MKPointAnnotation()
    annotation1.title = "Times Square"
    annotation1.coordinate =  CLLocationCoordinate2D(latitude: 40.759011, longitude: -73.984472)

    let annotation2 = MKPointAnnotation()
    annotation2.title = "Empire State Building"
    annotation2.coordinate =  CLLocationCoordinate2D(latitude: 40.748441, longitude: -73.985564)

    let annotation3 = MKPointAnnotation()
    annotation3.title = "Some Point"
    annotation3.coordinate =  CLLocationCoordinate2D(latitude: 40.7484, longitude: -73.97)

    let arrayOfPoints = [ annotation1, annotation2, annotation3]
    self.mapView.addAnnotations(arrayOfPoints)
    self.mapView.centerCoordinate = annotation2.coordinate

    for (index, annotation) in arrayOfPoints.enumerate() {
        if index < (arrayOfPoints.count-1) {
            //I am taking the two consecutive annotation and performing the routing operation.
            self.directionHandlerMethod(annotation.coordinate, ePoint: arrayOfPoints[index+1].coordinate)
        }
    }
}

directionHandlerMethod 中,我正在执行如下所示的实际方向请求,

func directionHandlerMethod(sPoint: CLLocationCoordinate2D, ePoint: CLLocationCoordinate2D) {
    let sourcePlacemark = MKPlacemark(coordinate: sPoint, addressDictionary: nil)
    let destinationPlacemark = MKPlacemark(coordinate: ePoint, addressDictionary: nil)
    let sourceMapItem = MKMapItem(placemark: sourcePlacemark)
    let destinationMapItem = MKMapItem(placemark: destinationPlacemark)

    let directionRequest = MKDirectionsRequest()
    directionRequest.source = sourceMapItem
    directionRequest.destination = destinationMapItem
    directionRequest.transportType = .Automobile
    let directions = MKDirections(request: directionRequest)
    directions.calculateDirectionsWithCompletionHandler {
        (response, error) -> Void in
        guard let response = response else {
            if let error = error {
                print("Error: \(error)")
            }
            return
        }
        //I am assuming that it will contain one and only one result so I am taking that one passing to addRoute method
        self.addRoute(response.routes[0])
    }
}

接下来我在 addRoute 方法中添加 map 上的折线路线,如下所示,

func addRoute(route: MKRoute) {
    let polyline = route.polyline

    //Here I am taking the centre point on the polyline and placing an annotation by giving the title as 'Route' and the distance in the subtitle
    let annoatation = MKPointAnnotation()
    annoatation.coordinate = MKCoordinateForMapPoint(polyline.points()[polyline.pointCount/2])
    annoatation.title = "Route"
    let timeInMinute = route.expectedTravelTime / 60
    let distanceString =  String.localizedStringWithFormat("%.2f %@", timeInMinute, timeInMinute>1 ? "minutes" : "minute")
    annoatation.subtitle = distanceString
    self.mapView.addAnnotation(annoatation)

    self.mapView.addOverlay(polyline)
}

接下来我将实现 rendererForOverlay 委托(delegate)方法,如下所示,

func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer {
    let renderer = MKPolylineRenderer(overlay: overlay)
    renderer.strokeColor = UIColor.blueColor()
    renderer.lineWidth = 2
    renderer.lineCap = .Butt
    renderer.lineJoin = .Round
    return renderer
}

下一个是重要的委托(delegate)方法,即viewForAnnotation。在这里,我正在做一些事情,比如放置标签而不是下面的注释,

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation.title != nil && annotation.title!! == "Route" {
        let label = UILabel()
        label.adjustsFontSizeToFitWidth = true
        label.backgroundColor = UIColor.whiteColor()
        label.minimumScaleFactor = 0.5
        label.frame = CGRect(x: 0, y: 0, width: 100, height: 30)
        label.text = annotation.subtitle ?? ""
        let view = MKAnnotationView()
        view.addSubview(label)
        return view
    }
    return nil
}

关于ios - 如何将路线时间显示为谷歌地图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38720881/

相关文章:

ios - 使用 MKPinAnnotationView 和 MKAnnotationView 更新用户位置

ios - 如何添加多个注释?

ios - 需要在 IOS 的 mapview 上添加一个固定的叠加层

iOS - MKPlacemark 设置标题问题

ios - 如何使用 swift 3 在 iOS 上散列文件?

javascript - Cordova 2.9、3.0 没有 console.log

ios - 解析findObjectsInBackgroundWithBlock返回nil

ios - 从 mapView.annotations 访问 MKAnnotationView

swift - 反转 MapView 的颜色

objective-c - 为什么我的函数没有在我的 AppDelegate 中运行?