【问题标题】:How to Move marker exactly along Polyline?如何沿着折线精确移动标记?
【发布时间】:2017-07-30 01:22:52
【问题描述】:

Marker Movement from source to destination

显示标记移动的图像,它是一条直线,标记仅在一个方向上。 我想沿着折线移动我的标记,标记方向应该随着折线方向而改变。如何实现这一点。 我现在的代码是这样的:

类 MapScreenVC: BaseVC {

var path = GMSMutablePath()
var arrayCoordinates : [CLLocationCoordinate2D] = []
var destCoord = CLLocationCoordinate2D()
var marker = GMSMarker()
var mapView : GMSMapView? = nil
override func viewDidLoad() {
    super.viewDidLoad()

    let camera = GMSCameraPosition.camera(withLatitude: 53.4545, longitude: -2.1811, zoom: 14)
    mapView = GMSMapView.map(withFrame: .zero, camera: camera)
    marker = GMSMarker()
    marker.position = CLLocationCoordinate2DMake(53.4387, -2.1827)
    marker.title = "Source"
    marker.snippet = "Source"
    marker.icon = UIImage(named: "car")
    marker.map = mapView



    let DestinationMarker = GMSMarker()
    self.destCoord = CLLocationCoordinate2DMake(53.4645, -2.1873)
    DestinationMarker.position = CLLocationCoordinate2DMake(53.4643, -2.1869)
    DestinationMarker.title = "Destination"
    DestinationMarker.snippet = "Destination"
    DestinationMarker.icon = UIImage(named: "home")
    DestinationMarker.map = mapView

//折线

    path.addLatitude(53.4395, longitude:-2.1834)
    path.addLatitude(53.4403, longitude:-2.1854)
    path.addLatitude(53.4414, longitude:-2.1852)
    path.addLatitude(53.4428, longitude:-2.1832)
    path.addLatitude(53.4442, longitude:-2.1818)
    path.addLatitude(53.4449, longitude:-2.1801)
    path.addLatitude(53.4478, longitude:-2.1793)
    path.addLatitude(53.4504, longitude:-2.1798)
    path.addLatitude(53.4526, longitude:-2.1806)
    path.addLatitude(53.4545, longitude:-2.1811)
    path.addLatitude(53.4564, longitude:-2.1811)
    path.addLatitude(53.4584, longitude:-2.1811)
    path.addLatitude(53.4601, longitude:-2.1811)
    path.addLatitude(53.4617, longitude:-2.1821)
    path.addLatitude(53.4630, longitude:-2.1829)
    path.addLatitude(53.4632, longitude:-2.1851)
    path.addLatitude(53.4635, longitude:-2.1869)
    path.addLatitude(53.4638, longitude:-2.1882)
    path.addLatitude(53.4645, longitude:-2.1873)


    let polyline = GMSPolyline(path: path)
    polyline.strokeColor = .blue
    polyline.strokeWidth = 6.0
    polyline.geodesic = true
    polyline.map = mapView
    updateMarker(coordinates: destCoord)
    view = mapView
}

func updateMarker(coordinates: CLLocationCoordinate2D) {
    CATransaction.begin()
    CATransaction.setAnimationDuration(10.0)
    marker.position = coordinates
    CATransaction.commit()
}

}

【问题讨论】:

  • 你是如何做到这一点的?请帮帮我,如何处理?

标签: swift3 google-maps-markers google-maps-sdk-ios google-polyline


【解决方案1】:

我不熟悉可以为您提供此功能的库或类似工具,因此我将概述一种手动方法,您可以使用它来轻松完成此操作。

要跨多段线移动标记,您需要计算标记从每个点到下一个点所需的步数。
一种选择是对定义折线的每条线执行以下操作:
1)为每2个点定义一条线f(x) = ax + b 如果您在计算直线斜率时需要帮助,可以进一步阅读here(上式中的a

2) 定义标记在到达该行末尾之前所走的步数。您可以将线的长度除以一个常数,这将为您提供一个常数step。这并不理想,因为跨越短线和长线需要相同数量的步骤,但它是一个开始。另请注意,2 个 lat/lng 点之间的距离是在一个球体上,而不是在 2 维中那么简单。查看https://en.wikipedia.org/wiki/Haversine_formula了解更多信息。

3) 将标记放在第一个点(x1,y1),并将其移动到(x1+step, f(x1+step))
你需要判断是左移还是右移就行了。

4) 每次移动标记后,检查是否到达当前行的终点,然后重新开始下一行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-12
    • 2022-10-14
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    相关资源
    最近更新 更多