【问题标题】:How do I create a line (polyline) between location points in Swift?如何在 Swift 中的位置点之间创建一条线(折线)?
【发布时间】:2018-08-31 05:04:02
【问题描述】:

我正在尝试制作一个应用程序来跟踪用户的位置并在用户去过的地方创建一条线。我在用户去过的地方创建了一堆注释,但不知道如何在它们之间创建一条线。我是 swift 和 Xcode 的新手,我在整个互联网上都看过,但找不到任何有用的东西。 Picture of app with annotations following user location

这是我的代码:

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {
    @IBOutlet var mapView: MKMapView?

    //Map
    @IBOutlet weak var map: MKMapView!
    @IBOutlet weak var lblSpeed: UILabel!

    let manager = CLLocationManager()

    @IBOutlet weak var lblAltitude: UILabel!

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let location = locations[0]

        let span:MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01)

        let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)

        let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)

        lblAltitude.text = String(format:"%.2f", location.altitude)
        lblSpeed.text = String(format:"%.2f", location.speed)

        self.map.showsUserLocation = true

        var testLocation = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)

        var annotation = MKPointAnnotation()
        annotation.coordinate = testLocation
        map.addAnnotation(annotation)

        var locationTest = [CLLocation(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)]
        var coordinates = locations.map({(location: CLLocation!) -> CLLocationCoordinate2D in return location.coordinate})
        var polyline = MKPolyline(coordinates: &testLocation, count: locations.count)

        func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
            if overlay is MKPolyline {
                let polylineRenderer = MKPolylineRenderer(overlay: overlay)
                polylineRenderer.strokeColor = UIColor.blue
                polylineRenderer.lineWidth = 5
                return polylineRenderer
            }

            return nil
        }
    }

【问题讨论】:

  • 为什么你的mapView(mapView:rendererForOverlay:)函数在locationManager(_: didUpdateLocations:)函数里面?

标签: ios swift location polyline


【解决方案1】:

你可以试试

let blueLocation1 = CLLocationCoordinate2D(latitude: lat1, longitude: lon1)       

let blueLocation2 = CLLocationCoordinate2D(latitude: lat2, longitude: lon2)

let routeLine = MKPolyline(coordinates:[blueLocation1,blueLocation2], count:2)

self.mapView.add(routeLine)

// 你也可能热衷于缩小以显示所有注释 + 看到画线

func zoomToFitMapAnnotations(aMapView:MKMapView)
{
    if(aMapView.annotations.count == 0)
    {
        return
    }


    var topLeftCoord = CLLocationCoordinate2D.init(latitude: -90, longitude: 180)


    var bottomRightCoord = CLLocationCoordinate2D.init(latitude: 90, longitude: -180)


    for i in 0..<aMapView.annotations.count
    {
        let annotation = aMapView.annotations[i]

        topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude);
        topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude);

        bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude);
        bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude);
    }


    let resd = CLLocationCoordinate2D.init(latitude: topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5, longitude: topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5)

    let span = MKCoordinateSpan.init(latitudeDelta: fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.3, longitudeDelta: fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.3)

    var region = MKCoordinateRegion.init(center: resd, span: span);



    region = aMapView.regionThatFits(region)

    aMapView.setRegion(region, animated: true)


}

// 另外rendererForOverlay func 必须在类级别而不是在另一个 func 中

 func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
        if overlay is MKPolyline {
            let polylineRenderer = MKPolylineRenderer(overlay: overlay)
            polylineRenderer.strokeColor = UIColor.blue
            polylineRenderer.lineWidth = 5
            return polylineRenderer
        }

        return nil
    }

【讨论】:

  • 谢谢,但这仍然不起作用,因为对于 blueLocation2-我没有任何坐标,因为它在不断更新。我可以创建某种数组或其他东西来将所有坐标附加到用户所在的位置,然后从中获取 [1] 值吗?
  • 要绘制折线,您必须至少有 2 个坐标,无论要求如何
  • 如何从不断更新的用户位置中获取信息?我可以得到第一个,但我如何得到第二个?
  • 第一个位置 ->>> let location = locations[0] ,这是最后一个 >>>> let lastLocation = locations.last
  • 我确定它有效,但这条线仍然不会出现。它显示此错误:错误/BuildRoot/Library/Caches/com.apple.xbs/Sources/VectorKit_Sim/VectorKit-1230.32.8.29.9/GeoGL/GeoGL/GLCoreContext.cpp 1763: InfoLog SolidRibbonShader: ERROR /BuildRoot/Library/Caches /com.apple.xbs/Sources/VectorKit_Sim/VectorKit-1230.32.8.29.9/GeoGL/GeoGL/GLCoreContext.cpp 1764:警告:片段着色器未读取顶点着色器“v_gradient”的输出我觉得自己像个白痴问这么多问题-对不起!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-25
  • 1970-01-01
  • 2016-08-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多