Get Iso Province Code For The Given Latitude/Longitude, Using Geonames Coffee Client

Posted on

Last event exhibit “Get ISO province code for the given latitude/longitude, using GeoNames Web Service, using HttpURLConnection“. GeoNames provide Java Client for GeoNames Webservices to attention developers to easily access the geonames spider web services alongside java. This post service exhibit how to role it inwards Android.

To use Java Client for GeoNames Webservices inwards you lot Android Studio project, you lot bring to download both geonames-1.1.13.jar in addition to jdom-1.0.jar to your local machine. Visit http://www.geonames.org/source-code/ to download.

Then you lot bring to add together the JAR modules inwards your Android Studio Project, refer to the video.


dependencies of ‘:geonames-1.1.13’ in addition to ‘:jdom-1.0’ volition live on added inwards your build.gradle.

Example to get ISO province code for the given latitude/longitude, using GeoNames Java Client:


MainActivity.java

package com.blogspot.android_er.androidgeonames;  import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast;  import org.geonames.GeoNamesException; import org.geonames.WebService;  import java.io.IOException;  world shape MainActivity extends AppCompatActivity {      EditText latText;     EditText lonText;     Button btnFind;     TextView textResult;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         latText = (EditText)findViewById(R.id.latText);         lonText = (EditText)findViewById(R.id.lonText);         btnFind = (Button)findViewById(R.id.find);         textResult = (TextView)findViewById(R.id.result);          btnFind.setOnClickListener(new View.OnClickListener() {             @Override             world void onClick(View v) {                 String strLat = latText.getText().toString();                 String strLon = lonText.getText().toString();                  boolean parsable = true;                 Double lat = null, lon = null;                  try{                     lat = Double.parseDouble(strLat);                 }catch (NumberFormatException ex){                     parsable = false;                     Toast.makeText(MainActivity.this,                             "Latitude does non incorporate a parsable double",                             Toast.LENGTH_LONG).show();                 }                  try{                     lon = Double.parseDouble(strLon);                 }catch (NumberFormatException ex){                     parsable = false;                     Toast.makeText(MainActivity.this,                             "Longitude does non incorporate a parsable double",                             Toast.LENGTH_LONG).show();                 }                  if(parsable){                     novel GeoNamesTask(textResult).execute(lat, lon);                 }              }         });     }      person shape GeoNamesTask extends AsyncTask<Double, Void, String> {         TextView tResult;          world GeoNamesTask(TextView vResult){             tResult = vResult;             tResult.setText("");         }          @Override         protected String doInBackground(Double... params) {             render queryGeoNames_countryCode(params[0], params[1]);         }          @Override         protected void onPostExecute(String s) {             tResult.setText(s);         }          person String queryGeoNames_countryCode(double latitude, double longitude){             String queryResult = "";              /*             Do non role the 'demo' trouble organisation human relationship for your app or your tests.             It is exclusively meant for the sample links on the documentation pages.             Create your ain trouble organisation human relationship instead.              */             WebService.setUserName("demo");              endeavor {                 queryResult = "CountryCode: " + WebService.countryCode(latitude, longitude);             } select grip of (IOException e) {                 e.printStackTrace();                 queryResult = e.getMessage();             } select grip of (GeoNamesException e) {                 e.printStackTrace();                 queryResult = e.getMessage();             }              render queryResult;         }     } }  

layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:padding="16dp"     android:orientation="vertical"     tools:context=".MainActivity">      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center_horizontal"         android:autoLink="web"         android:text="https://rootandroid.org//"         android:textStyle="bold" />      <EditText         android:id="@+id/latText"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:inputType="numberSigned|numberDecimal"         android:hint="Latitude"         android:text="47.03"/>      <EditText         android:id="@+id/lonText"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:inputType="numberSigned|numberDecimal"         android:hint="Longitude"         android:text="10.2"/>      <Button         android:id="@+id/find"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="find"/>      <TextView         android:id="@+id/result"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:textSize="28dp"         android:textStyle="bold"/> </LinearLayout>  

Permission of “android.permission.INTERNET” is needed inwards AndroidManifest.xml

    <uses-permission android:name="android.permission.INTERNET"/>