Ubidots Community

[SOLVED] Reading data from location (Map) using Android

Hello. I want create Android app, which display location of a GPS other device.

I have few location on the server: http://screenshot.sh/m1Mz1O8jyNVcj
and now, I want display last location (lat:35.89132749, lng:128.61438482) on the map.
How can I get Latitude and Latitude? I don’t want get Values (123).
Thank you :slight_smile:

Hi,

There are two ways to get the latitude and longitude

The first to use the endpoint https://things.ubidots.com/api/v1.6/variables/VARIABLE_ID/ and use the key properties._last_value

The second is with the endpoint https://things.ubidots.com/api/v1.6/variables/VARIABLE_ID/values?page_size=1 and send the page_size parameter, then you should use the key results[0].context.

Example:

Gustavo.

hi can you please give us an idea how to implement that using android studio …

thank u

I think my solution is not the best (it is probably better to send request to server), but it works :smiley:
At the beginning you should create URL:

String url = "http://things.ubidots.com/api/v1.6/variables/" + VariableId
                + "/values?token=" + Token + "&format=json";

If you want to get only the last value you have to add to the end of the URL
&page_size=1
Then

        BufferedReader in;
        URL requestUrl;
        try {
            requestUrl = new URL(url); 
            HttpURLConnection con = (HttpURLConnection) requestUrl.openConnection();
            InputStream is = new BufferedInputStream(con.getInputStream());
            in = new BufferedReader(new InputStreamReader(is));
        } catch (Exception e) {
            errorMsg = "Incorrect Variable Id or Token";
            return null;
        }
        StringBuilder sb = new StringBuilder();
        int cp;
        try {
            while ((cp = in.read()) != -1)
                sb.append((char) cp);
        } catch (Exception e) {
            errorMsg = "Buffered Reader produced Null Pointer Exception";
            return null;
        }
        String json = sb.toString();

Now, you have JSON Object as String “json” and you need parser :slight_smile:

Thank youuuu :slight_smile: