Skip to content

Commit

Permalink
Added map logic to iOS, moving to Apple Watch
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-headspace committed Nov 25, 2018
1 parent f1784cb commit 318f458
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 16 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Musgravite/Controller/DetailViewController.swift"
timestampString = "564818490.101434"
timestampString = "564861121.219802"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "133"
endingLineNumber = "133"
startingLineNumber = "205"
endingLineNumber = "205"
landmarkName = "getData(_:_:_:)"
landmarkType = "7">
</BreakpointContent>
Expand Down Expand Up @@ -42,11 +42,11 @@
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Musgravite/Controller/DetailViewController.swift"
timestampString = "564818490.102037"
timestampString = "564861121.220378"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "134"
endingLineNumber = "134"
startingLineNumber = "206"
endingLineNumber = "206"
landmarkName = "getData(_:_:_:)"
landmarkType = "7">
</BreakpointContent>
Expand Down
80 changes: 76 additions & 4 deletions Musgravite/Controller/DetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ import SVProgressHUD
import Alamofire
import SwiftMessages
import WatchConnectivity
import MapKit

class DetailViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, WCSessionDelegate{
class DetailViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, WCSessionDelegate, MKMapViewDelegate, CLLocationManagerDelegate {
/* WCSessionDelegate */
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {}

func sessionDidBecomeInactive(_ session: WCSession) {}

func sessionDidDeactivate(_ session: WCSession) {}


//Laboratory information
var labInformation:JSON?
var selectedElementURL:URL?
Expand All @@ -36,18 +35,23 @@ class DetailViewController: UIViewController, UICollectionViewDelegate, UICollec
public let selection = UISelectionFeedbackGenerator()

//Static Elements
@IBOutlet weak var mapOutlet: MKMapView!
@IBOutlet weak var locationOutlet: UILabel!
@IBOutlet weak var descriptionOutlet: UITextView!
@IBOutlet weak var bigTitleOutlet: UITextView!
@IBOutlet weak var bigImageOutlet: UIImageView!
@IBOutlet weak var gradientCategory: UIImageView!

//CollectionViews
@IBOutlet weak var imagesCollectionView: UICollectionView!
@IBOutlet weak var videoCollectionView: UICollectionView!
@IBOutlet weak var modelCollectionView: UICollectionView!
@IBOutlet weak var button360: UIButton!
@IBOutlet weak var button3D: UIButton!

// CLLocationManager
let locationManager = CLLocationManager()

@IBAction func SwapMode(_ sender: Any) {
if(button3D.isHidden == true){
button3D.isHidden = false
Expand All @@ -68,7 +72,73 @@ class DetailViewController: UIViewController, UICollectionViewDelegate, UICollec
wcSession.delegate = self
wcSession.activate()
button3D.isHidden = true
/* MapKit Delegate */
mapOutlet.delegate = self
mapOutlet.showsPointsOfInterest = true
mapOutlet.showsScale = true
mapOutlet.showsUserLocation = true
displayMapDirections()
}

func displayMapDirections(){
/* Request Location */
locationManager.requestAlwaysAuthorization()
locationManager.requestWhenInUseAuthorization()

if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}

let sourceCoordinates = locationManager.location?.coordinate
let destinationCoordinates = CLLocationCoordinate2D(latitude: 19.2833333, longitude: -99.1352777777779)
let sourcePlacemark = MKPlacemark(coordinate: sourceCoordinates!)
let destinationPlacemark = MKPlacemark(coordinate: destinationCoordinates)
let sourceItem = MKMapItem(placemark: sourcePlacemark)
let destinationItem = MKMapItem(placemark: destinationPlacemark)
let directionRequest = MKDirections.Request()
directionRequest.source = sourceItem
directionRequest.destination = destinationItem
directionRequest.transportType = .walking
let directions = MKDirections(request: directionRequest)
directions.calculate(completionHandler: {response, error in
guard let response = response else {
if let error = error {
print("omg something went wrong")
}
return
}

let route = response.routes[0]
self.mapOutlet.addOverlay(route.polyline, level: .aboveRoads)
let rekt = route.polyline.boundingMapRect
self.mapOutlet.setRegion(MKCoordinateRegion(rekt), animated: true)
})
}

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolylineRenderer(overlay: overlay)
renderer.strokeColor = UIColor.blue
renderer.lineWidth = 5.0
return renderer
}

@IBAction func sendToAppleMaps(_ sender: Any) {
let sourceCoordinates = locationManager.location?.coordinate
let destinationCoordinates = CLLocationCoordinate2D(latitude: 19.2833333, longitude: -99.1352777777779)
let directionsURL = "http://maps.apple.com/?saddr=\(sourceCoordinates?.latitude ?? 0),\(sourceCoordinates?.longitude ?? 0)&daddr=\(destinationCoordinates.latitude),\(destinationCoordinates.longitude)"
guard let url = URL(string: directionsURL) else {
return
}
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
}


@IBAction func sendToWatch(_ sender: Any) {
sendLabInformation(labInformation!)
}
Expand Down Expand Up @@ -123,6 +193,8 @@ class DetailViewController: UIViewController, UICollectionViewDelegate, UICollec
bigTitleOutlet.text = labInformation!["nombre"].stringValue
/* To be changed once this information is available */
bigImageOutlet.sd_setImage(with: URL(string: labInformation!["PosterImage"].stringValue),placeholderImage: UIImage(named: "grad0"))
/* Create map value */

}

/* Downloads the required data from an URL */
Expand Down
8 changes: 7 additions & 1 deletion Musgravite/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSLocationUsageDescription</key>
<string>$(PRODUCT_NAME) desea saber tu localización</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>$(PRODUCT_NAME) desea saber tu localización</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>$(PRODUCT_NAME) desea saber tu localización</string>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
Expand Down Expand Up @@ -30,7 +36,7 @@
<key>NSCameraUsageDescription</key>
<string>$(PRODUCT_NAME) desea usar tu cámara para mostrar mundos virtuales.</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>$(PRODUCT_NAME) desea saber tu localización</string>
<string>$(PRODUCT_NAME) desea saber tu localización incluso cuando la aplicacion no este en uso.</string>
<key>NSPhotoLibraryAddUsageDescription</key>
<string>$(PRODUCT_NAME) desea acceder a tu carrete para escoger una imagen</string>
<key>UILaunchStoryboardName</key>
Expand Down
24 changes: 19 additions & 5 deletions Musgravite/View/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -492,14 +492,14 @@
<objects>
<viewController id="uOn-dU-ibC" customClass="DetailViewController" customModule="Musgravite" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="Oy9-uG-ylQ">
<rect key="frame" x="0.0" y="0.0" width="375" height="1900"/>
<rect key="frame" x="0.0" y="0.0" width="375" height="1950"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<scrollView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="tI8-h9-M4r">
<rect key="frame" x="0.0" y="88" width="375" height="1778"/>
<rect key="frame" x="0.0" y="88" width="375" height="1828"/>
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="F9f-eU-zWf">
<rect key="frame" x="0.0" y="0.0" width="375" height="1800"/>
<rect key="frame" x="0.0" y="0.0" width="375" height="1850"/>
<subviews>
<imageView userInteractionEnabled="NO" contentMode="scaleAspectFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" image="lab-default" translatesAutoresizingMaskIntoConstraints="NO" id="ghk-5p-d4h">
<rect key="frame" x="0.0" y="0.0" width="375" height="296"/>
Expand Down Expand Up @@ -798,6 +798,9 @@
<mapView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" fixedFrame="YES" mapType="standard" translatesAutoresizingMaskIntoConstraints="NO" id="WR3-fo-K6O">
<rect key="frame" x="12" y="1375" width="354" height="386"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<connections>
<outlet property="delegate" destination="uOn-dU-ibC" id="Nd0-u9-vm0"/>
</connections>
</mapView>
<view contentMode="scaleToFill" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="gSm-uV-OTb">
<rect key="frame" x="10" y="1319" width="358" height="39"/>
Expand All @@ -818,10 +821,20 @@
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</view>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="fKD-ZJ-KzG">
<rect key="frame" x="117" y="1779" width="140" height="30"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<state key="normal" title="Abrir en Apple Maps">
<color key="titleColor" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</state>
<connections>
<action selector="sendToAppleMaps:" destination="uOn-dU-ibC" eventType="touchUpInside" id="036-UF-3pI"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" red="0.97521414974619292" green="0.97521414974619292" blue="0.97521414974619292" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstAttribute="height" constant="1800" id="IoP-5d-qVj"/>
<constraint firstAttribute="height" constant="1850" id="IoP-5d-qVj"/>
</constraints>
</view>
</subviews>
Expand All @@ -843,7 +856,7 @@
</constraints>
<viewLayoutGuide key="safeArea" id="kfW-sX-LZN"/>
</view>
<size key="freeformSize" width="375" height="1900"/>
<size key="freeformSize" width="375" height="1950"/>
<connections>
<outlet property="bigImageOutlet" destination="ghk-5p-d4h" id="EUX-3g-cLV"/>
<outlet property="bigTitleOutlet" destination="oWc-yO-EJY" id="RPv-76-RpI"/>
Expand All @@ -852,6 +865,7 @@
<outlet property="descriptionOutlet" destination="HF7-KG-ouB" id="Wp2-Oe-Vsl"/>
<outlet property="imagesCollectionView" destination="8TW-Zf-lF7" id="oFn-b6-etJ"/>
<outlet property="locationOutlet" destination="3F9-Ql-c7I" id="Ddc-UW-Sep"/>
<outlet property="mapOutlet" destination="WR3-fo-K6O" id="YBa-1K-gTP"/>
<outlet property="modelCollectionView" destination="q00-yE-fR5" id="rSd-Sx-udA"/>
<outlet property="videoCollectionView" destination="Ijw-yQ-9WS" id="YAD-K9-24U"/>
<segue destination="SON-WU-SIL" kind="show" identifier="VideoViewController" id="hYp-ZW-713"/>
Expand Down

0 comments on commit 318f458

Please sign in to comment.