【发布时间】:2020-01-22 17:20:57
【问题描述】:
我要这样声明一个观点
makeAnnotation(givenTitle: "Start", pointCoords: userCoords)
turnLocations.append(userCoords)
let region = MKCoordinateRegion(center: userCoords, latitudinalMeters: 100, longitudinalMeters: 100)
mapView.setRegion(region, animated: true)
userCoords 设置正确,并且标题“开始”出现在地图上,没有注释图钉视觉,尽管它确实随着区域正确缩放到它。当我计算这个“开始”和我的其他注释之间的距离并将它们加在一起时,它可以正常工作,甚至重新调整起始路线。但是,当我制作这样的折线时:(并正确渲染)
let polyline = MKPolyline(coordinates: turnLocations, count: turnLocations.count) //makes polyLine w/ coords given
mapView.addOverlay(polyline)
它只会在turnLocations 数组中的每个其他点之间创建一条折线,或者像这样奇怪地连接它(它应该连接到 turn1)
编辑:因为我被问到,这里是如何添加其他坐标的 turnLocations:
let coords = mapView.convert(tapLocation,toCoordinateFrom: mapView)
print("Tapped at lat: \(coords.latitude) long: \(coords.longitude)")
makeAnnotation(givenTitle: "Turn\(annotations.count)", pointCoords: coords)
turnLocations.append(coords)
它在顶部是这样定义的:
var turnLocations: [CLLocationCoordinate2D] = []
并用
清除turnLocations = []
每当用户重置应用程序上的坐标时(使用重置按钮) 只要没有注释,就会添加开始按钮
if(turnLocations.count < 1){ //if start isn't there already, it creates it
makeAnnotation(givenTitle: "Start", pointCoords: userCoords)
turnLocations.append(userCoords)
let region = MKCoordinateRegion(center: userCoords, latitudinalMeters: 100, longitudinalMeters: 100)
mapView.setRegion(region, animated: true)
}
一旦 userLocation 被更新(使用内置的位置管理器方法),userCoords 就会被定义
userCoords = mapView.userLocation.coordinate
【问题讨论】:
-
我建议向我们展示
turnLocations是如何构建的。或者也许打印出turnLocations的坐标并确认它是您认为的那样。MKPolyline有一些奇怪的行为,但根据到目前为止共享的内容,我们不知道是不是这样,或者更有可能只是turnLocations的人口有一些问题。我们需要bare minimum amount of code to reproduce the problem,但无需深入了解代码的血腥细节。所以与我们分享turnLocations。 -
@Rob 我尝试按照您的要求进行操作,这是否为您提供了足够的信息?还是太多了?
-
@Rob 对此感到抱歉,我不知道 Xcode 标记,我将在折线函数中打印出 mapView.overlays 和 turnLocations 并在可能的情况下对其进行编辑。 (正在上课)
-
使用带有如下注释的地图:imgur.com/a/MqI5Ack>,mapView.overlays 打印
[<MKPolyline: 0x600000d540a0>],打印位置为[__C.CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0), __C.CLLocationCoordinate2D(latitude: -1.197754045278998e-06, longitude: -0.00022158450201459345), __C.CLLocationCoordinate2D(latitude: 0.00030902054875525664, longitude: -0.0002335620426663354), __C.CLLocationCoordinate2D(latitude: 0.00019164065039944944, longitude: 0.00014013722557137953)]
标签: swift xcode annotations mapkit