Sunday 23 June 2013

Finding Present Place

Demo:

1)MainActivity.java: 
package com.venky.getpresentplaceaddress;
import java.util.List;
import java.util.Locale;
import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

      TextView tv;
      LocationManager locationManager;
      LocationListener locationListener;

      String presentAddress = "";

      @Override
      protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            tv = (TextView) findViewById(R.id.tv);

            locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

            locationListener = new LocationListener() {

                  @Override
                  public void onStatusChanged(String provider, int status,
                              Bundle extras) {
                        // TODO Auto-generated method stub

                  }

                  @Override
                  public void onProviderEnabled(String provider) {
                        // TODO Auto-generated method stub

                  }

                  @Override
                  public void onProviderDisabled(String provider) {
                        // TODO Auto-generated method stub

                  }

                  @Override
                  public void onLocationChanged(Location location) {
                        // TODO Auto-generated method stub

                        double lati = location.getLatitude();
                        double longi = location.getLongitude();

                        findPresentAddress(lati, longi);
                  }
            };

            locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
      }

      public void findPresentAddress(double lati, double longi) {

            try {
                  Geocoder gc = new Geocoder(MainActivity.this, Locale.getDefault());

                  List<Address> address = gc.getFromLocation(lati, longi, 1);

                  for (int i = 0; i < address.get(0).getMaxAddressLineIndex(); i++) {
                        presentAddress += address.get(0).getAddressLine(i);
                  }
            } catch (Exception e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            }

            tv.setText(presentAddress);
      }
}

2)activity_main.xml:
<RelativeLayout 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:background="#5F04B4"
    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=".MainActivity" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        android:textColor="#ffffff" />

</RelativeLayout>

3)Required Permission:

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

4)Download this code Click Here

No comments:

Post a Comment