diff --git a/MapViewController.swift b/MapViewController.swift new file mode 100644 index 0000000..d892002 --- /dev/null +++ b/MapViewController.swift @@ -0,0 +1,158 @@ +// +// MapViewController.swift +// adam +// +// Created by ariyasuk-k on 6/24/15. +// Copyright (c) 2015 gobbl. All rights reserved. +// + +import UIKit +import Alamofire +import SwiftyJSON + +class MapViewController: UIViewController, GMSMapViewDelegate { + + @IBOutlet weak var addressTextView: UITextView! + @IBAction func backToDetail(sender: AnyObject) { + self.performSegueWithIdentifier("backFromMapView", sender: self) + } + + @IBOutlet weak var mapView: UIView! + var restaurant: Restaurant! + + var gMap:GMSMapView! + var gCamera:GMSCameraPosition! + var destinationMarker:GMSMarker! + var steps:Array! + var polyline:GMSPolyline! + + var apiKey:String! + + override func viewWillAppear(animated: Bool) { + super.viewWillAppear(animated) + self.gMap.myLocationEnabled = true + self.gMap.addObserver(self, forKeyPath: "myLocation", options: .New, context: nil) + } + + override func viewWillDisappear(animated: Bool) { + super.viewWillDisappear(animated) + self.gMap.removeObserver(self, forKeyPath: "myLocation") + } + override func viewDidLoad() { + super.viewDidLoad() + + // Do any additional setup after loading the view. + + if let path = NSBundle.mainBundle().pathForResource("APISetting", ofType: "plist") { + if let dict = NSDictionary(contentsOfFile: path) as? Dictionary { + if let googleMapAPI = dict["GoogleDirection"] as? String{ + self.apiKey = googleMapAPI + } + } + } + + if restaurant != nil { + + addressTextView.text = restaurant.address + + let screenWidth = UIScreen.mainScreen().bounds.width + let screenHeight = UIScreen.mainScreen().bounds.height + + self.gCamera = GMSCameraPosition.cameraWithLatitude(restaurant.location.coordinate.latitude, + longitude: restaurant.location.coordinate.longitude, zoom: 17) + + self.gMap = GMSMapView.mapWithFrame(CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight), camera: self.gCamera) + self.gMap.myLocationEnabled = true + self.gMap.settings.myLocationButton = true + self.gMap.settings.compassButton = true + self.gMap.delegate = self + + self.destinationMarker = GMSMarker() + self.destinationMarker.position = self.gCamera.target + self.destinationMarker.snippet = restaurant.name + self.destinationMarker.appearAnimation = kGMSMarkerAnimationPop + self.destinationMarker.map = self.gMap + + self.mapView.addSubview(self.gMap) + + } + } + + override func didReceiveMemoryWarning() { + super.didReceiveMemoryWarning() + // Dispose of any resources that can be recreated. + } + + // MARK: - Google Maps Delegate + func mapView(mapView: GMSMapView!, didTapMarker marker: GMSMarker!) -> Bool { + if let startLocation = self.gMap.myLocation { + fetchDirectionsFrom(startLocation.coordinate, to: self.destinationMarker.position, completion: { (optionalRoute) -> Void in + + }) + } + + return true + } + + + + /* + // MARK: - Navigation + + // In a storyboard-based application, you will often want to do a little preparation before navigation + override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { + // Get the new view controller using segue.destinationViewController. + // Pass the selected object to the new view controller. + } + */ + + + // MARK: Observer + + override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer) { + + if keyPath == "myLocation" && object.isKindOfClass(GMSMapView) { + + /*fetchDirectionsFrom(self.gMap.myLocation.coordinate, to: self.destinationMarker.position, completion: { (optionalRoute) -> Void in + + })*/ + + + } + } + + + func fetchDirectionsFrom(from: CLLocationCoordinate2D, to: CLLocationCoordinate2D, completion: ((String?) -> Void)) -> () + { + let urlString = "https://maps.googleapis.com/maps/api/directions/json" + let parameters:[String:AnyObject] = [ + "key" : self.apiKey, + "origin" : [String(stringInterpolationSegment: from.latitude),String(stringInterpolationSegment: from.longitude)], + "destination" : [String(stringInterpolationSegment: to.latitude),String(stringInterpolationSegment: to.longitude)], + "mode" : "walking" + ] + UIApplication.sharedApplication().networkActivityIndicatorVisible = true + + Alamofire.request(.GET, urlString, parameters: parameters, encoding: .URL).responseJSON{ + (req, res, json, error) in + UIApplication.sharedApplication().networkActivityIndicatorVisible = false + if error != nil { + println(error) + } else { + + let myJSON = JSON(json!) + self.steps = myJSON["routes"][0]["legs"][0]["steps"].arrayValue + + NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in + if let encodePath = myJSON["routes"][0]["overview_polyline"]["points"].string { + var path:GMSPath = GMSPath(fromEncodedPath: encodePath) + self.polyline = GMSPolyline(path: path) + self.polyline.strokeWidth = 5 + self.polyline.strokeColor = UIColor.blueColor() + self.polyline.map = self.gMap + } + }) + } + } + } +} diff --git a/adam.xcodeproj/project.pbxproj b/adam.xcodeproj/project.pbxproj index bf931e2..f291d38 100644 --- a/adam.xcodeproj/project.pbxproj +++ b/adam.xcodeproj/project.pbxproj @@ -7,9 +7,11 @@ objects = { /* Begin PBXBuildFile section */ + 213997341B3907580027AB55 /* MenuDetailViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 213997331B3907580027AB55 /* MenuDetailViewController.swift */; }; 2150F5011B0EFEE200AFDF5E /* ProfileViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2150F5001B0EFEE200AFDF5E /* ProfileViewController.swift */; }; 21577E061B2E875000D4218F /* CurrencyConverter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21577E051B2E875000D4218F /* CurrencyConverter.swift */; }; 21577E081B2E87DB00D4218F /* CurrencyList.plist in Resources */ = {isa = PBXBuildFile; fileRef = 21577E071B2E87DB00D4218F /* CurrencyList.plist */; }; + 21A29D341B3A6166009E0D0C /* MapViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21A29D331B3A6166009E0D0C /* MapViewController.swift */; }; 21AF14871B05FA0B00108190 /* RestaurantServerAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21AF14861B05FA0B00108190 /* RestaurantServerAPI.swift */; }; 21AF14891B0601C800108190 /* Restaurant.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21AF14881B0601C800108190 /* Restaurant.swift */; }; 21BF4D171B099714001CDBC3 /* RestaurantCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21BF4D161B099714001CDBC3 /* RestaurantCache.swift */; }; @@ -55,10 +57,12 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 213997331B3907580027AB55 /* MenuDetailViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MenuDetailViewController.swift; sourceTree = ""; }; 2150F4FB1B0ED81C00AFDF5E /* Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Bridging-Header.h"; sourceTree = ""; }; 2150F5001B0EFEE200AFDF5E /* ProfileViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ProfileViewController.swift; sourceTree = ""; }; 21577E051B2E875000D4218F /* CurrencyConverter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CurrencyConverter.swift; sourceTree = ""; }; 21577E071B2E87DB00D4218F /* CurrencyList.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = CurrencyList.plist; sourceTree = ""; }; + 21A29D331B3A6166009E0D0C /* MapViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = MapViewController.swift; path = ../MapViewController.swift; sourceTree = ""; }; 21AF14861B05FA0B00108190 /* RestaurantServerAPI.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RestaurantServerAPI.swift; sourceTree = ""; }; 21AF14881B0601C800108190 /* Restaurant.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Restaurant.swift; sourceTree = ""; }; 21BF4D161B099714001CDBC3 /* RestaurantCache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RestaurantCache.swift; sourceTree = ""; }; @@ -137,6 +141,14 @@ name = Frameworks; sourceTree = ""; }; + 213997321B3907160027AB55 /* Menu Detail */ = { + isa = PBXGroup; + children = ( + 213997331B3907580027AB55 /* MenuDetailViewController.swift */, + ); + name = "Menu Detail"; + sourceTree = ""; + }; 4730F9DECD23A478A1E3060B /* Pods */ = { isa = PBXGroup; children = ( @@ -169,6 +181,7 @@ 9656AD9B1AECC62800933A8E /* benri */ = { isa = PBXGroup; children = ( + 21A29D331B3A6166009E0D0C /* MapViewController.swift */, 96E377191B35981100229684 /* Localizable.strings */, 21D741271B21A9A9008A8EB5 /* DiscoverView.xib */, 96DA5A191AF28AAA0003E506 /* SVAPI */, @@ -235,6 +248,7 @@ 96DA5A131AF284A80003E506 /* Menu */ = { isa = PBXGroup; children = ( + 213997321B3907160027AB55 /* Menu Detail */, 96DA5A141AF284BB0003E506 /* Menu.swift */, 96DA5A161AF2850E0003E506 /* MenuCell.swift */, 21577E051B2E875000D4218F /* CurrencyConverter.swift */, @@ -418,6 +432,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 213997341B3907580027AB55 /* MenuDetailViewController.swift in Sources */, 964690861B3308D800DB187D /* TutorialViewController.swift in Sources */, 21AF14891B0601C800108190 /* Restaurant.swift in Sources */, 967867701B2C9A7F0047C7BB /* SearchCell.swift in Sources */, @@ -425,6 +440,7 @@ 96DA5A0F1AF274670003E506 /* Const.swift in Sources */, 21BF4D171B099714001CDBC3 /* RestaurantCache.swift in Sources */, 96DA5A171AF2850E0003E506 /* MenuCell.swift in Sources */, + 21A29D341B3A6166009E0D0C /* MapViewController.swift in Sources */, 96DA5A151AF284BB0003E506 /* Menu.swift in Sources */, 9656AD9F1AECC62800933A8E /* AppDelegate.swift in Sources */, 9646908A1B3392F600DB187D /* PageContentViewController.swift in Sources */, diff --git a/benri/Base.lproj/Main.storyboard b/benri/Base.lproj/Main.storyboard index e0aa771..d6591b7 100644 --- a/benri/Base.lproj/Main.storyboard +++ b/benri/Base.lproj/Main.storyboard @@ -31,7 +31,7 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -592,7 +1272,7 @@ - +