如何将红色Pin更改为自定义图像? iOS版

时间:2017-03-09 10:05:00

标签: ios swift annotations mapkit customization

我创建了一个简单的地图,从变量中获取引脚的位置。由于测试目的,我只使用用户位置。现在我想将红色Pin更改为自定义图像。我怎么做? 图像的大小应该是多少? 我知道这是一个简单的问题,但我是快速编码的新手,希望你能帮助我。

这是我的代码:

import UIKit
import CoreLocation
import MapKit

class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate{
var locationService = CLLocationManager()

@IBOutlet weak var ly: UILabel!

@IBOutlet weak var lx: UILabel!
var reuseID = "myposition";
var annotation: MKAnnotation?
var pinAnnotationView:MKPinAnnotationView!

@IBOutlet weak var mapView: MKMapView!


override func viewDidLoad() {
    super.viewDidLoad()
    mapView.showsUserLocation = false
    locationService.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
    locationService.delegate = self
    locationService.requestAlwaysAuthorization()
}
override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    locationService.startUpdatingLocation()
}
override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    locationService.stopUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    locations.forEach { location in
        print("location changed: (\(location.coordinate.latitude), \(location.coordinate.longitude))")
        lx.text = String(location.coordinate.latitude)
        ly.text = String(location.coordinate.longitude)
    }
    if (locations.count > 0) {
        if let an = annotation {
            mapView.removeAnnotation(an)
            annotation = nil
        }
        let loc = locations[0]
        let location2D = CLLocationCoordinate2D(latitude:loc.coordinate.latitude, longitude:loc.coordinate.longitude)
        centerMap(location: location2D)
        let pointAnnotation = MKPointAnnotation()
        pointAnnotation.coordinate = location2D
        annotation = pointAnnotation
        mapView.addAnnotation(pointAnnotation)
                         }
}
func centerMap(location: CLLocationCoordinate2D) {
    let span = MKCoordinateSpanMake(0.1, 0.1)
    let region = MKCoordinateRegionMake(location, span);
    mapView.setRegion(region, animated: true)

}

 }

1 个答案:

答案 0 :(得分:0)

import UIKit
import MapKit

class AlarmMapViewController: UIViewController {
    @IBOutlet weak var map: MKMapView!

    override func viewDidLoad() {
        super.viewDidLoad()
        showAlarms()
        map.showsUserLocation = true
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func showAlarms(){

        map.region.center.latitude = 49
        map.region.center.longitude = 12
        map.region.span.latitudeDelta = 1
        map.region.span.longitudeDelta = 1

        for alarm in Alarms.sharedInstance.alarms {

            let location = CLLocationCoordinate2D(
                latitude: Double(alarm.latitude),
                longitude: Double(alarm.longtitude)
            )

            let annotation = MKPointAnnotation()
            annotation.setCoordinate(location)
            annotation.title = alarm.name
            annotation.subtitle = alarm.description
            mapView(map, viewForAnnotation: annotation).annotation = annotation
            map.addAnnotation(annotation)
        }
    }

    @IBAction func zoomIn(sender: AnyObject) {
    }

    @IBAction func changeMapType(sender: AnyObject) {
    }

    func mapView(mapView: MKMapView!,
        viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {

        if annotation is MKUserLocation {
            //return nil so map view draws "blue dot" for standard user location
            return nil
        }

        let reuseId = "pin"
        var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
        if pinView == nil {
            pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
            pinView!.canShowCallout = true
            pinView!.animatesDrop = true
            pinView!.image = UIImage(named:"GreenDot")!

        }
        else {
            pinView!.annotation = annotation
        }

        return pinView
    }
}