Simple Geolocation code

In this very simple code bellow i find my longitude, latitude and altitude via GPS or Wi-Fi… Most of the time altitude is zero and i dont know why.

import ketai.sensors.*; 

double longitude, latitude, altitude;
KetaiLocation location;

void setup() {
  orientation(LANDSCAPE);
  textAlign(CENTER, CENTER);
  textSize(36);
  location = new KetaiLocation(this);
}

void draw() {
  background(78, 93, 75);
  if (location.getProvider() == "none")
    text("Location data is unavailable. \n" +
      "Please check your location settings.", 0, 0, width, height);
  else
    text("Latitude: " + latitude + "\n" + 
      "Longitude: " + longitude + "\n" + 
      "Altitude: " + altitude + "\n" + 
      "Provider: " + location.getProvider(), 0, 0, width, height);  
  // getProvider() returns "gps" if GPS is available
  // otherwise "network" (cell network) or "passive" (WiFi MACID)
}

void onLocationEvent(double _latitude, double _longitude, double _altitude)
{
  longitude = _longitude;
  latitude = _latitude;
  altitude = _altitude;
  println("lat/lon/alt: " + latitude + "/" + longitude + "/" + altitude);
}

@netphysicist===

  • what do you mean by “most of the time”? - Does it returns correct values sometime? - What is the provider used in this case?

  • altitude can be obtained mainly from 3 ways and ketai uses 2 of them: internet (which means WIFI), or GPS; third way is not used by ketai: it s pressure that you can get using pressure sensor and sensor service.

  • altitude from GPS depends of the hardware: perhaps you have a phone which is not good for that

  • your code is the basic ketai example: when i tested it on my phones i saw that the results were not the same and one of them returns 0 for the altitude (i cannot remember which one) when others (Samsung tablet, sonyXperia, Google nexus 7…) return similar values for altitude.

  • in your case, in order to verify i would try to use the pressure sensor (not with ketai but with android native)

1 Like

@akenaton when the provider is GPS it returns all values correct and when the provider is WiFi the altitude is a zero… My phone is xiaomi redmi 3 but i tried it and in a lenovo one and still the same results. How can we use the pressure sensor??

@netphysicist===

code snippet for that (native android so it needs some imports but easy to adapt to P5 )::

@Override
            public void onSensorChanged(SensorEvent event) {
            
              float press = 0.0f;
              float alt = 0.0f;

                           if( Sensor.TYPE_PRESSURE == event.sensor.getType() ) {
                press = event.values[0];
                System.out.println("PRESSURE" + press);
                height = SensorManager.getAltitude(SensorManager.PRESSURE_STANDARD_ATMOSPHERE, press);
                System.out.println("altitude" + height);
                
               
              }
            }

…In order to be more accurate you have to change the STANDARD value for your value at sea level.

more details here: https://developer.android.com/reference/android/hardware/SensorManager#getAltitude(float,%20float)

2 Likes

@akenaton

@netphysicist

FATAL EXCEPTION: Animation Thread
Process: processing.test.chemistry_210101, PID: 27895
java.lang.NoSuchMethodError: No virtual method getSurface()Lprocessing/core/PSurface; in class Lprocessing/core/PApplet; or its super classes (declaration of 'processing.core.PApplet' appears in /data/app/processing.test.chemistry_210101-RWZnH1bDvEP4YubgDuIfgg==/base.apk)
	at ketai.sensors.KetaiLocation.<init>(Unknown Source:24)
	at processing.test.chemistry_210101.chemistry_210101.setup(chemistry_210101.java:152)
	at processing.core.PApplet.handleDraw(Unknown Source:81)
	at processing.core.PGraphicsAndroid2D.requestDraw(Unknown Source:2)
	at processing.core.PApplet.run(Unknown Source:37)
	at java.lang.Thread.run(Thread.java:764)

hello.

The ketai library gives an error message like the one above. Any solution?

@GWAK hi

There is issues with ketai for Android 10 and above

With Android 9 the sketch run

Screenshot_2023-05-01-14-41-01-866

1 Like

@GWAK hi

1 Like

@jafal

you’re right. And thank you for your reply.

It doesn’t work in version 10.
Thank you for answer.