-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NEW FEATURE] ✨ Add map view to show location of restaurants
- Loading branch information
Showing
7 changed files
with
206 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+3.87 KB
(110%)
...odeproj/project.xcworkspace/xcuserdata/william.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters