Ios 用swift 4在地图上绘制路线
我要在swift4中的两个坐标之间绘制路线。 我用的是这个代码Ios 用swift 4在地图上绘制路线,ios,swift3,routes,swift4,mapkit,Ios,Swift3,Routes,Swift4,Mapkit,我要在swift4中的两个坐标之间绘制路线。 我用的是这个代码 import UIKit import MapKit class ViewController: UIViewController, MKMapViewDelegate { @IBOutlet weak var myMap: MKMapView! var myRoute : MKRoute! override func viewDidLoad() { super.viewDidLoad() le
import UIKit
import MapKit
class ViewController: UIViewController, MKMapViewDelegate {
@IBOutlet weak var myMap: MKMapView!
var myRoute : MKRoute!
override func viewDidLoad() {
super.viewDidLoad()
let point1 = MKPointAnnotation()
let point2 = MKPointAnnotation()
point1.coordinate = CLLocationCoordinate2DMake(25.0305, 121.5360)
point1.title = "Taipei"
point1.subtitle = "Taiwan"
myMap.addAnnotation(point1)
point2.coordinate = CLLocationCoordinate2DMake(24.9511, 121.2358)
point2.title = "Chungli"
point2.subtitle = "Taiwan"
myMap.addAnnotation(point2)
myMap.centerCoordinate = point2.coordinate
myMap.delegate = self
//Span of the map
myMap.setRegion(MKCoordinateRegionMake(point2.coordinate, MKCoordinateSpanMake(0.7,0.7)), animated: true)
let directionsRequest = MKDirectionsRequest()
let markTaipei = MKPlacemark(coordinate: CLLocationCoordinate2DMake(point1.coordinate.latitude, point1.coordinate.longitude), addressDictionary: nil)
let markChungli = MKPlacemark(coordinate: CLLocationCoordinate2DMake(point2.coordinate.latitude, point2.coordinate.longitude), addressDictionary: nil)
directionsRequest.source = MKMapItem(placemark: markChungli)
directionsRequest.destination = MKMapItem(placemark: markTaipei)
directionsRequest.transportType = MKDirectionsTransportType.automobile
let directions = MKDirections(request: directionsRequest)
directions.calculate(completionHandler: {
response, error in
if error == nil {
self.myRoute = response!.routes[0] as MKRoute
self.myMap.add(self.myRoute.polyline)
}
})
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) ->MKOverlayRenderer {
let myLineRenderer = MKPolylineRenderer(polyline: myRoute.polyline)
myLineRenderer.strokeColor = UIColor.red
myLineRenderer.lineWidth = 3
return myLineRenderer
}
}
这个密码给了我正确的答案
但当我改变坐标时,它不会显示路线。
新的坐标是
点1=26.9124,75.7873
point2=26.9124,76.7873看起来苹果地图还不支持印度。(我想如果这是你的应用程序的一个关键部分,你可能不得不考虑谷歌地图
这些新坐标在印度,看起来那里没有方向。我得到一个错误,上面写着“error Domain=MKErrorDomain Code=4”Directions Not Available“UserInfo={NSLocalizedDescription=Directions Not Available,mkerrorgeorgeorer=-8,mkerrorgeoruserinfo={},mkerrorgeortransitionincidentkey=,MKDirectionsErrorCode=0,NSLocalizedFailureReason=这些位置之间的方向不可用。”
为了这场战争,塔克斯·罗布 看起来苹果地图还不支持印度。(我想如果这是你的应用程序的一个关键部分,你可能不得不考虑谷歌地图
这些新坐标在印度,看起来那里没有方向。我得到一个错误,上面写着“error Domain=MKErrorDomain Code=4”Directions Not Available“UserInfo={NSLocalizedDescription=Directions Not Available,mkerrorgeorgeorer=-8,mkerrorgeoruserinfo={},mkerrorgeortransitionincidentkey=,MKDirectionsErrorCode=0,NSLocalizedFailureReason=这些位置之间的方向不可用。”
为了这场战争,塔克斯·罗布 现在,如果
calculate
无法获得路由,那么您就看不到错误对象是什么。如果您想知道为什么没有获得路由,我建议检查一下错误是什么。表达式生成的错误:错误:/var/folders/g5/747t2c710z36d6rc4w22ds_m0000gn/T//lldb/1003/expr12。swift:1:65:错误:使用未声明的类型“Builtin”swift.\u DebuggerSupport.stringForPrintObject(swift.UnsafePointer(位模式:0x121bba200)!.pointee)不相关,但您的渲染器正在引用myRoute.polyline
。你不应该那样做。它只会让你面临问题。您应该获取传递到此代理消息的覆盖
,并从中创建渲染器。例如,let renderer=MKPolylineRenderer(overlay:overlay)
。这些新坐标位于印度,看起来那里没有方向。我得到一个错误,上面写着“error Domain=MKErrorDomain Code=4”Directions Not Available“UserInfo={NSLocalizedDescription=Directions Not Available,mkerrorgeorgeorer=-8,mkerrorgeoruserinfo={},mkerrorgeortransitionincidentkey=,MKDirectionsErrorCode=0,NSLocalizedFailureReason=这些位置之间的方向不可用。”看起来苹果地图还不支持印度。(我想如果这是你的应用程序的一个关键部分,你可能不得不考虑谷歌地图。现在,如果<代码>计算<代码>无法获得一条路径,你就不会查看错误对象是什么。如果您想知道为什么没有获得路由,我建议检查一下错误是什么。表达式生成的错误:错误:/var/folders/g5/747t2c710z36d6rc4w22ds_m0000gn/T//lldb/1003/expr12。swift:1:65:错误:使用未声明的类型“Builtin”swift.\u DebuggerSupport.stringForPrintObject(swift.UnsafePointer(位模式:0x121bba200)!.pointee)不相关,但您的渲染器正在引用myRoute.polyline
。你不应该那样做。它只会让你面临问题。您应该获取传递到此代理消息的覆盖
,并从中创建渲染器。例如,let renderer=MKPolylineRenderer(overlay:overlay)
。这些新坐标位于印度,看起来那里没有方向。我得到一个错误,上面写着“error Domain=MKErrorDomain Code=4”Directions Not Available“UserInfo={NSLocalizedDescription=Directions Not Available,mkerrorgeorgeorer=-8,mkerrorgeoruserinfo={},mkerrorgeortransitionincidentkey=,MKDirectionsErrorCode=0,NSLocalizedFailureReason=这些位置之间的方向不可用。”看起来苹果地图还不支持印度。(我想如果这是你的应用程序的一个关键部分,你可能不得不考虑谷歌地图。