Skip to content

appoly/ARCore-Location

Repository files navigation

ARCoreLocation

Allows items to be placed within the AR world using real-world coordinates.

Built for the ARCore Android SDK.

Version 1.x is now for SceneForm projects, and will not be compatible with old versions of ARCore. If you are still using the older version of ARCore, change branch to "legacy". Note that legacy will not recieve frequent updates.

Example usage

It's first required to set-up a basic ARCore sceneform project, or you can use our example.

Importing the library

Add the JitPack repository to your build file

Step 1.

Add JitPack in your root build.gradle at the end of repositories:

allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}

Step 2.

Add the ARCore-Location dependency. Replace 1.0.6 with the latest release from the releases tab on Github

dependencies {
    implementation 'com.github.appoly:ARCore-Location:1.0.6'
}

Using the library

It's highly reccomended to use the example project as a reference. To implement this library into one of your AR projects, do the following.

Step 1.

Inside your AR Activity, you should create a new variable called locationScene, which will be the instance of this library.

private LocationScene locationScene;

Step 2.

Set up your renderables in onCreate

CompletableFuture<ModelRenderable> andy = ModelRenderable.builder()
        .setSource(this, R.raw.andy)
        .build();


CompletableFuture.allOf(andy)
        .handle(
                (notUsed, throwable) -> 
                {
                    if (throwable != null) {
                        DemoUtils.displayError(this, "Unable to load renderables", throwable);
                        return null;
                    }

                    try {
                        andyRenderable = andy.get();

                    } catch (InterruptedException | ExecutionException ex) {
                        DemoUtils.displayError(this, "Unable to load renderables", ex);
                    }
                    return null;
                });

Step 3.

Both your LocationScene, and various location markers should be instantiated in your onCreate method. Assuming you already have a arSceneView - the LocationScene and LocationMarkers can be set-up as follows:

arSceneView
.getScene()
.setOnUpdateListener(
frameTime -> {

    if (locationScene == null) {
        locationScene = new LocationScene(this, this, arSceneView);
        locationScene.mLocationMarkers.add(
                new LocationMarker(
                        -0.119677,
                        51.478494,
                        getAndy()));
    }

    ....

    if (locationScene != null) {
        locationScene.processFrame(frame);
    }

});

Where getAndy() is the following (also showing a onTapListener):

private Node getAndy() {
    Node base = new Node();
    base.setRenderable(andyRenderable);
    Context c = this;
    base.setOnTapListener((v, event) -> {
        Toast.makeText(
                c, "Andy touched.", Toast.LENGTH_LONG)
                .show();
    });
    return base;
}

Demo: Adding a Cluster of Nodes

A new sample activity demonstrates how to add multiple nodes (a cluster) to the AR scene at different real-world locations.

// Example: Adding a cluster of nodes
List<LatLng> clusterLocations = Arrays.asList(
    new LatLng(37.7749, -122.4194),
    new LatLng(37.7750, -122.4195),
    new LatLng(37.7751, -122.4196)
);

Renderable renderable = ...; // Load your custom Renderable

for (LatLng location : clusterLocations) {
    LocationMarker marker = new LocationMarker(
        location.longitude,
        location.latitude,
        renderable
    );
    locationScene.mLocationMarkers.add(marker);
}

You can find this demo as ClusterDemoActivity in the sample app. To try it:

  1. Build and install the sample app.
  2. Select "Cluster Nodes Demo" from the launcher menu.

Permissions

This library requires permission to use the device Camera and Fine Location. You should set this up in AndroidManifest.xml. If you're unfamiliar with requesting permissions, have a look at HelloAR's implementation:

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

The library provides ARLocationPermissionHelper - which is similar to the PermissionHelper used in Google's example projects, and provides a range of functions to correctly aquire the necessary permissions.

Support

If you're having problems with the library, please open a new issue, and we'll aim to address it quickly.

Known issues

Mobile phone compasses only have an accuracy of about 15 degrees, even when calibrated. For most applications this is adequate, but when trying to superimpose markers over the real world it can be a limitation.

Contributing

We'd love your help in making this library better. Pull requests with new features and bug fixes are welcome.

Apps built with ARCore-Location

Where's my cAR? - Appoly

Sponsor this project

  •  

Packages

No packages published

Contributors 10

Languages