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() {
  textAlign(CENTER, CENTER);
  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);
    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);


  • 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??


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

            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:,%20float)

1 Like