# Geolocation example for Processing ;)

**URL:** <https://discourse.processing.org/t/geolocation-example-for-processing/20342>\
**Category:** Gallery\
**Created:** [May 1, 2020, 9:10am UTC](https://discourse.processing.org/t/geolocation-example-for-processing/20342 "2020-05-01T09:10:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![erkosone](https://avatars.discourse-cdn.com/v4/letter/e/ba9def/32.png) [@erkosone](https://discourse.processing.org/u/erkosone)\
**Post date:** [May 1, 2020, 9:10am UTC](https://discourse.processing.org/t/geolocation-example-for-processing/20342/1 "2020-05-01T09:10:59Z")

</div>

**This is a simple example to get geoLocation in Processing JAVA MODE on “windows / linux / macosx / andorid”.**

```auto
import java.net.*;
import java.io.*;
void setup(){
    String[] l = geolocation();
    for(String s : l){
        println(s);
    }
}
void draw(){
    
}
String[] geolocation(){
    StringList l = new StringList();
    String[] data = null;
    Socket sock = null;
    BufferedReader bufRead = null;
    try{
        sock = new Socket("ip-api.com", 80);
        //Instantiates a new PrintWriter passing in the sockets output stream
        PrintWriter wtr = new PrintWriter(sock.getOutputStream());
        //Prints the request string to the output stream
        wtr.println("GET /json HTTP/1.0");
        wtr.println("");
        wtr.flush();
                    
        //Creates a BufferedReader that contains the server response
        bufRead = new BufferedReader(new InputStreamReader(sock.getInputStream()));
        String outStr = "";
                    
        //Prints each line of the response 
        while((outStr = bufRead.readLine()) != null){
            l.append(outStr);
        }
            
        //Closes out buffer and writer
        bufRead.close();
        wtr.close();
        
        // parse output..
        for(String s : l){
            if(s.contains("{")){
                String s1 = s.substring(1);
                String s2 = s1.substring(0, s1.length()-1);
                data = split(s2, ",");
            }
        }
        
    }catch(Exception e){
        println(e);
    }
    
    return data;
}

```

---

<div class="post-metadata">

**Author:** ![erkosone](https://avatars.discourse-cdn.com/v4/letter/e/ba9def/32.png) [@erkosone](https://discourse.processing.org/u/erkosone)\
**Post date:** [May 1, 2020, 9:13am UTC](https://discourse.processing.org/t/geolocation-example-for-processing/20342/2 "2020-05-01T09:13:07Z")

</div>

**My output for this program are this:**

“status”:“success”  
“country”:“Spain”  
“countryCode”:“ES”  
“region”:“CT”  
“regionName”:“Catalonia”  
“city”:“Barcelona”  
“zip”:“08005”  
“lat”:41.3891  
“lon”:2.1611  
“timezone”:“Europe/Madrid”  
“isp”:“Xtratelecom”  
“org”:“XFERA Moviles S.A.”  
“as”:“AS15704 XTRA TELECOM S.A.”  
“query”:“95.169.234.191”

**Very util for rankings online with “country” tag etc etc…**

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [May 1, 2020, 9:47am UTC](https://discourse.processing.org/t/geolocation-example-for-processing/20342/3 "2020-05-01T09:47:53Z")

</div>

Very nice, but in my case it missed the city.  
It should be Recife. instead of Caruaru. How cume?

---

<div class="post-metadata">

**Author:** ![erkosone](https://avatars.discourse-cdn.com/v4/letter/e/ba9def/32.png) [@erkosone](https://discourse.processing.org/u/erkosone)\
**Post date:** [May 4, 2020, 3:42pm UTC](https://discourse.processing.org/t/geolocation-example-for-processing/20342/4 "2020-05-04T15:42:47Z")

</div>

In any case, this service is interesting, although it may have a small margin of error. 😉

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [May 7, 2020, 3:51am UTC](https://discourse.processing.org/t/geolocation-example-for-processing/20342/5 "2020-05-07T03:51:44Z")

</div>

> [@noel](#):
>
> It should be Recife. instead of Caruaru.

Because it simply loads the data from [https://ip-api.com/](https://ip-api.com/) (scroll down) based on your current IP.

If the IP address of your internet service provider or proxy is not in the same city as you then the geolocation will not be correct.
