Skip to content

Commit

Permalink
[NEW FEATURE] ✨ Add map view to show location of restaurants
Browse files Browse the repository at this point in the history
  • Loading branch information
Williano committed Mar 4, 2019
1 parent e45cd0f commit 9777b4d
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 8 deletions.
21 changes: 21 additions & 0 deletions FoodPin.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
3D7C67FF222AC13D002C05C7 /* Restaurant.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3D7C67FE222AC13D002C05C7 /* Restaurant.swift */; };
3D7C6801222AD3CC002C05C7 /* RestaurantDetailTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3D7C6800222AD3CC002C05C7 /* RestaurantDetailTableViewCell.swift */; };
3DD9C3B8222C091700055619 /* ReviewViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3DD9C3B7222C091700055619 /* ReviewViewController.swift */; };
3DD9C3BB222D587E00055619 /* MapKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3DD9C3BA222D587E00055619 /* MapKit.framework */; };
3DD9C3BD222D68AB00055619 /* MapViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3DD9C3BC222D68AB00055619 /* MapViewController.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -32,13 +34,16 @@
3D7C67FE222AC13D002C05C7 /* Restaurant.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Restaurant.swift; sourceTree = "<group>"; };
3D7C6800222AD3CC002C05C7 /* RestaurantDetailTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RestaurantDetailTableViewCell.swift; sourceTree = "<group>"; };
3DD9C3B7222C091700055619 /* ReviewViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReviewViewController.swift; sourceTree = "<group>"; };
3DD9C3BA222D587E00055619 /* MapKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MapKit.framework; path = System/Library/Frameworks/MapKit.framework; sourceTree = SDKROOT; };
3DD9C3BC222D68AB00055619 /* MapViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapViewController.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
3D3C22ED22282C62008FA989 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
3DD9C3BB222D587E00055619 /* MapKit.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -50,6 +55,7 @@
children = (
3D3C22F222282C62008FA989 /* FoodPin */,
3D3C22F122282C62008FA989 /* Products */,
3DD9C3B9222D587E00055619 /* Frameworks */,
);
sourceTree = "<group>";
};
Expand All @@ -75,10 +81,19 @@
3D7C67FE222AC13D002C05C7 /* Restaurant.swift */,
3D7C6800222AD3CC002C05C7 /* RestaurantDetailTableViewCell.swift */,
3DD9C3B7222C091700055619 /* ReviewViewController.swift */,
3DD9C3BC222D68AB00055619 /* MapViewController.swift */,
);
path = FoodPin;
sourceTree = "<group>";
};
3DD9C3B9222D587E00055619 /* Frameworks */ = {
isa = PBXGroup;
children = (
3DD9C3BA222D587E00055619 /* MapKit.framework */,
);
name = Frameworks;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -111,6 +126,11 @@
TargetAttributes = {
3D3C22EF22282C62008FA989 = {
CreatedOnToolsVersion = 10.1;
SystemCapabilities = {
com.apple.Maps.iOS = {
enabled = 1;
};
};
};
};
};
Expand Down Expand Up @@ -157,6 +177,7 @@
3D7C6801222AD3CC002C05C7 /* RestaurantDetailTableViewCell.swift in Sources */,
3D7C67FD222AA77E002C05C7 /* RestaurantDetailViewController.swift in Sources */,
3D3C22F422282C62008FA989 /* AppDelegate.swift in Sources */,
3DD9C3BD222D68AB00055619 /* MapViewController.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Binary file not shown.
40 changes: 38 additions & 2 deletions FoodPin/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
</objects>
<point key="canvasLocation" x="1052" y="158.77061469265368"/>
</scene>
<!--Title-->
<!--Restaurant Detail View Controller-->
<scene sceneID="aHz-4J-PPr">
<objects>
<viewController id="FV5-zo-8q2" customClass="RestaurantDetailViewController" customModule="FoodPin" customModuleProvider="target" sceneMemberID="viewController">
Expand All @@ -109,6 +109,10 @@
<rect key="frame" x="0.0" y="0.0" width="375" height="300"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
</imageView>
<mapView key="tableFooterView" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleAspectFill" mapType="standard" zoomEnabled="NO" scrollEnabled="NO" rotateEnabled="NO" showsTraffic="YES" id="0TD-Ul-kVf">
<rect key="frame" x="0.0" y="364" width="375" height="135"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
</mapView>
<prototypes>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" insetsLayoutMarginsFromSafeArea="NO" selectionStyle="blue" hidesAccessoryWhenEditing="NO" indentationLevel="1" indentationWidth="0.0" reuseIdentifier="Cell" rowHeight="36" id="efR-1v-Omm" customClass="RestaurantDetailTableViewCell" customModule="FoodPin" customModuleProvider="target">
<rect key="frame" x="0.0" y="328" width="375" height="36"/>
Expand Down Expand Up @@ -182,10 +186,12 @@
</constraints>
<viewLayoutGuide key="safeArea" id="fIf-1C-ciW"/>
</view>
<navigationItem key="navigationItem" title="Title" largeTitleDisplayMode="never" id="h9s-Wh-Gy7"/>
<navigationItem key="navigationItem" largeTitleDisplayMode="never" id="2N3-OJ-IdV"/>
<connections>
<outlet property="mapView" destination="0TD-Ul-kVf" id="a5N-mC-Udu"/>
<outlet property="restaurantImageView" destination="Cbx-9s-wq2" id="sCW-L6-dQU"/>
<outlet property="tableView" destination="bYK-qh-Edo" id="yIF-wK-OrC"/>
<segue destination="oTC-LE-Y60" kind="show" identifier="showMap" id="k84-34-3TE"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="rnk-xW-KfO" userLabel="First Responder" sceneMemberID="firstResponder"/>
Expand Down Expand Up @@ -313,6 +319,36 @@
</objects>
<point key="canvasLocation" x="2844" y="158.77061469265368"/>
</scene>
<!--Map View Controller-->
<scene sceneID="J4i-IM-IVE">
<objects>
<viewController id="oTC-LE-Y60" customClass="MapViewController" customModule="FoodPin" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="6BX-Uw-OlW">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<mapView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" mapType="standard" translatesAutoresizingMaskIntoConstraints="NO" id="vcA-qg-ojP">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
</mapView>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstAttribute="trailing" secondItem="vcA-qg-ojP" secondAttribute="trailing" id="Dvv-0F-mov"/>
<constraint firstItem="vcA-qg-ojP" firstAttribute="top" secondItem="6BX-Uw-OlW" secondAttribute="top" id="EZj-0t-H48"/>
<constraint firstItem="vcA-qg-ojP" firstAttribute="bottom" secondItem="1W7-Gk-E99" secondAttribute="bottom" id="lsJ-Rj-I82"/>
<constraint firstItem="vcA-qg-ojP" firstAttribute="leading" secondItem="6BX-Uw-OlW" secondAttribute="leading" id="yv3-es-NZa"/>
</constraints>
<viewLayoutGuide key="safeArea" id="1W7-Gk-E99"/>
</view>
<navigationItem key="navigationItem" largeTitleDisplayMode="never" id="4Vn-3s-3nX"/>
<connections>
<outlet property="mapView" destination="vcA-qg-ojP" id="HQ5-bf-lRv"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="W6r-Ht-v9P" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="2001" y="860"/>
</scene>
<!--Navigation Controller-->
<scene sceneID="5bc-K2-6PV">
<objects>
Expand Down
99 changes: 99 additions & 0 deletions FoodPin/MapViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//
// MapViewController.swift
// FoodPin
//
// Created by William Kpabitey Kwabla on 3/4/19.
// Copyright © 2019 William Kpabitey Kwabla. All rights reserved.
//

import UIKit
import MapKit

class MapViewController: UIViewController {

@IBOutlet var mapView:MKMapView!

var restaurant:Restaurant!

override func viewDidLoad() {
super.viewDidLoad()

mapView.delegate = self
mapView.showsCompass = true
mapView.showsScale = true
mapView.showsTraffic = true
mapView.showsBuildings = true

// Do any additional setup after loading the view.

// Convert address to coordinate and annotate it on map.
let gecoder = CLGeocoder()
gecoder.geocodeAddressString(restaurant.location) { (placemarks, error) in
if let error = error {
print(error)
return
}

guard let placemarks = placemarks else{return}
// Get the first placemark
let placemark = placemarks[0]

// Add annotation
let annotation = MKPointAnnotation()
annotation.title = self.restaurant.name
annotation.subtitle = self.restaurant.type

guard let location = placemark.location else{return}
annotation.coordinate = location.coordinate

// Display the annotation
self.mapView.showAnnotations([annotation], animated: true)
self.mapView.selectAnnotation(annotation, animated: true)



}
}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/

}


extension MapViewController: MKMapViewDelegate {

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let identifier = "MyPin"

if annotation.isKind(of: MKUserLocation.self) {
return nil
}

// Reuse the annotation if possible
var annotationView: MKPinAnnotationView? = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKPinAnnotationView

if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView?.canShowCallout = true
}

let leftIconView = UIImageView(frame: CGRect.init(x: 0, y: 0, width: 53, height: 53))
leftIconView.image = UIImage(named: restaurant.image)
annotationView?.leftCalloutAccessoryView = leftIconView

// Customize pin tint color
annotationView?.pinTintColor = UIColor.orange


return annotationView
}
}
48 changes: 45 additions & 3 deletions FoodPin/RestaurantDetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
//

import UIKit
import MapKit

class RestaurantDetailViewController: UIViewController {
@IBOutlet var restaurantImageView: UIImageView!
@IBOutlet var tableView:UITableView!
@IBOutlet var mapView:MKMapView!

var restaurant:Restaurant!

Expand All @@ -20,13 +22,44 @@ class RestaurantDetailViewController: UIViewController {
// Do any additional setup after loading the view.
title = restaurant.name

// Enable self sizing cells
tableView.estimatedRowHeight = 36.0
tableView.rowHeight = UITableView.automaticDimension

tableView.backgroundColor = UIColor(red: 240.0/255.0, green: 240.0/255.0, blue: 240.0/250.0, alpha: 0.2)
tableView.separatorColor = UIColor(red: 240.0/255.0, green: 240.0/255.0, blue: 240.0/250.0, alpha: 0.8)
tableView.tableFooterView = UIView(frame: CGRect.zero)
// tableView.tableFooterView = UIView(frame: CGRect.zero)
restaurantImageView.image = UIImage(named: restaurant.image)

// Handle tap gesture for the map view
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(showMap))
mapView.addGestureRecognizer(tapGestureRecognizer)


let geoCoder = CLGeocoder()
geoCoder.geocodeAddressString(restaurant.location) { (placemarks, error) in
if let error = error {
print(error)
return
}

guard let placemarks = placemarks else {return}
// Get first placemarker
let placemark = placemarks[0]

// Add anotation
let annotation = MKPointAnnotation()

guard let location = placemark.location else {return}

// Display the annotation
annotation.coordinate = location.coordinate
self.mapView.addAnnotation(annotation)

// Set the zoom level
let region = MKCoordinateRegion.init(center: annotation.coordinate, latitudinalMeters: 250, longitudinalMeters: 250)
self.mapView.setRegion(region, animated: false)
}
}

// override func viewWillAppear(_ animated: Bool) {
Expand All @@ -35,17 +68,26 @@ class RestaurantDetailViewController: UIViewController {
// navigationController?.hidesBarsOnSwipe = false
// navigationController?.setNavigationBarHidden(false, animated: true)
// }

// MARK: - Custom methods
@objc func showMap() {
performSegue(withIdentifier: "showMap", sender: self)
}


// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
if segue.identifier == segue.identifier {
if segue.identifier == "showReview" {
let destinationController = segue.destination as! ReviewViewController
// Pass the selected object to the new view controller.
destinationController.restaurant = restaurant
} else if segue.identifier == "showMap" {
let destinationController = segue.destination as! MapViewController
// Pass the selected object to the new view controller.
destinationController.restaurant = restaurant
}
}

Expand All @@ -71,7 +113,7 @@ class RestaurantDetailViewController: UIViewController {

tableView.reloadData()
}

}


Expand Down
2 changes: 1 addition & 1 deletion FoodPin/RestaurantTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class RestaurantTableViewController: UITableViewController {
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)

tableView.estimatedRowHeight = 80.0
tableView.rowHeight = UITableView.automaticDimension
// tableView.rowHeight = UITableView.automaticDimension

// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
Expand Down
4 changes: 2 additions & 2 deletions FoodPin/ReviewViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ class ReviewViewController: UIViewController {

override func viewDidAppear(_ animated: Bool) {
closeReviewButton.transform = CGAffineTransform(translationX: 1000, y: 0)
UIView.animate(withDuration: 0.3) {
UIView.animate(withDuration: 1.5) {
self.containerView.transform = CGAffineTransform.identity
}

UIView.animate(withDuration: 1.0) {
UIView.animate(withDuration: 2.0) {
self.closeReviewButton.transform = CGAffineTransform.identity
}
// UIView.animate(withDuration: 0.4, delay: 0.0, usingSpringWithDamping: 0.3,
Expand Down

0 comments on commit 9777b4d

Please sign in to comment.