Skip to content

Instantly share code, notes, and snippets.

@morhpeus87
Created June 26, 2018 14:12
Show Gist options
  • Save morhpeus87/f64b6598632dc959abb970e7f21c68c0 to your computer and use it in GitHub Desktop.
Save morhpeus87/f64b6598632dc959abb970e7f21c68c0 to your computer and use it in GitHub Desktop.
private CompletableFuture[] completableFuturesArray;
protected void onCreate(Bundle savedInstanceState) {
Log.d(LOGTAG, "in onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
try { //Load all markers data
Log.d(LOGTAG, "getting location sources...");
String file = Utils.openFile(getApplicationContext(), R.raw.location_source);
locationSourceList = Utils.toJson(file);
Log.d(LOGTAG, "getting location sources done");
} catch (IOException e) {
e.printStackTrace();
}
completableFutureHashMap = new HashMap<>();
completableFuturesArray = new CompletableFuture[locationSourceList.size()];
for (int i = 0; i < locationSourceList.size(); i++) {
LocationSource locationSource = locationSourceList.get(i);
CompletableFuture<ViewRenderable> completableFuture = ViewRenderable.builder()
.setView(this, R.layout.marker_layout)
.build();
completableFutureHashMap.put(locationSource.getName(), completableFuture);
completableFuturesArray[i] = completableFuture;
}
CompletableFuture
.allOf(completableFuturesArray)
.handle(
(notUsed, throwable) -> {
if (throwable != null) {
DemoUtils.displayError(this, "Unable to load renderables", throwable);
return null;
}
hasFinishedLoading = true;
return null;
}
);
// Set an update listener on the Scene that will hide the loading message once a Plane is
// detected.
arSceneView
.getScene()
.setOnUpdateListener(
frameTime -> {
if (!hasFinishedLoading) {
return;
}
if (locationScene == null) {
locationScene = new LocationScene(this, this, arSceneView);
locationScene.setAnchorRefreshInterval(ANCHOR_REFRESH_INTERVAL);
for(LocationSource locationSource : locationSourceList){
LocationMarker marker = new LocationMarker(locationSource.getLng(), locationSource.getLat(), locationSource.getName(), new Node());
CompletableFuture<ViewRenderable> completableFuture = completableFutureHashMap.get(locationSource.getName());
try {
ViewRenderable viewRenderable = completableFuture.get();
marker.node.setRenderable(viewRenderable);
marker.node.setOnTapListener((v, event) -> Toast.makeText(this, locationSource.getName()+" - distance "+marker.anchorNode.getDistance() + " meters", Toast.LENGTH_LONG).show());
marker.setOnlyRenderWhenWithin(ONLY_RENDER_WHEN_WITHIN);
TextView nameTextView = viewRenderable.getView().findViewById(R.id.txtv_marker_location_name);
nameTextView.setText(n.getLocationMarker().name);
marker.setRenderEvent(n -> {
TextView distanceTextView = viewRenderable.getView().findViewById(R.id.txtv_marker_location_distance);
distanceTextView.setText(getString(R.string.location_distance, n.getDistance()));
});
locationScene.mLocationMarkers.add(marker);
}catch (InterruptedException | ExecutionException ex) {
DemoUtils.displayError(this, "Unable to load renderables", ex);
}
}
}
Frame frame = arSceneView.getArFrame();
if (frame == null) {
return;
}
if (frame.getCamera().getTrackingState() != TrackingState.TRACKING) {
return;
}
if (locationScene != null)
locationScene.processFrame(frame);
});
ARLocationPermissionHelper.requestPermission(this);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment