# Simple Geolocation code

**URL:** https://discourse.processing.org/t/simple-geolocation-code/6503
**Category:** Processing for Android
**Created:** [December 11, 2018, 12:25pm UTC](https://discourse.processing.org/t/simple-geolocation-code/6503 "2018-12-11T12:25:55Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![netphysicist](https://avatars.discourse-cdn.com/v4/letter/n/87869e/32.png) [@netphysicist](https://discourse.processing.org/u/netphysicist)
#### Post date: [December 11, 2018, 12:25pm UTC](https://discourse.processing.org/t/simple-geolocation-code/6503/1 "2018-12-11T12:25:55Z")

</div>

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.

```auto
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);
}

```

---

<div class="post-metadata">

### Author: ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)
#### Post date: [December 12, 2018, 9:51am UTC](https://discourse.processing.org/t/simple-geolocation-code/6503/2 "2018-12-12T09:51:31Z")

</div>

@netphysicist===

> [@netphysicist](#):
>
> Most of the time altitude is zero and i dont know why.

- 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)

---

<div class="post-metadata">

### Author: ![netphysicist](https://avatars.discourse-cdn.com/v4/letter/n/87869e/32.png) [@netphysicist](https://discourse.processing.org/u/netphysicist)
#### Post date: [December 18, 2018, 6:49pm UTC](https://discourse.processing.org/t/simple-geolocation-code/6503/3 "2018-12-18T18:49:44Z")

</div>

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

---

<div class="post-metadata">

### Author: ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)
#### Post date: [December 19, 2018, 8:26am UTC](https://discourse.processing.org/t/simple-geolocation-code/6503/4 "2018-12-19T08:26:14Z")

</div>

@netphysicist===

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

```auto
@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)](https://developer.android.com/reference/android/hardware/SensorManager#getAltitude(float,%20float))

---

<div class="post-metadata">

### Author: ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)
#### Post date: [May 1, 2023, 8:30am UTC](https://discourse.processing.org/t/simple-geolocation-code/6503/5 "2023-05-01T08:30:32Z")

</div>

@akenaton

@netphysicist

```auto
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?

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [May 1, 2023, 11:48am UTC](https://discourse.processing.org/t/simple-geolocation-code/6503/6 "2023-05-01T11:48:48Z")

</div>

@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](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/3/b/3bb9b9e16725e86ec4bd60edb0a998fc5049f9c9.jpeg)

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [May 1, 2023, 12:27pm UTC](https://discourse.processing.org/t/simple-geolocation-code/6503/7 "2023-05-01T12:27:34Z")

</div>

@GWAK hi

> <https://github.com/danielsauter/rapid-android-development/blob/master/sensors.md>

---

<div class="post-metadata">

### Author: ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)
#### Post date: [May 1, 2023, 12:34pm UTC](https://discourse.processing.org/t/simple-geolocation-code/6503/8 "2023-05-01T12:34:26Z")

</div>

@jafal

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

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