Swift:MKAnnotation,执行mapView委托来定制pin

时间:2015-04-19 18:43:49

标签: ios swift mkmapview mkannotation

我只是想把红色针脚转换成紫色针脚。

这是FirstViewController.swift代码:

import UIKit
import CoreLocation
import MapKit


class FirstViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {

@IBOutlet weak var theMap: MKMapView!
var locationManager = CLLocationManager()
var userLocation = CLLocation()

override func viewDidLoad() {
    super.viewDidLoad()

    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestWhenInUseAuthorization()
    locationManager.requestAlwaysAuthorization()
    locationManager.startUpdatingLocation()


    //The Pin

    let pin1 = CLLocationCoordinate2D(
        latitude: 48.89,
        longitude: 2.6968
    )

    let thepoint = MKPointAnnotation()
    thepoint.coordinate = pin1
    thepoint.title = "the title"
    thepoint.subtitle = "the subtitle"

    theMap.addAnnotation(thepoint)

}

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!.pinColor = .Purple
    }
    else {
        pinView!.annotation = annotation
    }

    return pinView
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
    println("error")
}

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    userLocation = locations[0] as! CLLocation

    locationManager.stopUpdatingLocation()

    let location = CLLocationCoordinate2D(latitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude)

    let span = MKCoordinateSpanMake(0.05, 0.05)

    let region = MKCoordinateRegion(center: location, span: span)

    theMap.setRegion(region, animated: false)

}

}

我知道我在mapView函数的实现上做错了。

有人帮我吗?

由于

0 个答案:

没有答案