Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
675 views
in Technique[技术] by (71.8m points)

ios - How to make custom MKPolyline in SWIFT with additional argument - color

Can any help me with making custom MKPolyline with additional argument Color?

CustomPolyline.swift

import Foundation
import MapKit
class CustomPolyline : MKPolyline {
    let coordinates: UnsafeMutablePointer<CLLocationCoordinate2D>
    let count : Int = 0
    let color : String = "ff0000"
    init(coordinates: UnsafeMutablePointer<CLLocationCoordinate2D>, count: Int, color: String) {

        self.coordinates = coordinates
        self.count = count
        self.color = color
    }
}

Init

Polyline = CustomPolyline(coordinates: &Path, count: Path.count, color: "ff0000")
self.mapView.addOverlay(Polyline)

func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {      
        if (overlay is CustomPolyline) {
            var pr = MKPolylineRenderer(overlay: overlay);
            pr.strokeColor = UIColor.colorWithRGBHex(0xff0000).colorWithAlphaComponent(0.5);
            pr.lineWidth = 10;
            return pr;
        }

        return nil
    }

My solution doesn't work and I can't figure it out why. Polylines isn't visible at all. I'm a beginner in SWIFT so I think that problem is with my CustomPolyline class. Thanks for help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It can be done much more simpler than I throught:

Class

import Foundation
import MapKit

class CustomPolyline : MKPolyline {

    var color: String?
}

Init

cPolyline = CustomPolyline(coordinates: &Path, count: Path.count)
cPolyline.color = "#ff0000"
self.mapView.addOverlay(cPolyline)

func mapView(mapView: MKMapView!, rendererForOverlay overlay: CustomPolyline!) -> MKOverlayRenderer! {      

            var pr = MKPolylineRenderer(overlay: overlay);
            pr.strokeColor = UIColor(rgba: overlay.color);
            pr.lineWidth = 10;
            return pr;

    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...