Created
April 26, 2017 13:16
-
-
Save KordianKL/9dfa0cb47958bee45338c92a51697ceb to your computer and use it in GitHub Desktop.
Example of using HDAugumentedReality with iOS (Swift)
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
// | |
// Created by Kordian Ledzion on 15.03.2017. | |
// Copyright © 2017 Wolf Development. All rights reserved. | |
// | |
// Part of the code for HDAugumentedReality usage in one of my projects. | |
// It's showing AR pins with name and distance to locations loaded | |
import UIKit | |
import CoreLocation | |
class CameraViewController: UIViewController { | |
fileprivate var arViewController: ARViewController! | |
var annotations = [ARAnnotation]() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
let places = DataAccess.access.getPlaces() //Fetching all the Places in the app | |
//Converting Places to AR-friendly model | |
for place in places{ | |
let annotation = ARModel(location: CLLocation(latitude: place.latitude, longitude: place.longitude), name: place.name) | |
annotations.append(annotation) | |
} | |
//Setting up AR View | |
arViewController = ARViewController() //Initializing view | |
arViewController.dataSource = self //Source of data | |
arViewController.maxDistance = 5000 //Max distance from the user | |
arViewController.maxVisibleAnnotations = 30 //Self explanatory | |
arViewController.headingSmoothingFactor = 0.05 | |
arViewController.setAnnotations(annotations) //Where are annotations | |
self.present(arViewController, animated: true, completion: nil) //Show AR View | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
@IBAction func expandArCameraView(_ sender: UIBarButtonItem) { | |
self.present(arViewController, animated: true, completion: nil) | |
} | |
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { | |
if segue.identifier == "showDetail" { | |
if let dest = segue.destination as? DetailViewController, let sender = sender as? AnnotationView { | |
dest.place = DataAccess.access.getPlaceFor(name: sender.titleLabel!.text!) | |
} | |
} | |
} | |
} | |
//Extending CameraViewController by ARDataSource protocol to deliver custom views for annotations in AR view (just like regular annotations on map) | |
extension CameraViewController: ARDataSource{ | |
func ar(_ arViewController: ARViewController, viewForAnnotation: ARAnnotation) -> ARAnnotationView { | |
let annotationView = AnnotationView() | |
annotationView.annotation = viewForAnnotation | |
annotationView.delegate = self | |
annotationView.frame = CGRect(x: 0, y: 0, width: 150, height: 50) | |
return annotationView | |
} | |
} | |
//Extending CameraViewController by AnnotationViewDelegate to define action on touching the annotation in AR View | |
extension CameraViewController: AnnotationViewDelegate { | |
func didTouch(annotationView: AnnotationView) { | |
performSegue(withIdentifier: "showDetail", sender: annotationView) | |
arViewController.closeButtonTap() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment