位置 A から位置 B (現在のユーザーの GPS 位置) への MapView のルートにポリラインを追加しようとしています。したがって、ルート/ポリラインは、設定された位置 A からユーザーが現在いる場所までたどることができます。その場で(場所B)で。
現在、オーバーレイが機能していません。MKPolylineViewで他のいくつかの SO スレッドを見ましたが、それらのコード (以下にもあります) を実装しようとすると、ルート/ラインはまだ表示されません。私は iOS が初めてなので、まだ Swift/mapKit に慣れています。
これが私がこれまでに持っているものです:(重要な部分を示すために変更されています)
class ViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var setLocButton: UIButton!
@IBOutlet weak var mapView: MKMapView!
var locationManager: CLLocationManager = CLLocationManager()
var carLat = 36.136111, carLong = -80.279462
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [CLLocation]!) { //AnyObject->CLLocation
var latestLocation: CLLocation = locations[locations.count - 1]
//Displaying location A on mapView
let carCoords = CLLocationCoordinate2D(latitude: carLat, longitude: carLong)
let region = MKCoordinateRegion(center: carCoords, span: MKCoordinateSpan(latitudeDelta: 0.0035, longitudeDelta: 0.0035))
mapView.mapType = MKMapType.Hybrid
mapView.setRegion(region, animated: true)
//Attempting to display route information on mapView
mapView.showsUserLocation = true
var locations = [CLLocation(latitude: carLat, longitude: carLong), CLLocation(latitude: latestLocation.coordinate.latitude, longitude: latestLocation.coordinate.latitude)]
var coordinates = locations.map({(location: CLLocation) -> CLLocationCoordinate2D in return location.coordinate})
var polyline = MKPolyline(coordinates: &coordinates, count: locations.count)
self.mapView.addOverlay(polyline)
}
//rendererForOverlay
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
if overlay is MKPolyline {
/* Does not get called */
print("rendererForOverlay")
var polylineRenderer = MKPolylineRenderer(overlay: overlay)
polylineRenderer.strokeColor = UIColor.blueColor()
polylineRenderer.lineWidth = 5
return polylineRenderer
}
return nil
}
MKDirections を使用した別の例も見つけました。これは、トランスポート タイプ (MKDirectionsTransportType.Walking) を設定できるため、より理想的です。私もこれらの指示でルートを描くのに問題があります。
2 番目の一連の手順を使用して、Xcode が警告したいくつかのエラーを解決した後に得たものを次に示します。
var route: MKRoute?
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [CLLocation]!) { //AnyObject->CLLocation
var latestLocation: CLLocation = locations[locations.count - 1] //AnyObject
/*
...
*/
//carLocation = Location A; currentLocation = location B
var directionsRequest = MKDirectionsRequest()
let carLocation = MKPlacemark(coordinate: carCoords, addressDictionary: nil)
var currentLocation = MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: coorLat, longitude: coorLong), addressDictionary: nil)
directionsRequest.source = MKMapItem(placemark: currentLocation)
directionsRequest.destination = MKMapItem(placemark: carLocation)
directionsRequest.transportType = MKDirectionsTransportType.Walking
var directions = MKDirections(request: directionsRequest)
directions.calculateDirectionsWithCompletionHandler {
(response, error) -> Void in
if error == nil {
self.route = response!.routes[0] as? MKRoute
self.mapView.addOverlay((self.route?.polyline)!)
}
}
}
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
/* Does not get executed as well */
print("RendererForOverLay")
var myLineRenderer = MKPolylineRenderer(polyline: (route?.polyline)!)
myLineRenderer.strokeColor = UIColor.redColor()
myLineRenderer.lineWidth = 3
return myLineRenderer
}
rendererForOverlay が両方のインスタンスで呼び出されていないため、何らかの方法で間違ってリンクしていますか?