【问题标题】:Polyline not connecting to first point折线未连接到第一个点
【发布时间】: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 打印[&lt;MKPolyline: 0x600000d540a0&gt;],打印位置为[__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)]
  • 这很好reproducible example。

标签: swift xcode annotations mapkit


【解决方案1】:

你说:

打印的位置是[__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)]

折线只是被纬度和经度为 0, 0 的第一个数据点弄糊涂了。

考虑:

let coordinates = [
    CLLocationCoordinate2D(latitude: 0, longitude: 0),
    CLLocationCoordinate2D(latitude: -1.197754045278998e-06, longitude: -0.00022158450201459345),
    CLLocationCoordinate2D(latitude: 0.00030902054875525664, longitude: -0.0002335620426663354),
    CLLocationCoordinate2D(latitude: 0.00019164065039944944, longitude: 0.00014013722557137953)
]

let polyline = MKPolyline(coordinates: coordinates, count: coordinates.count)
mapView.addOverlay(polyline)
mapView.setVisibleMapRect(polyline.boundingMapRect, animated: true)

产生:

如果您将第一个更改为其他内容,例如CLLocationCoordinate2D(latitude: 0.0000001, longitude: 0.0000001),然后就可以正常使用了:

显然MKPolylineRenderer 只是被 0, 0 坐标混淆了。但是,如果您使用最终用户可能处于的实际、真实世界坐标,那么它就可以正常工作。

坦率地说,这些数据点看起来都不完全正确。您通常会用 -180 到 180 之间的值表示纬度和经度,但在非洲海岸附近的海洋中,它们本身都在零附近。

因此,如果您担心地图视图无法正确处理退化的 0、0 场景,请随时 post a bug report。如果您认为您将在您的应用程序中遇到 0、0 和相关场景,我建议您添加一个针对 0 纬度和/或经度值的测试并进行相应处理(例如,使用一个微小的非零值) .

【讨论】:

  • 所以,据我所知,根据您所说,点坐标上的 0,0 会导致 mapkit 出现问题?这些数据点看起来错误的原因是因为我没有在模拟器中设置我的欺骗位置,并且“开始”是基于使用的位置,因此将其设置为 0,0 默认值。
  • 这两点都是。对于位置信息,在物理设备上进行测试几乎总是更好。模拟器做得不错,但与它在 iOS 设备上的工作方式不同。
猜你喜欢
  • 1970-01-01
  • 2020-08-05
  • 1970-01-01
  • 2018-04-29
  • 1970-01-01
  • 1970-01-01
  • 2015-04-01
  • 2021-08-22
  • 1970-01-01
相关资源
最近更新 更多