Get ISO country code for the given latitude/longitude, using GeoNames Java Client

Last example show "Get ISO country code for the given latitude/longitude, using GeoNames Web Service, using HttpURLConnection". GeoNames provide Java Client for GeoNames Webservices to help developers to easily access the geonames web services with java. This post show how to use it in Android.

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

Then you have to add the JAR modules in your Android Studio Project, refer to the video.


dependencies of ':geonames-1.1.13' and ':jdom-1.0' will be added in your build.gradle.

Example to get ISO country 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;

public class 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
public 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 not contain a parsable double",
Toast.LENGTH_LONG).show();
}

try{
lon = Double.parseDouble(strLon);
}catch (NumberFormatException ex){
parsable = false;
Toast.makeText(MainActivity.this,
"Longitude does not contain a parsable double",
Toast.LENGTH_LONG).show();
}

if(parsable){
new GeoNamesTask(textResult).execute(lat, lon);
}

}
});
}

private class GeoNamesTask extends AsyncTask<Double, Void, String> {
TextView tResult;

public GeoNamesTask(TextView vResult){
tResult = vResult;
tResult.setText("");
}

@Override
protected String doInBackground(Double... params) {
return queryGeoNames_countryCode(params[0], params[1]);
}

@Override
protected void onPostExecute(String s) {
tResult.setText(s);
}

private String queryGeoNames_countryCode(double latitude, double longitude){
String queryResult = "";

/*
Do not use the 'demo' account for your app or your tests.
It is only meant for the sample links on the documentation pages.
Create your own account instead.
*/
WebService.setUserName("demo");

try {
queryResult = "CountryCode: " + WebService.countryCode(latitude, longitude);
} catch (IOException e) {
e.printStackTrace();
queryResult = e.getMessage();
} catch (GeoNamesException e) {
e.printStackTrace();
queryResult = e.getMessage();
}

return 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="http://android-er.blogspot.com/"
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 in AndroidManifest.xml
    <uses-permission android:name="android.permission.INTERNET"/>




Get ISO country code for the given latitude/longitude, using GeoNames Java Client Get ISO country code for the given latitude/longitude, using GeoNames Java Client Reviewed by Pendik on 08.55 Rating: 5

Tidak ada komentar:

Diberdayakan oleh Blogger.