1
A
回答
0
我嘗試一些東西,會顯示兩個註釋,但不是當你輕按MKPolylineOverlay之間的距離。還有一件重要的事情我沒有保持任何標準。
這是我的控制器結構。
import UIKit
import MapKit
class RouteViewController: UIViewController, MKMapViewDelegate {
@IBOutlet weak var mapView: MKMapView!
//Rest of the code see below
}
首先我會添加一些註釋,如下圖所示viewDidLoad中委託方法映射。
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方法如下,
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)
}
Next我正在實施rendererForOve rlay如下委託方法,
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
}
接着是一個重要的一個委託方法是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
}
相關問題
- 1. 如何在谷歌地圖上清除顯示的路線?
- 2. 谷歌地圖javascript:在半地圖上顯示路線
- 3. 在谷歌地圖上顯示多個路線
- 4. 在谷歌地圖上顯示多條路線
- 5. 如何在地圖上顯示路線
- 6. 如何不同顏色顯示谷歌地圖的路線上道路優先
- 7. 谷歌地圖不顯示路線
- 8. 谷歌地圖顯示路線
- 9. 如何在谷歌地圖中直觀地顯示路線
- 10. 如何顯示谷歌地圖上的路徑和距離 - Xamarin
- 11. 顯示在谷歌地圖上的Android
- 12. 在wen上使用谷歌地圖顯示巴士路線上的車站
- 13. 谷歌地圖上繪製路線
- 14. 如何在谷歌地圖上顯示完整路線黑白兩點?
- 15. 如何離線顯示谷歌地圖?
- 16. 在谷歌地圖上顯示英國
- 17. 在谷歌地圖上顯示風向
- 18. 在谷歌地圖上顯示文本
- 19. 谷歌地圖不顯示在Firefox上
- 20. 如何在ios上的谷歌地圖上顯示特定路線的實時交通數據
- 21. 如何在Android上的touchevent上的谷歌地圖上畫線?
- 22. 谷歌折線圖時間顯示
- 23. 谷歌地圖:指定路線上兩點之間的距離
- 24. 谷歌地圖上兩個位置之間的Android路線
- 25. 谷歌地圖/路線的3D表示?
- 26. 如何使用Swift打開谷歌地圖以顯示路線
- 27. 谷歌地圖V3在谷歌地圖上顯示地圖鏈接
- 28. 在走的路線谷歌地圖上繪製折線android
- 29. 谷歌地圖的實時路線和顯示路線的歷史
- 30. 在json數據谷歌地圖上顯示路徑
附加的圖像,我需要顯示與附加圖像相同的MKPolyline上的時間彈出窗口。你能有任何想法嗎? –
您是否嘗試過在特定位置添加其他註釋? – Wain
@感謝您的回覆。是的,我嘗試添加另一個註釋。但是做完這些之後,viewForAnnotation會調用註釋(第一組註釋和中點註釋)並給出意想不到的結果。 –