Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ViewController.swift
- // papp
- //
- // Created by Marco Lemmens on 26-11-15.
- // Copyright © 2015 Fontys. All rights reserved.
- //
- import UIKit
- import GoogleMaps
- import CoreLocation
- import Foundation
- import Alamofire
- class MarkersViewController: UIViewController, GMSMapViewDelegate, CLLocationManagerDelegate {
- var lm:CLLocationManager!
- var mapView:GMSMapView!
- let locationManager = CLLocationManager()
- var currentLat: Double = 0.0
- var currentLon: Double = 0.0
- var firstLoop: Bool = true
- override func viewDidLoad() {
- super.viewDidLoad()
- self.locationManager.delegate = self
- self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
- self.locationManager.requestWhenInUseAuthorization()
- self.locationManager.startUpdatingLocation()
- lm = CLLocationManager()
- lm.delegate = self
- lm.startUpdatingHeading()
- let camera = GMSCameraPosition.cameraWithLatitude(self.currentLat,
- longitude: self.currentLon, zoom: 7)
- mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
- mapView.mapType = kGMSTypeNormal
- mapView.delegate = self
- self.view = mapView
- mapView.myLocationEnabled = true
- showAllMarkers()
- }
- func locationManager(manager: CLLocationManager, didUpdateToLocation newLocation: CLLocation, fromLocation oldLocation: CLLocation) {
- CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: { (placemarks, error) ->
- Void in
- if error != nil{
- print("Error: " + (error!.localizedDescription))
- return
- }
- if placemarks!.count > 0{
- let pm = placemarks![0]
- self.currentLat = Double((pm.location?.coordinate.latitude)!)
- self.currentLon = Double((pm.location?.coordinate.longitude)!)
- if self.firstLoop == true{
- self.updateMapCenter()
- self.firstLoop = false
- }
- }
- })
- }
- func updateMapCenter(){
- let location = CLLocationCoordinate2D(latitude: self.currentLat, longitude: self.currentLon)
- mapView.animateToLocation(location)
- }
- func showAllMarkers(){
- let url = "http://athena.fhict.nl/users/i278062/tomar/json.php"
- Alamofire.request(.GET, url).responseJSON { response in
- switch response.result {
- case .Success(let data):
- let json = JSON(data)
- for index in 0...json["people"].count - 1 {
- let title = String(json["people"][index]["locationName"])
- let personName = String(json["people"][index]["name"])
- let personLat = String(json["people"][index]["lat"])
- let personLon = String(json["people"][index]["lon"])
- let degree = String(json["people"][index]["degree"])
- let vliegtuigID = String(json["people"][index]["id"])
- let iconRotation: CLLocationDegrees = Double(degree)!
- let marker = GMSMarker()
- marker.position = CLLocationCoordinate2DMake(Double(personLat)!, Double(personLon)!)
- marker.title = title
- marker.snippet = "van: " + personName
- marker.icon = UIImage(named: "markerIcon")
- marker.rotation = iconRotation
- marker.flat = true
- marker.userData = vliegtuigID
- marker.map = self.mapView
- }
- case .Failure(let error):
- print("Request failed with error: \(error)")
- }
- }
- }
- // MARK: GMSMapViewDelegate
- func mapView(mapView: GMSMapView!, didTapMarker marker: GMSMarker!) -> Bool {
- self.mapView.clear()
- marker.map = self.mapView
- marker.infoWindowAnchor = CGPoint(x: 0.5, y: 0.5)
- /// Laat alle vorige locaties van dit vliegtuigje zien
- print(marker.userData)
- let path = GMSMutablePath()
- path.addCoordinate(CLLocationCoordinate2DMake(50.769471, 4.790039))
- path.addCoordinate(CLLocationCoordinate2DMake(52.900619, 5.910645))
- let polyline = GMSPolyline(path: path)
- polyline.strokeWidth = 3
- polyline.strokeColor = UIColor.blackColor()
- polyline.geodesic = true
- return false
- }
- func mapView(mapView: GMSMapView!, didTapAtCoordinate coordinate: CLLocationCoordinate2D) {
- print("You tapped at \(coordinate.latitude), \(coordinate.longitude)")
- self.mapView.clear()
- showAllMarkers()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement