Last active
November 9, 2017 12:49
-
-
Save rodydavis/1d17bafa987bc848da493c221ebedf2e to your computer and use it in GitHub Desktop.
Address From String Function
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
import UIKit | |
import MapKit | |
class MapViewController: UIViewController, MKMapViewDelegate { | |
@IBOutlet var mapView: MKMapView! | |
var restaurant: RestaurantMO! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
mapView.delegate = self | |
// Convert address to coordinate and annotate it on map | |
let geoCoder = CLGeocoder() | |
geoCoder.geocodeAddressString(restaurant.location ?? "", completionHandler: { placemarks, error in | |
if let error = error { | |
print(error) | |
return | |
} | |
if let placemarks = placemarks { | |
// Get the first placemark | |
let placemark = placemarks[0] | |
// Add annotation | |
let annotation = MKPointAnnotation() | |
annotation.title = self.restaurant.name | |
annotation.subtitle = self.restaurant.type | |
if let location = placemark.location { | |
annotation.coordinate = location.coordinate | |
// Display the annotation | |
self.mapView.showAnnotations([annotation], animated: true) | |
self.mapView.selectAnnotation(annotation, animated: true) | |
} | |
} | |
}) | |
mapView.showsCompass = true | |
mapView.showsTraffic = true | |
mapView.showsScale = true | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
// MARK: - MKMapViewDelegate methods | |
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { | |
let identifier = "MyMarker" | |
if annotation.isKind(of: MKUserLocation.self) { | |
return nil | |
} | |
// Reuse the annotation if possible | |
var annotationView: MKMarkerAnnotationView? = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKMarkerAnnotationView | |
if annotationView == nil { | |
annotationView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: identifier) | |
} | |
annotationView?.glyphText = "😋" | |
annotationView?.markerTintColor = UIColor.orange | |
return annotationView | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment