Advertisement
Guest User

Map markers, history of paper planes

a guest
Dec 2nd, 2015
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 5.48 KB | None | 0 0
  1. //
  2. //  ViewController.swift
  3. //  papp
  4. //
  5. //  Created by Marco Lemmens on 26-11-15.
  6. //  Copyright © 2015 Fontys. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import GoogleMaps
  11. import CoreLocation
  12. import Foundation
  13. import Alamofire
  14.  
  15. class MarkersViewController: UIViewController, GMSMapViewDelegate, CLLocationManagerDelegate {
  16.    
  17.     var lm:CLLocationManager!
  18.     var mapView:GMSMapView!
  19.     let locationManager = CLLocationManager()
  20.     var currentLat: Double = 0.0
  21.     var currentLon: Double = 0.0
  22.     var firstLoop: Bool = true
  23.  
  24.    
  25.     override func viewDidLoad() {
  26.         super.viewDidLoad()
  27.        
  28.        
  29.         self.locationManager.delegate = self
  30.         self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
  31.         self.locationManager.requestWhenInUseAuthorization()
  32.         self.locationManager.startUpdatingLocation()
  33.        
  34.        
  35.         lm = CLLocationManager()
  36.         lm.delegate = self
  37.         lm.startUpdatingHeading()
  38.        
  39.        
  40.         let camera = GMSCameraPosition.cameraWithLatitude(self.currentLat,
  41.             longitude: self.currentLon, zoom: 7)
  42.         mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
  43.         mapView.mapType = kGMSTypeNormal
  44.         mapView.delegate = self
  45.        
  46.  
  47.         self.view = mapView
  48.         mapView.myLocationEnabled = true
  49.        
  50.        
  51.        
  52.        
  53.        
  54.        
  55.         showAllMarkers()
  56.        
  57.        
  58.        
  59.        
  60.        
  61.     }
  62.    
  63.     func locationManager(manager: CLLocationManager, didUpdateToLocation newLocation: CLLocation, fromLocation oldLocation: CLLocation) {
  64.        
  65.         CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: { (placemarks, error) ->
  66.             Void in
  67.            
  68.             if error != nil{
  69.                 print("Error: " + (error!.localizedDescription))
  70.                 return
  71.             }
  72.            
  73.             if placemarks!.count > 0{
  74.                
  75.                 let pm = placemarks![0]
  76.                 self.currentLat = Double((pm.location?.coordinate.latitude)!)
  77.                
  78.                 self.currentLon = Double((pm.location?.coordinate.longitude)!)
  79.                
  80.                 if self.firstLoop == true{
  81.                
  82.                     self.updateMapCenter()
  83.                     self.firstLoop = false
  84.                    
  85.                 }
  86.                
  87.                
  88.             }
  89.            
  90.         })
  91.        
  92.  
  93.     }
  94.    
  95.    
  96.     func updateMapCenter(){
  97.        
  98.         let location = CLLocationCoordinate2D(latitude: self.currentLat, longitude: self.currentLon)
  99.         mapView.animateToLocation(location)
  100.     }
  101.    
  102.    
  103.     func showAllMarkers(){
  104.        
  105.         let url = "http://athena.fhict.nl/users/i278062/tomar/json.php"
  106.         Alamofire.request(.GET, url).responseJSON { response in
  107.             switch response.result {
  108.             case .Success(let data):
  109.                 let json = JSON(data)
  110.                
  111.                
  112.                
  113.                 for index in 0...json["people"].count - 1 {
  114.                    
  115.                     let title = String(json["people"][index]["locationName"])
  116.                     let personName = String(json["people"][index]["name"])
  117.                     let personLat = String(json["people"][index]["lat"])
  118.                     let personLon = String(json["people"][index]["lon"])
  119.                     let degree = String(json["people"][index]["degree"])
  120.                     let vliegtuigID = String(json["people"][index]["id"])
  121.                    
  122.                     let iconRotation: CLLocationDegrees = Double(degree)!
  123.                    
  124.                     let marker = GMSMarker()
  125.                     marker.position = CLLocationCoordinate2DMake(Double(personLat)!, Double(personLon)!)
  126.                     marker.title = title
  127.                     marker.snippet = "van: " + personName
  128.                     marker.icon = UIImage(named: "markerIcon")
  129.                     marker.rotation = iconRotation
  130.                     marker.flat = true
  131.                     marker.userData = vliegtuigID
  132.                     marker.map = self.mapView
  133.                    
  134.                 }
  135.                
  136.                
  137.                
  138.             case .Failure(let error):
  139.                 print("Request failed with error: \(error)")
  140.             }
  141.         }
  142.  
  143.     }
  144.    
  145.    
  146.  
  147.  
  148.  
  149.  
  150.    
  151.    
  152.     // MARK: GMSMapViewDelegate
  153.    
  154.     func mapView(mapView: GMSMapView!, didTapMarker marker: GMSMarker!) -> Bool {
  155.         self.mapView.clear()
  156.         marker.map = self.mapView
  157.         marker.infoWindowAnchor = CGPoint(x: 0.5, y: 0.5)
  158.        
  159.        
  160.        
  161.        
  162.         /// Laat alle vorige locaties van dit vliegtuigje zien
  163.        
  164.        
  165.        
  166.         print(marker.userData)
  167.        
  168.         let path = GMSMutablePath()
  169.         path.addCoordinate(CLLocationCoordinate2DMake(50.769471, 4.790039))
  170.         path.addCoordinate(CLLocationCoordinate2DMake(52.900619, 5.910645))
  171.         let polyline = GMSPolyline(path: path)
  172.         polyline.strokeWidth = 3
  173.         polyline.strokeColor = UIColor.blackColor()
  174.         polyline.geodesic = true
  175.        
  176.         return false
  177.     }
  178.    
  179.     func mapView(mapView: GMSMapView!, didTapAtCoordinate coordinate: CLLocationCoordinate2D) {
  180.         print("You tapped at \(coordinate.latitude), \(coordinate.longitude)")
  181.         self.mapView.clear()
  182.         showAllMarkers()
  183.    
  184.    
  185.    
  186.     }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement