jeudi 23 juin 2016

Swift - MKPolyline Not Appearing


My problem is that when a jog has been completed, I can not present the recordedMapView with a polyline that had tracked the location.

Currently I am able to persist the data from tracking a run, fetch the data and unwrap/assign them to the respective variables, however, I am not sure where I go wrong in the code that is not allowing a polyline to appear. The map region does seem to be getting set properly as the map zooms in and out dynamically.

What could be the issue for why a polyline is not being presented and is there a solution to the code that I have provided to aid in this problem?

var context : NSManagedObjectContext?

var runTimestamp      : NSDate?
var runDuration       : NSNumber?
var runDistance       : NSNumber?
var runLocations      : NSOrderedSet?

var locationTimeStamp : NSDate?
var locationLatitude  : NSNumber?
var locationLongitude : NSNumber?

override func viewDidLoad()
{
    super.viewDidLoad()

    guard let context = context, finishedLocations = fetchLocation ( context ), finishedRun = fetchRun ( context ) else { return }

    for location in finishedLocations
    {
        if let timestamp = location.timestamp, latitude = location.latitude, longitude = location.longitude
        {
            locationTimeStamp = timestamp
            locationLatitude  = latitude
            locationLongitude = longitude
        }
    }

    for run in finishedRun
    {
        if let timeStamp = run.timestamp, duration = run.duration, distance = run.distance, locations = run.locations
        {
            runTimestamp = timeStamp
            runDuration  = duration
            runDistance  = distance
            runLocations = locations
        }
    }

    updateUI      ()
} 

 func loadMapView()
{
    if runLocations!.count > 0
    {
        recordedMapView.region =    mapRegion()
        recordedMapView.addOverlay( polyline () )
    }
    else
    {
        let alertController = UIAlertController( title: "Error", message: "No Locations Saved", preferredStyle: .Alert )
        let alertAction     = UIAlertAction    ( title: "Error", style  : .Default            , handler       : nil    )
        alertController.addAction( alertAction                                      )
        presentViewController    ( alertController, animated: true, completion: nil )
    }
}

func mapRegion() -> MKCoordinateRegion
{
    let initialLocation = runLocations!.firstObject as! Location

    var minLat = initialLocation.latitude! .doubleValue
    var minLng = initialLocation.longitude!.doubleValue
    var maxLat = minLat
    var maxLng = minLng

    let locations = runLocations!.array as! [Location]

    for location in locations
    {
        minLat = min( minLat, location.latitude! .doubleValue )
        minLng = min( minLng, location.longitude!.doubleValue )
        maxLat = max( maxLat, location.latitude! .doubleValue )
        maxLng = max( maxLng, location.longitude!.doubleValue )
    }

    return MKCoordinateRegion(
        center: CLLocationCoordinate2D( latitude     : ( (minLat + maxLat)/2  ) , longitude     : ( (minLng + maxLng)/2  ) ),
        span  : MKCoordinateSpan      ( latitudeDelta: ( (maxLat - minLat)*1.1) , longitudeDelta: ( (maxLng - minLng)*1.1) )
                             )
}

func polyline() -> MKPolyline
{
    var coordinates = [CLLocationCoordinate2D]()
    let locations   = runLocations!.array as! [Location]
    for location in locations
    {
        coordinates.append( CLLocationCoordinate2D(latitude: (location.latitude!.doubleValue), longitude: (location.longitude!.doubleValue)) )
    }
    return MKPolyline(coordinates: &coordinates, count: runLocations!.count)
}

func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer
{
    let polyline         = overlay as! MulticolorPolylineSegment
    let renderer         = MKPolylineRenderer(polyline: polyline)
    renderer.strokeColor = polyline.color
    renderer.lineWidth   = 3
    return renderer
}

Aucun commentaire:

Enregistrer un commentaire