Hi Friend,
In previous posts, i explained how to got your current location using gps, but in that case you only get your latitude and longitude.
Today i going to explain how to get your location with help of latitude and longitude.
This concept is known as Reverse Geo Coding.
In this application we get location of a particular latitude and longitude using Geocoder API.
http://maps.googleapis.com/maps/api/geocode/xml?latlng=28.6100,77.2300&sensor=true
The response of above api is like this
For getting location you have to parse The response of Geocoder API using parsing concept.
Now we come on Android application Code.
Create an application project named AndroidReverseGeoCodingExample.
Application LayOut View
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00aaff"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ReverseGeo" >
android:layout_width="240dp"
android:layout_height="300dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#ffccaa"
android:orientation="vertical" >
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
android:layout_width="match_parent"
android:layout_height="302dp"
android:orientation="vertical" >
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter Latitude"
>
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter Longitude" />
android:id="@+id/radioGroup1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="using Gps" />
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="using network" />
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CurrentLocaction" />
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="147dp"
android:text="TextView" />
Java Source Code:-
My Activity file name is ReverseGeo.java
package in.androidshivendra.androidreversegeocodingexample;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
public class ReverseGeo extends Activity{
Button btn;
ProgressDialog pb;
EditText elat, elon;
LocationManager locationManager ;
LocationListener locationListener;
RadioButton rgps, rnet;
CheckBox ccurrent;
Location loc;
TextView tv;
String la, lo;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reverse_geo);
//http://maps.googleapis.com/maps/api/geocode/json?latlng=28.6100,77.2300&sensor=true
btn = (Button)findViewById(R.id.button1);
tv = (TextView)findViewById(R.id.textView1);
ccurrent =(CheckBox)findViewById(R.id.checkBox1);
rgps = (RadioButton)findViewById(R.id.radio0);
rnet = (RadioButton)findViewById(R.id.radio1);
elat =(EditText)findViewById(R.id.editText1);
elon = (EditText)findViewById(R.id.editText2);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(ccurrent.isChecked())
{
elat.setText(la);
elon.setText(lo);
}
else
{
la= elat.getText().toString();
lo=elon.getText().toString();
}
if(rnet.isChecked() )
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
else if(rgps.isChecked() )
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
new back().execute();
}
});
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
loc = location;
la=String.valueOf(loc.getLatitude());
lo=String.valueOf(loc.getLongitude());
Toast.makeText(ReverseGeo.this,"New Location deteted" ,Toast.LENGTH_SHORT).show();
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {
Toast.makeText(ReverseGeo.this,"GPS/Use Wireless network is not enabled" ,Toast.LENGTH_SHORT).show();
}
};
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
// Register the listener with the Location Manager to receive location updates
if(rnet.isChecked())
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
else if(rgps.isChecked())
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
locationManager.removeUpdates(locationListener);
super.onPause();
}
class back extends AsyncTask Void, Void , Void{
String result=null;
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
pb.dismiss();
tv.setText(Xmlparse.sb);
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pb = new ProgressDialog(ReverseGeo.this);
pb.setTitle("Recognizing your address...");
pb.show();
}
@Override
protected Void doInBackground(Void... params) {
Xmlparse parse = new Xmlparse();
parse.execute(la,lo);
return null;
}
}
}
And for parsing purpose i have used SAX XML parsing concept and my parsing file name is Xmlparse.java
package in.androidshivendra.androidreversegeocodingexample;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import android.util.Log;
public class Xmlparse {
static int i =0;
String temp= "";
static StringBuffer sb ;
void execute(String lat, String lon){
try{
URLConnection conn = new URL("http://maps.googleapis.com/maps/api/geocode/xml?latlng="+lat+","+lon+"&sensor=true").openConnection();
InputStream is = conn.getInputStream();
sb = new StringBuffer();
DefaultHandler dh = new DefaultHandler(){
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
temp = new String(ch,start,length);
super.characters(ch, start, length);
}
@Override
public void endElement(String uri, String localName,
String qName) throws SAXException {
if(qName.equals("formatted_address"))
{
sb.append(temp);
Log.d("MO",(i++)+temp);
}
super.endElement(uri, localName, qName);
}
@Override
public void startElement(String uri, String localName,
String qName, Attributes attributes)
throws SAXException {
super.startElement(uri, localName, qName, attributes);
}
};
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
parser.parse(is, dh);
}catch(Exception e){
Log.i("MO", "Exception"+e.toString());
}
}
}
finally my AndroidManifest file is as follow. we required some uses permission like
1.android.permission.INTERNET
2.android.permission.ACCESS_FINE_LOCATION
3.android.permission.ACCESS_COARSE_LOCATION
Complete manifest file code:-
package="in.androidshivendra.androidreversegeocodingexample"
android:versionCode="1"
android:versionName="1.0" >
android:minSdkVersion="8"
android:targetSdkVersion="18" />
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:name="in.androidshivendra.androidreversegeocodingexample.ReverseGeo"
android:label="@string/app_name"
android:icon="@drawable/ic_launcher">
Now your application ready to run just check it.
if Current location is checked then it shows your current location address using gps or network provider
Now You can download complete application project source code
HereHow to get latitude and longitude using gps explain Here.
Get APK File Here
No comments:
Post a Comment