Skip to content

Instantly share code, notes, and snippets.

@aodhol
Last active August 7, 2017 19:07
Show Gist options
  • Save aodhol/c4490cf2063628bf8fb96c637caa43e9 to your computer and use it in GitHub Desktop.
Save aodhol/c4490cf2063628bf8fb96c637caa43e9 to your computer and use it in GitHub Desktop.
import Mapbox
// MGLPointAnnotation subclass
class MyCustomPointAnnotation: MGLPointAnnotation {
var willUseImage: Bool = false
}
class MyCustomPointAnnotationView: MGLAnnotationView {
static let reuseIdentifier = String(describing: MyCustomPointAnnotationView.self)
let imageView: UIImageView
init(with annotation: MGLAnnotation, reuseIdentifier: String?) {
imageView = UIImageView(image: UIImage(named: "Pin_Small"))
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
imageView.translatesAutoresizingMaskIntoConstraints = false
addSubview(imageView)
// frame = CGRect(x: 0, y: 0, width: imageView.frame.width, height: imageView.frame.height)
imageView.topAnchor.constraint(equalTo: topAnchor).isActive = true
imageView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
imageView.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
imageView.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
layer.borderColor = UIColor.red.cgColor
layer.borderWidth = 1
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
centerOffset = CGVector(dx: 0, dy: -(imageView.frame.maxY / 2))
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
imageView.image = selected ? UIImage(named: "Pin_Large")! : UIImage(named: "Pin_Small")!
// imageView.sizeToFit()
// centerOffset = CGVector(dx: 0, dy: -(imageView.frame.maxY / 2))
// let currentFrame = frame
// frame = CGRect(x: currentFrame.origin.x, y: currentFrame.origin.y, width: imageView.frame.width, height: imageView.frame.height)
}
}
// end MGLPointAnnotation subclass
@objc(AnnotationViewMultipleExample_Swift)
class AnnotationViewMultipleExample_Swift: UIViewController, MGLMapViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Create a new map view using the Mapbox Light style.
let mapView = MGLMapView(frame: view.bounds, styleURL: MGLStyle.lightStyleURL())
mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
// Set the map’s center coordinate and zoom level.
mapView.setCenter(CLLocationCoordinate2D(latitude: 36.54, longitude: -116.97), zoomLevel: 9, animated: false)
view.addSubview(mapView)
mapView.delegate = self
// Create four new point annotations with specified coordinates and titles.
let pointA = MyCustomPointAnnotation()
pointA.coordinate = CLLocationCoordinate2D(latitude: 36.4623, longitude: -116.8656)
pointA.title = "Stovepipe Wells"
pointA.willUseImage = true
let pointB = MyCustomPointAnnotation()
pointB.coordinate = CLLocationCoordinate2D(latitude: 36.6071, longitude: -117.1458)
pointB.title = "Furnace Creek"
pointB.willUseImage = true
let pointC = MyCustomPointAnnotation()
pointC.title = "Zabriskie Point"
pointC.coordinate = CLLocationCoordinate2D(latitude: 36.4208, longitude: -116.8101)
let pointD = MyCustomPointAnnotation()
pointD.title = "Mesquite Flat Sand Dunes"
pointD.coordinate = CLLocationCoordinate2D(latitude: 36.6836, longitude: -117.1005)
// Fill an array with four point annotations.
let myPlaces = [pointA, pointB, pointC, pointD]
// Add all annotations to the map all at once, instead of individually.
mapView.addAnnotations(myPlaces)
}
// This delegate method is where you tell the map to load a view for a specific annotation based on the willUseImage property of the custom subclass.
func mapView(_ mapView: MGLMapView, viewFor annotation: MGLAnnotation) -> MGLAnnotationView? {
if let castAnnotation = annotation as? MyCustomPointAnnotation {
if (castAnnotation.willUseImage) {
return nil;
}
}
// For better performance, always try to reuse existing annotations.
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: MyCustomPointAnnotationView.reuseIdentifier)
// If there’s no reusable annotation view available, initialize a new one.
if annotationView == nil {
annotationView = MyCustomPointAnnotationView(with: annotation, reuseIdentifier: MyCustomPointAnnotationView.reuseIdentifier)
annotationView?.translatesAutoresizingMaskIntoConstraints = false
}
return annotationView
}
func mapView(_ mapView: MGLMapView, didSelect annotationView: MGLAnnotationView) {
annotationView.setSelected(true, animated: true)
}
func mapView(_ mapView: MGLMapView, didDeselect annotationView: MGLAnnotationView) {
annotationView.setSelected(false, animated: true)
}
// This delegate method is where you tell the map to load an image for a specific annotation based on the willUseImage property of the custom subclass.
func mapView(_ mapView: MGLMapView, imageFor annotation: MGLAnnotation) -> MGLAnnotationImage? {
if let castAnnotation = annotation as? MyCustomPointAnnotation {
if (!castAnnotation.willUseImage) {
return nil;
}
}
// For better performance, always try to reuse existing annotations.
var annotationImage = mapView.dequeueReusableAnnotationImage(withIdentifier: "camera")
// If there is no reusable annotation image available, initialize a new one.
if(annotationImage == nil) {
annotationImage = MGLAnnotationImage(image: UIImage(named: "camera")!, reuseIdentifier: "camera")
}
return annotationImage
}
func mapView(_ mapView: MGLMapView, annotationCanShowCallout annotation: MGLAnnotation) -> Bool {
// Always allow callouts to popup when annotations are tapped.
return false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment