Creating a Point of Interest with Augmented Reality in Wikitude Part 2

- Andrés Cruz

En español
Creating a Point of Interest with Augmented Reality in Wikitude Part 2

Download

This entry corresponds to the continuation of the article Creating a Point of Interest with Augmented Reality in Wikitude Part 1; in which we define what a Point of Interest or POI is, some considerations about the use of GPS on an Android device and we define part of the Architect World (JavaScript code) used to manage Points of Interest based on the location of the device.

Creation of the cross platform to show Points of Interest

Already introducing this entry, we will see how to link our Architect World (JavaScript code) with the native code (Java code) of the application to obtain and constantly update the Points of Interest according to the position of the device:

The native code (Java)

Our activity in Android will be in charge of obtaining the position of the device when a certain time has elapsed or when it has moved a certain distance; once the device's position is obtained, the activity will call a JavaScript method called locationChangedFn(lat, lon, alt, acc) that receives the user's location (latitude, longitude, altitude, and precision) as parameters.

The Architect World (JavaScript code)

Once the locationChangedFn(lat, lon, alt, acc) method is invoked, it will be in charge of invoking the necessary functions to obtain the nearby Points of Interest based on the position (longitude, latitude and altitude) of the device and rendering them on the device screen.

Architect World possibilities (JavaScript code) to get nearby Points of Interest given the position of the device

Al ser código web, existen múltiples formas en la que podemos obtener los Puntos de Interés que hayamos definidos según la posición del usuario:

We can call the architectView.callJavaScript() method to pass data models from our Java code to the JavaScript; This method allows you to create a cross platform (communicate the native code of an application -for example, the one provided in an Android activity- with the Architect World -JavaScript code-) between the native code and the web code: this.architectView.callJavascript( "newData('" +array.toString() + "');" );

function newData(jsonData){
  jsonObject = JSON.parse(jsonData);
  for(var i = 0; i < jsonObject.length; i++){
    // use jsonObject[i] data to create the AR objects for each POI
  }
}
  • We can also load POIs from a local resource as a separate file or some data structure like an Array: var myJsonData = [{ "id": "1", "longitude": "13.0833", "latitude": " 47.75", "description": "This is the description of POI#1", "altitude": "100.0", "name": "POI#1"}, {...}];
  • Or using the Web Services following a format like the following: var ServerInformation = { // sample service returning dummy POIs POIDATA_SERVER: "http://example.wikitude.com/GetSamplePois/", POIDATA_SERVER_ARG_LAT: "lat", POIDATA_SERVER_ARG_LON: "lon ", POIDATA_SERVER_ARG_NR_POIS: "nrPois" };
  • Of course we can use any other technology such as AJAX to obtain the Points of Interest or even generate fictitious Points of Interest close to the position obtained from the device for test cases; this last case is the one that we will use in our experiment in the next section to obtain the Points of Interest.

For more information, you can consult the official documentation RETRIEVING POI DATA

Developing the native code (Java) of the application

Now that we've cleared up some key concepts about how Wikitude works cross-platform for Points of Interest, let's start getting the location of the device.

As has been commented throughout this entry, the native code (Java) is responsible for obtaining the position of the device using GPS, the telephone network, Wi-Fi access points or some associated technology; In order to obtain the user's location, we must request the following permissions in the AndroidManifest:

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

Once requested, we can get the last valid location of the user using the method:

getLastKnownLocation(String provider)

An important point is that Android does not return the current position of the device but the last one registered; In other words, the method called getLastKnownLocation(String provider) does not return the current position, it only returns the last position that was obtained through the provider that is indicated as a parameter and this position could be obtained seconds, minutes, hours ago , days, months, etc.

Although it is possible to use a "listener event" that allows obtaining the real position of the user that can be updated according to the values of the supplier parameters through the requestLocationUpdates() method that allows obtaining the user's position updated over time; said method receives:

  • Name of the location provider.
  • Minimum time between given updates in milliseconds.
  • Minimum distance between given updates in meter.
  • Instance of a LocationListener object which will define the actions to perform when the position is updated.

Defining the listener event in our activity

The following code snippet will be used within our activity to update the user's position:

		LocationListener locationListener = new LocationListener() {
			@Override
			public void onLocationChanged(Location location) {

				if (location != NULL) {
					latitude = location.getLatitude();
					longitude = location.getLongitude();
				}

				architectView.setLocation(latitude, longitude, 1f);
			}
//*** otras validaciones del evento escuchador

Once the "listener event" has been instantiated, we indicate which location services we will use; GPS:

locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)

And the telephone network with Wi-Fi access points:

locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)

At the same time, we indicate how often and how often we want the position to be updated:

		locationManager.requestLocationUpdates(
					LocationManager.NETWORK_PROVIDER, 80000, 0,
					locationListener);

Finally, we return the position:

Location location = locationManager .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); if (location != NULL) { latitude = location.getLatitude(); longitude = location.getLongitude(); }

The versatility of this technology is that we can load only the Points of Interest close to the position of the device based on a range; Points of Interest could be updated according to the position of the device.

The rest of the body of the activity is exactly the same used in previous posts about Wikitude; as for the Architect World or JavaScript code, it is exactly the same as explained in detail in part one of the previous entry.

When executing the application you will be able to realize that it paints a simple POI:

app ANdroid con un PoI - WikiTude SDK

Continued use of POIs with Wikitude

In the following entries we will see how to paint multiple Points of Interest and interact with them; you can download the complete code of the application in our GitHub repository.

Andrés Cruz

Desarrollo con Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz en Udemy

Acepto recibir anuncios de interes sobre este Blog.