1
A
答えて
0
は、私はあなたがMKPolylineOverlayをタップしたときに2つの注釈の間の距離を表示しませんが、何かを試してみました。もう一つ重要なことは、私は基準を維持していないことです。
ここは私のコントローラの構造です。
import UIKit
import MapKit
class RouteViewController: UIViewController, MKMapViewDelegate {
@IBOutlet weak var mapView: MKMapView!
//Rest of the code see below
}
は、まず第一に、私は以下のようにのviewDidLoadデリゲートメソッドにマップするためのいくつかの注釈を追加します。 directionHandlerMethodで
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)
}
}
}
、Iは
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])
}
}
、以下のように方向に対する実際の要求を行っており次にIは、以下のように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)
}
次は、レンダリング用のものを実装しています以下のように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. 飛行経路をGoogleマップで表示
- 2. GoogleマップGDirections - マップ上の2つのポイント間の経路を指示する
- 3. Googleマップとgoogle streetviewの両方を表示する経路をアニメ化する
- 4. Googleマップ上で飛行経路をプロトタイプ化する
- 5. 緯度/経度をデータセットに統合してマップ上に表示する方法
- 6. Googleマップapiの指示サービスは、複数のマーカー間の経路を表示しているときにOVER_QUERY_LIMITを表示します。javascript
- 7. Rails - 経路は経路に表示されていますが、経路上には経路が表示されません
- 8. Googleマップ上で、ソース、目的地、開始時間、および(開始時間+時間)を入力としてGoogleマップのユーザー位置を取得する方法
- 9. Google Maps API - 経路/ポリラインの表示、kmlLayerの代替方法
- 10. DijkstraアルゴリズムをGoogleマップに適用して、2点間の最短経路を見つける方法は?
- 11. Google Transit APIバスは経路非表示と地図上で停止する
- 12. 地図上に所定の経路を表示する方法は?
- 13. Googleマップを使用して道路全体をハイライト表示
- 14. Google KMLを使用してウェブベースのGoogleマップに経路/線を描画する
- 15. Android、Googleマップの経路を描画
- 16. パス() - マップ(グーグルマップおよびOpenStreetマップ)上に経路を表示するアプリケーションを開発するスマート有色セグメント
- 17. 緯度と経度を入力してAjax Googleマップを表示
- 18. 時間の経過後にイメージを表示する方法は?
- 19. Googleマップのポリラインを使用して2点間の経路を描く
- 20. GoogleマップJavascript API v3経路の反復
- 21. GoogleマップクライアントのJava APIを使用してGoogleマップの経路を最適化する方法。
- 22. Googleマップからドラッグした経路データを取得する
- 23. Google Maps API - ズームアウト時にすべてのローカル道路を表示
- 24. Googleマップでウェイポイントを経由して合計時間と合計距離を取得する方法
- 25. Googleマップapiとlaravelを使用してマップ上にgpxファイルを表示
- 26. 異なる色Googleマップでルート上の道路の優先順位を表示する方法異なる色Googleマップでルート上の道路の優先順位を表示する方法
- 27. テキストビューで経過時間を表示する方法1時間30分
- 28. Swift iOS-ラベルを非表示にして一定時間が経過した後に表示する方法
- 29. 経路をプログラムで表示する方法は?
- 30. アンドロイドの経路を表示
添付画像、添付画像と同じMKPolylineに時間のポップオーバーを表示する必要があります。あなたは誰にも考えがありますか? –
別の注釈を特定の場所に追加しようとしましたか? – Wain
@Wain返信ありがとうございます。はい、別の注釈を追加してみました。この後、viewForAnnotationは注釈(注釈と中点注釈の最初のセット)と予期しない結果を両方とも呼び出すことになります。 –