How to draw a line in arkit based on CLLocation in iOS Swift?
I am trying to use ARKit
for outdoor navigation. I have used mapbox
for AR navigation and used mapboxarkit
and mapboxDirection
cocoapods.
The nodes are shown as paths. I am able to show paths as dotted shperes or planes but am not able show them as lines. Here are my output screenshots
This is the node, which I have tried to show as a line breaking and tried to adjust scnbox
to be straight and show as a line:
.
This is the result I expect:
Here is the code I have tried so far - Here is the code to add an AR annotation and AR line.
if let route = routes?.first, let leg = route.legs.first {
var polyline = [CLLocationCoordinate2D]()
// Add an AR node and map view annotation for every defined "step" in the route
for step in leg.steps {
let coordinate = step.coordinates!.first!
polyline.append(coordinate)
let stepLocation = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
// Update feature collection for map view
self.updateShapeCollectionFeature(&self.waypointShapeCollectionFeature, with: stepLocation, typeKey: "waypoint-type", typeAttribute: "big")
// Add an AR node
let annotation = Annotation(location: stepLocation, calloutImage: self.calloutImage(for: step.description))
annotationsToAdd.append(annotation)
}
let metersPerNode: CLLocationDistance = 1
let turfPolyline = Polyline(polyline)
// Walk the route line and add a small AR node and map view annotation every metersPerNode
for i in stride(from: metersPerNode, to: turfPolyline.distance() - metersPerNode, by: metersPerNode) {
// Use Turf to find the coordinate of each incremented distance along the polyline
if let nextCoordinate = turfPolyline.coordinateFromStart(distance: i) {
let interpolatedStepLocation = CLLocation(latitude: nextCoordinate.latitude, longitude: nextCoordinate.longitude)
// Update feature collection for map view
self.updateShapeCollectionFeature(&self.waypointShapeCollectionFeature, with: interpolatedStepLocation, typeKey: "waypoint-type", typeAttribute: "small")
// Add an AR node
let annotation = Annotation(location: interpolatedStepLocation, calloutImage: nil)
annotationsToAdd.append(annotation)
}
}
}
Here is the code for creating a scnplane
:
func createLightBulbNode() -> SCNNode {
let lightBulbNode = collada2SCNNode(filepath: "art.scnassets/cubeSolid.dae")
lightBulbNode.position = SCNVector3(0, -0.5, 0)
return lightBulbNode
}
func collada2SCNNode(filepath:String) -> SCNNode {
let node = SCNNode()
let scene = SCNScene(named: filepath, inDirectory: nil, options: [SCNSceneSource.LoadingOption.animationImportPolicy: SCNSceneSource.AnimationImportPolicy.doNotPlay])
let nodeArray = scene!.rootNode.childNodes
for childNode in nodeArray {
node.addChildNode(childNode as SCNNode)
}
return node
}
func node(for annotation: Annotation) -> SCNNode? {
if annotation.calloutImage == nil {
// Comment `createLightBulbNode` and add `return nil` to use the default node
return createLightBulbNode()
} else {
let firstColor = UIColor(red: 0.0, green: 99/255.0, blue: 175/255.0, alpha: 1.0)
return createSphereNode(with: 0.5, firstColor: firstColor, secondColor: UIColor.green)
}
}
ios swift mapbox arkit
add a comment |
I am trying to use ARKit
for outdoor navigation. I have used mapbox
for AR navigation and used mapboxarkit
and mapboxDirection
cocoapods.
The nodes are shown as paths. I am able to show paths as dotted shperes or planes but am not able show them as lines. Here are my output screenshots
This is the node, which I have tried to show as a line breaking and tried to adjust scnbox
to be straight and show as a line:
.
This is the result I expect:
Here is the code I have tried so far - Here is the code to add an AR annotation and AR line.
if let route = routes?.first, let leg = route.legs.first {
var polyline = [CLLocationCoordinate2D]()
// Add an AR node and map view annotation for every defined "step" in the route
for step in leg.steps {
let coordinate = step.coordinates!.first!
polyline.append(coordinate)
let stepLocation = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
// Update feature collection for map view
self.updateShapeCollectionFeature(&self.waypointShapeCollectionFeature, with: stepLocation, typeKey: "waypoint-type", typeAttribute: "big")
// Add an AR node
let annotation = Annotation(location: stepLocation, calloutImage: self.calloutImage(for: step.description))
annotationsToAdd.append(annotation)
}
let metersPerNode: CLLocationDistance = 1
let turfPolyline = Polyline(polyline)
// Walk the route line and add a small AR node and map view annotation every metersPerNode
for i in stride(from: metersPerNode, to: turfPolyline.distance() - metersPerNode, by: metersPerNode) {
// Use Turf to find the coordinate of each incremented distance along the polyline
if let nextCoordinate = turfPolyline.coordinateFromStart(distance: i) {
let interpolatedStepLocation = CLLocation(latitude: nextCoordinate.latitude, longitude: nextCoordinate.longitude)
// Update feature collection for map view
self.updateShapeCollectionFeature(&self.waypointShapeCollectionFeature, with: interpolatedStepLocation, typeKey: "waypoint-type", typeAttribute: "small")
// Add an AR node
let annotation = Annotation(location: interpolatedStepLocation, calloutImage: nil)
annotationsToAdd.append(annotation)
}
}
}
Here is the code for creating a scnplane
:
func createLightBulbNode() -> SCNNode {
let lightBulbNode = collada2SCNNode(filepath: "art.scnassets/cubeSolid.dae")
lightBulbNode.position = SCNVector3(0, -0.5, 0)
return lightBulbNode
}
func collada2SCNNode(filepath:String) -> SCNNode {
let node = SCNNode()
let scene = SCNScene(named: filepath, inDirectory: nil, options: [SCNSceneSource.LoadingOption.animationImportPolicy: SCNSceneSource.AnimationImportPolicy.doNotPlay])
let nodeArray = scene!.rootNode.childNodes
for childNode in nodeArray {
node.addChildNode(childNode as SCNNode)
}
return node
}
func node(for annotation: Annotation) -> SCNNode? {
if annotation.calloutImage == nil {
// Comment `createLightBulbNode` and add `return nil` to use the default node
return createLightBulbNode()
} else {
let firstColor = UIColor(red: 0.0, green: 99/255.0, blue: 175/255.0, alpha: 1.0)
return createSphereNode(with: 0.5, firstColor: firstColor, secondColor: UIColor.green)
}
}
ios swift mapbox arkit
add a comment |
I am trying to use ARKit
for outdoor navigation. I have used mapbox
for AR navigation and used mapboxarkit
and mapboxDirection
cocoapods.
The nodes are shown as paths. I am able to show paths as dotted shperes or planes but am not able show them as lines. Here are my output screenshots
This is the node, which I have tried to show as a line breaking and tried to adjust scnbox
to be straight and show as a line:
.
This is the result I expect:
Here is the code I have tried so far - Here is the code to add an AR annotation and AR line.
if let route = routes?.first, let leg = route.legs.first {
var polyline = [CLLocationCoordinate2D]()
// Add an AR node and map view annotation for every defined "step" in the route
for step in leg.steps {
let coordinate = step.coordinates!.first!
polyline.append(coordinate)
let stepLocation = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
// Update feature collection for map view
self.updateShapeCollectionFeature(&self.waypointShapeCollectionFeature, with: stepLocation, typeKey: "waypoint-type", typeAttribute: "big")
// Add an AR node
let annotation = Annotation(location: stepLocation, calloutImage: self.calloutImage(for: step.description))
annotationsToAdd.append(annotation)
}
let metersPerNode: CLLocationDistance = 1
let turfPolyline = Polyline(polyline)
// Walk the route line and add a small AR node and map view annotation every metersPerNode
for i in stride(from: metersPerNode, to: turfPolyline.distance() - metersPerNode, by: metersPerNode) {
// Use Turf to find the coordinate of each incremented distance along the polyline
if let nextCoordinate = turfPolyline.coordinateFromStart(distance: i) {
let interpolatedStepLocation = CLLocation(latitude: nextCoordinate.latitude, longitude: nextCoordinate.longitude)
// Update feature collection for map view
self.updateShapeCollectionFeature(&self.waypointShapeCollectionFeature, with: interpolatedStepLocation, typeKey: "waypoint-type", typeAttribute: "small")
// Add an AR node
let annotation = Annotation(location: interpolatedStepLocation, calloutImage: nil)
annotationsToAdd.append(annotation)
}
}
}
Here is the code for creating a scnplane
:
func createLightBulbNode() -> SCNNode {
let lightBulbNode = collada2SCNNode(filepath: "art.scnassets/cubeSolid.dae")
lightBulbNode.position = SCNVector3(0, -0.5, 0)
return lightBulbNode
}
func collada2SCNNode(filepath:String) -> SCNNode {
let node = SCNNode()
let scene = SCNScene(named: filepath, inDirectory: nil, options: [SCNSceneSource.LoadingOption.animationImportPolicy: SCNSceneSource.AnimationImportPolicy.doNotPlay])
let nodeArray = scene!.rootNode.childNodes
for childNode in nodeArray {
node.addChildNode(childNode as SCNNode)
}
return node
}
func node(for annotation: Annotation) -> SCNNode? {
if annotation.calloutImage == nil {
// Comment `createLightBulbNode` and add `return nil` to use the default node
return createLightBulbNode()
} else {
let firstColor = UIColor(red: 0.0, green: 99/255.0, blue: 175/255.0, alpha: 1.0)
return createSphereNode(with: 0.5, firstColor: firstColor, secondColor: UIColor.green)
}
}
ios swift mapbox arkit
I am trying to use ARKit
for outdoor navigation. I have used mapbox
for AR navigation and used mapboxarkit
and mapboxDirection
cocoapods.
The nodes are shown as paths. I am able to show paths as dotted shperes or planes but am not able show them as lines. Here are my output screenshots
This is the node, which I have tried to show as a line breaking and tried to adjust scnbox
to be straight and show as a line:
.
This is the result I expect:
Here is the code I have tried so far - Here is the code to add an AR annotation and AR line.
if let route = routes?.first, let leg = route.legs.first {
var polyline = [CLLocationCoordinate2D]()
// Add an AR node and map view annotation for every defined "step" in the route
for step in leg.steps {
let coordinate = step.coordinates!.first!
polyline.append(coordinate)
let stepLocation = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
// Update feature collection for map view
self.updateShapeCollectionFeature(&self.waypointShapeCollectionFeature, with: stepLocation, typeKey: "waypoint-type", typeAttribute: "big")
// Add an AR node
let annotation = Annotation(location: stepLocation, calloutImage: self.calloutImage(for: step.description))
annotationsToAdd.append(annotation)
}
let metersPerNode: CLLocationDistance = 1
let turfPolyline = Polyline(polyline)
// Walk the route line and add a small AR node and map view annotation every metersPerNode
for i in stride(from: metersPerNode, to: turfPolyline.distance() - metersPerNode, by: metersPerNode) {
// Use Turf to find the coordinate of each incremented distance along the polyline
if let nextCoordinate = turfPolyline.coordinateFromStart(distance: i) {
let interpolatedStepLocation = CLLocation(latitude: nextCoordinate.latitude, longitude: nextCoordinate.longitude)
// Update feature collection for map view
self.updateShapeCollectionFeature(&self.waypointShapeCollectionFeature, with: interpolatedStepLocation, typeKey: "waypoint-type", typeAttribute: "small")
// Add an AR node
let annotation = Annotation(location: interpolatedStepLocation, calloutImage: nil)
annotationsToAdd.append(annotation)
}
}
}
Here is the code for creating a scnplane
:
func createLightBulbNode() -> SCNNode {
let lightBulbNode = collada2SCNNode(filepath: "art.scnassets/cubeSolid.dae")
lightBulbNode.position = SCNVector3(0, -0.5, 0)
return lightBulbNode
}
func collada2SCNNode(filepath:String) -> SCNNode {
let node = SCNNode()
let scene = SCNScene(named: filepath, inDirectory: nil, options: [SCNSceneSource.LoadingOption.animationImportPolicy: SCNSceneSource.AnimationImportPolicy.doNotPlay])
let nodeArray = scene!.rootNode.childNodes
for childNode in nodeArray {
node.addChildNode(childNode as SCNNode)
}
return node
}
func node(for annotation: Annotation) -> SCNNode? {
if annotation.calloutImage == nil {
// Comment `createLightBulbNode` and add `return nil` to use the default node
return createLightBulbNode()
} else {
let firstColor = UIColor(red: 0.0, green: 99/255.0, blue: 175/255.0, alpha: 1.0)
return createSphereNode(with: 0.5, firstColor: firstColor, secondColor: UIColor.green)
}
}
ios swift mapbox arkit
ios swift mapbox arkit
edited Dec 3 '18 at 14:34
Dávid Pásztor
22.4k82849
22.4k82849
asked Nov 20 '18 at 17:24
Parameswaran VParameswaran V
262
262
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53398325%2fhow-to-draw-a-line-in-arkit-based-on-cllocation-in-ios-swift%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53398325%2fhow-to-draw-a-line-in-arkit-based-on-cllocation-in-ios-swift%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown