import UIKit
import MapKit
class ViewController: UIViewController, MKMapViewDelegate {
var points:NSMutableArray?
let locationManager = CLLocationManager()
//var currentLocation: CLLocationCoordinate2D?
var routeLine: MKPolyline?
@IBOutlet weak var mapView: MKMapView!
//@IBOutlet weak var currentLocationButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
if CLLocationManager.authorizationStatus() == CLAuthorizationStatus.NotDetermined {
locationManager.requestAlwaysAuthorization()
}
self.currentLocationButton.addTarget(self, action: Selector("currentLocationButtonAction"), forControlEvents: UIControlEvents.TouchUpInside)
self.mapView.mapType = MKMapType.Standard
self.mapView.userTrackingMode = MKUserTrackingMode.Follow
self.mapView.showsUserLocation = true
self.mapView.delegate = self
var point = CLLocation(latitude: 121, longitude: 38)
if self.points == nil {
self.points = NSMutableArray()
}
self.points?.addObject(point)
self.routeLine = self.polyline()
if self.routeLine != nil {
self.mapView.addOverlay(self.routeLine)
}
}
func polyline() -> MKPolyline {
var coords = [CLLocationCoordinate2D]()
for (var i = 0; i < self.points?.count; i++) {
var location = self.points?.objectAtIndex(i) as CLLocation
let c = location.coordinate
coords.append(c)
}
return MKPolyline(coordinates: &coords, count: self.points!.count)
}
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
println("mark") //这句并没有打印
if overlay is MKPolyline {
var polylineRenderer = MKPolylineRenderer(polyline: self.routeLine)
polylineRenderer.fillColor = UIColor.redColor()
polylineRenderer.strokeColor = UIColor.blueColor()
polylineRenderer.lineWidth = 4
return polylineRenderer
}
return nil
}
}