Skip to content

Example code how to create an object with GPS coordinates and display its distance

bitstars edited this page Apr 21, 2013 · 1 revision

The following code is from a normal DefaultARSetup class and creates an object at a specific location which reacts to the distance of the user to this object:

@Override
public void addObjectsTo(GL1Renderer renderer, final World world,
		GLFactory objectFactory) {

	Location l = new Location("");
	l.setLatitude(123);
	l.setLongitude(123);
	Obj o = new GeoObj(l);
	o.setComp(objectFactory.newCube());
	o.setComp(new Entity() {

		private Updateable p;
		private float waitTimeInMilliseconds = 1000;
		UpdateTimer timer = new UpdateTimer(waitTimeInMilliseconds, null);

		@Override
		public boolean accept(Visitor visitor) {
			return false;
		}

		@Override
		public boolean update(float timeDelta, Updateable parent) {
			p = parent;
			if (timer.update(timeDelta, parent)) {
				// true once a second, do the calculation here:
				float distanceOfGeoObjToUser = Vec.distance(((GeoObj) p)
						.getVirtualPosition(), world.getMyCamera()
						.getPosition());
				// do something with the distance e.g. update a
				// corresponding Android UI element
			}
			return true;
		}

		@Override
		public void setMyParent(Updateable parent) {
			this.p = parent;
		}

		@Override
		public Updateable getMyParent() {
			return p;
		}
	});
}