Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // MapViewController.swift
- // CheapEat
- //
- // Created by Johnathon Moss on 16/01/2015.
- // Copyright (c) 2015 CheapEat. All rights reserved.
- //
- import UIKit
- import MapKit
- import CoreLocation
- class MapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
- @IBOutlet weak var mapView: MKMapView!
- var latArray = [String]()
- var lngArray = [String]()
- var resName = [String]()
- var deals:[Deal] = [Deal]()
- let model:DealModel = DealModel()
- var lat:String = ""
- var lng:String = ""
- var manager = CLLocationManager()
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view.
- //add logo to nav bar
- let navBarLogo: UIImageView = UIImageView(frame: CGRectMake(0, 0, 120, 36))
- navBarLogo.image = UIImage(named: "logo")
- self.navigationItem.titleView = navBarLogo
- // Core Location
- manager.delegate = self
- manager.desiredAccuracy = kCLLocationAccuracyBest
- manager.requestWhenInUseAuthorization()
- manager.startUpdatingLocation()
- // show user location on map
- // mapView.showsUserLocation = true
- // this combined with manager.stopUpdatingLocation fixed map snapping back issue
- // mapView.userLocation
- // -34.426298, 150.892260
- /*
- var latitude:CLLocationDegrees = -34.426298
- var longitude:CLLocationDegrees = 150.89226
- var latDelta:CLLocationDegrees = 0.03
- var lonDelta:CLLocationDegrees = 0.03
- var span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)
- var location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
- var region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)
- mapView.setRegion(region, animated: true)
- */
- // add annotations to map
- }
- func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
- // var userLocation:CLLocation = locations[0] as CLLocation
- // var latitude:CLLocationDegrees = userLocation.coordinate.latitude
- // var longitude:CLLocationDegrees = userLocation.coordinate.longitude
- var latString : NSString = "-34.4232722"
- var lngString : NSString = "150.8865837"
- var latitude = latString.doubleValue
- var longitude = lngString.doubleValue
- // var latString:String = "\(latitude)"
- // var lngString:String = "\(longitude)"
- var latDelta:CLLocationDegrees = 0.03
- var lonDelta:CLLocationDegrees = 0.03
- var span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)
- var location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
- var region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)
- mapView.setRegion(region, animated: false)
- // stop updating the location
- manager.stopUpdatingLocation()
- self.lat = latString
- self.lng = lngString
- // Get the deals from the deal model
- self.deals = getDeals()
- // Add annotations to the map
- var index:Int
- for var index = 0; index < deals.count; ++index {
- var currentdeal = self.deals[index]
- var lat = NSString(string: currentdeal.latitude.stringByTrimmingCharactersInSet(NSCharacterSet.punctuationCharacterSet()))
- var latDouble:CLLocationDegrees = -lat.doubleValue
- var long = NSString(string: currentdeal.longitude.stringByTrimmingCharactersInSet(NSCharacterSet.punctuationCharacterSet()))
- var longDouble:CLLocationDegrees = long.doubleValue
- var location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latDouble, longDouble)
- var annotation = MKPointAnnotation()
- annotation.coordinate = location
- annotation.title = currentdeal.location
- annotation.subtitle = "Deal Available"
- self.mapView.addAnnotation(annotation)
- }
- }
- 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
- }
- var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier("pin")
- if pinView == nil {
- pinView = MKAnnotationView(annotation: annotation, reuseIdentifier: "pin")
- pinView!.canShowCallout = true
- pinView!.image = UIImage(named: "mappin")
- // Add image to left callout
- // var mugIconView = UIImageView(image: UIImage(named: "mappin"))
- // pinView!.leftCalloutAccessoryView = mugIconView
- // Add detail button to right callout
- var calloutButton = UIButton.buttonWithType(.DetailDisclosure) as UIButton
- pinView!.rightCalloutAccessoryView = calloutButton
- calloutButton.addTarget(self, action: Selector("DetailView"), forControlEvents: UIControlEvents.TouchUpInside)
- }
- else {
- pinView!.annotation = annotation
- }
- return pinView
- }
- func DetailView(){
- println("Button Pressed")
- }
- func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
- println(error)
- }
- func getDeals() -> [Deal] {
- // Array of deal objects
- var deals:[Deal] = [Deal]()
- self.latArray = [String]()
- // Get Json array of dictionaries
- let jsonObjects:[NSDictionary] = self.getRemoteJsonFile()
- // Loop through each dictionary and assign values to our deal objs
- var index:Int
- for index = 0; index < jsonObjects.count; index++ {
- // Current JSON dict
- let jsonDictionary:NSDictionary = jsonObjects[index]
- let restaurantDictionarys:[NSDictionary] = jsonDictionary["restaurants"] as [NSDictionary]
- // Create a deal obj
- var q:Deal = Deal()
- // Assign the values of each key value pair to the deal object
- q.title = jsonDictionary["name"] as String
- q.price = jsonDictionary["price_data"] as String
- q.imageID = jsonDictionary["id"] as String
- q.location = restaurantDictionarys[0]["name"] as String
- q.latitude = restaurantDictionarys[0]["lat"] as String
- self.latArray.append(q.latitude)
- q.longitude = restaurantDictionarys[0]["lng"] as String
- q.days = jsonDictionary["availability_day"] as String
- q.time = jsonDictionary["availability_time"] as String
- // Add the question to the question array
- deals.append(q)
- }
- println(self.latArray)
- // Return list of question objects
- return deals
- }
- func getRemoteJsonFile() -> [NSDictionary] {
- // Create a new URL
- // let remoteUrl:NSURL? = NSURL(string: "http://staging.api.cheapeat.com.au/search?lat=\(self.lat)&lng=\(self.lng)")
- let remoteUrl:NSURL? = NSURL(string: "http://staging.api.cheapeat.com.au/search?lat=-34.4232722&lng=150.8865837")
- let urlString:String = "\(remoteUrl)"
- println(urlString)
- // Check if it's nil
- if let actualRemoteUrl = remoteUrl {
- // Try to get the data
- let fileData:NSData? = NSData(contentsOfURL: actualRemoteUrl)
- // Check if it's nil
- if let actualFileData = fileData {
- // Parse out the dictionaries
- let arrayOfDictionaries:[NSDictionary]? = NSJSONSerialization.JSONObjectWithData(actualFileData, options: NSJSONReadingOptions.MutableContainers, error: nil) as [NSDictionary]?
- if let actualArrayOfDictionaries = arrayOfDictionaries {
- // Successfully parsed out array of dictionaries
- return actualArrayOfDictionaries
- }
- }
- }
- return [NSDictionary]()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment