Last active
October 7, 2016 05:05
-
-
Save julianshen/229f4ac32b3893816bd7636b96fe6f7d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// mapanitest | |
// | |
// Created by Julian Shen on 2016/10/4. | |
// Copyright © 2016年 cowbay.wtf. All rights reserved. | |
// | |
import UIKit | |
import MapKit | |
import LocationPickerViewController | |
class ViewController: UIViewController, MKMapViewDelegate { | |
@IBOutlet weak var mapView: MKMapView! | |
var planePositionIndex:Int = 0 | |
var geodesicPolyline:MKGeodesicPolyline? | |
let thePlane = MKPointAnnotation() | |
var startloc, endloc: CLLocationCoordinate2D? | |
var step:Int = 50 | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
print("view did load") | |
mapView.delegate = self | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
func updatePlane() { | |
planePositionIndex = planePositionIndex + step; | |
if (planePositionIndex >= geodesicPolyline!.pointCount) | |
{ | |
//plane has reached end, stop moving | |
planePositionIndex = 0 | |
return; | |
} | |
let s = 8.0 | |
let nextMapPoint = geodesicPolyline?.points()[planePositionIndex] | |
thePlane.coordinate = MKCoordinateForMapPoint(nextMapPoint!) | |
mapView.region = MKCoordinateRegionMake(thePlane.coordinate, MKCoordinateSpan(latitudeDelta: s, longitudeDelta: s)) | |
perform(#selector(updatePlane), with: self, afterDelay: 0.4) | |
} | |
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer { | |
guard let polyline = overlay as? MKPolyline else { | |
return MKOverlayRenderer() | |
} | |
let renderer = MKPolylineRenderer(polyline: polyline) | |
renderer.lineWidth = 3.0 | |
renderer.alpha = 0.5 | |
renderer.strokeColor = UIColor.blue | |
return renderer | |
} | |
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { | |
let planeIdentifier = "Plane" | |
let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: planeIdentifier) | |
?? MKAnnotationView(annotation: annotation, reuseIdentifier: planeIdentifier) | |
annotationView.image = UIImage(named: "ic_flight_48pt") | |
return annotationView | |
} | |
@IBAction func pickLocation(_ sender: AnyObject) { | |
let button = sender as! UIButton | |
let locationPicker = LocationPicker() | |
locationPicker.pickCompletion = { (pickedLocationItem) in | |
// Do something with the location the user picked. | |
print(pickedLocationItem.coordinate) | |
print(button.accessibilityIdentifier) | |
let coord = pickedLocationItem.coordinate! | |
if button.accessibilityIdentifier == "startloc" { | |
self.startloc = CLLocationCoordinate2D(latitude: coord.latitude, longitude: coord.longitude) | |
} else if button.accessibilityIdentifier == "endloc" { | |
self.endloc = CLLocationCoordinate2D(latitude: coord.latitude, longitude: coord.longitude) | |
} | |
} | |
locationPicker.addBarButtons() | |
let navigationController = UINavigationController(rootViewController: locationPicker) | |
present(navigationController, animated: true) { | |
} | |
} | |
@IBAction func displayPath(_ sender: AnyObject) { | |
guard let sl = startloc else { | |
print("error: no startloc") | |
return | |
} | |
guard let el = endloc else { | |
print("error: no endloc") | |
return | |
} | |
let coordinates = [sl, el] | |
if let gl = geodesicPolyline { | |
mapView.remove(gl) | |
} | |
geodesicPolyline = MKGeodesicPolyline(coordinates: coordinates, count: 2) | |
print(geodesicPolyline!.pointCount) | |
step = geodesicPolyline!.pointCount/(Int)(20/0.4) | |
mapView.add(geodesicPolyline!) | |
thePlane.coordinate = sl | |
mapView.addAnnotation(thePlane) | |
UIView.animate(withDuration: 1.5) { | |
let span = MKCoordinateSpanMake(8.0, 8.0) | |
let region1 = MKCoordinateRegion(center: sl, span: span) | |
self.mapView.setRegion(region1, animated: true) | |
self.perform(#selector(self.updatePlane), with: self, afterDelay: 0.4) | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment