如何更改MapKit中的引脚颜色?

时间:2017-03-23 00:29:33

标签: ios swift dictionary mapkit

以下是我第一次添加地图。如何简单地将引脚颜色更改为绿色。我看到很多扩展,但是注释(引脚)有一个简单的属性来改变颜色。

寻找一种简单的(如果可能的话)以编程方式根据某些条件更改引脚颜色的方法 - 待确定。

import UIKit
import CoreLocation
import MapKit

class ColorPointAnnotation: MKPointAnnotation {
    var pinColor: UIColor

    init(pinColor: UIColor) {
        self.pinColor = pinColor
        super.init()
    }
}

class GMap_vc : BaseViewController, CLLocationManagerDelegate,  MKMapViewDelegate
{

    @IBOutlet weak var map: MKMapView!

    var firsttime:String = "YES";
    var locationManager:CLLocationManager!

    let regionRadius: CLLocationDistance = 1000

    var initialLocation = CLLocation()

    override func viewDidLoad()
    {
        newVars.put_timelog(timedata: "#######_____gmapc_vc_11")

        super.viewDidLoad()

        determineMyCurrentLocation()

        if (CLLocationManager.locationServicesEnabled())
        {
            locationManager = CLLocationManager()
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyBest
            locationManager.requestAlwaysAuthorization()
            locationManager.startUpdatingLocation()
            locationManager.startUpdatingHeading()

        }
        newVars.put_timelog(timedata: "#######_____gmapc_vc_12")

        let locationLat = locationManager.location?.coordinate.latitude
        let locationLong = locationManager.location?.coordinate.longitude
        print("locationLat = \(locationLat)")
        print("locationLong = \(locationLong)")


        //initialLocation = CLLocation(latitude: 31.3222, longitude:  -92.4555)
        //centerMapOnLocation(location: initialLocation)

        newVars.put_timelog(timedata: "#######_____gmapc_vc_13")
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
    {

        newVars.put_timelog(timedata: "#######_____gmapc_vc_2")

        print(" ")

        let location = locations.last! as CLLocation
        print("locationlatitude = \(location.coordinate.latitude)")
        print("lLocationlongitude = \(location.coordinate.longitude)")

        let userLocation:CLLocation = locations[0] as CLLocation
        print("userLocationlatitude = \(userLocation.coordinate.latitude)")
        print("userLocationlongitude = \(userLocation.coordinate.longitude)")


        if firsttime == "YES"
        {
            let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
            let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))

            self.map.setRegion(region, animated: true)
            firsttime = "NO"
        }
        map.removeAnnotations(map.annotations)

        //let annotation1 = MKPointAnnotation()
        let annotation2 = MKPointAnnotation()

        let annotation1 = ColorPointAnnotation(pinColor: UIColor.blue)
        annotation1.coordinate = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
        annotation1.title = "You"
        map.addAnnotation(annotation1)

        annotation2.coordinate = CLLocationCoordinate2D(latitude: location.coordinate.latitude + 0.003, longitude: location.coordinate.longitude)
        annotation2.title = "Me"
        annotation2.subtitle = "One At A Time"
        map.addAnnotation(annotation2)

    }

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print("Errors " + error.localizedDescription)
    }

    func centerMapOnLocation(location: CLLocation)
    {
        newVars.put_timelog(timedata: "#######_____gmapc_vc_3")

        let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, regionRadius * 2.0, regionRadius * 2.0)
        map.setRegion(coordinateRegion, animated: true)
    }

    func determineMyCurrentLocation()
    {
        newVars.put_timelog(timedata: "#######_____gmapc_vc_4")

        newVars.put_timelog(timedata: "#######_____gmapc_vc4_determineMyCurrentLocation1")
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestAlwaysAuthorization()

        if CLLocationManager.locationServicesEnabled()
        {
            newVars.put_timelog(timedata: "#######_____gmapc_vc4_determineMyCurrentLocation2")
            locationManager.startUpdatingLocation()
            locationManager.startUpdatingHeading()
        }

    }

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        if annotation is MKUserLocation {
            return nil
        }

        let reuseId = "pin"
        var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView
        if pinView == nil {
            pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)

            let colorPointAnnotation = annotation as! ColorPointAnnotation
            pinView?.pinTintColor = colorPointAnnotation.pinColor
        }
        else {
            pinView?.annotation = annotation
        }

        return pinView
    }

}

0 个答案:

没有答案