简体   繁体   中英

How do I create a line (polyline) between location points in Swift?

I am trying to make an app that tracks a user's location and creates a line following where the user went. I have created a bunch of annotations following where the user went, but can't figure out how to create a line between them. I am new to swift and Xcode and I have looked all over the internet but can't find anything that works. Picture of app with annotations following user location

Here is my code:

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
        }
    }

You can try

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)

// Also you may be keen to zoom out to show all annotations + seeing drawn line

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)


}

// Also rendererForOverlay func must be in class level not inside another 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
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM