Wednesday, November 7, 2012

How to get a longitude & latitude from a given Address

 How to get a longitude & latitude from a given Address

Step 1
   Copy pate following code in main.xml
 
 <?xml version="1.0" encoding="UTF-8"?>

       <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
              android:layout_height="wrap_content" android:layout_alignParentBottom="true"
              android:orientation="vertical">
              <EditText android:id="@+id/location" android:layout_width="fill_parent"
                     android:layout_height="wrap_content" android:text="Address here..." />
              <Button android:id="@+id/geocodeBtn" android:layout_width="wrap_content"
                     android:layout_height="wrap_content" android:text="Search" />
              <TextView android:id="@+id/position" android:layout_width="fill_parent"
                     android:layout_height="wrap_content"  />
       </LinearLayout>

Step 2
  Copy paste following Code in your Main Activity Class...
  In here your package name and your Main Activity Class name is different from my code ......

package com.adrianfindaddress;

import java.util.List;

import com.google.android.maps.GeoPoint;

import android.location.Address; 
import android.location.Geocoder; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
 
import com.google.android.maps.GeoPoint; 
import com.google.android.maps.MapActivity; 
import com.google.android.maps.MapView;

public class AdrianLocationFinderActivity extends MapActivity {
    /** Called when the activity is first created. */
        Geocoder geocoder = null; 
        
           @Override 
           protected void onCreate(Bundle savedInstanceState) { 
               super.onCreate(savedInstanceState); 
               setContentView(R.layout.main); 
       
            
               Button geoBtn = (Button) findViewById(R.id.geocodeBtn); 
        
               geocoder = new Geocoder(this); 
        
               geoBtn.setOnClickListener(new OnClickListener() { 
        
                   @Override 
                   public void onClick(View v) { 
                       try { 
                           EditText locale = (EditText) findViewById(R.id.location); 
                           String locationName = locale.getText().toString(); 
        
                           List<Address> addressList = geocoder.getFromLocationName( 
                                   locationName, 5); 
                           if (addressList != null && addressList.size() > 0) { 
                               int lat = (int) (addressList.get(0).getLatitude() * 1e6); 
                               int lng = (int) (addressList.get(0).getLongitude() * 1e6); 
        
                               TextView displayLocation= (TextView) findViewById(R.id.position); 
                               displayLocation.setText("lat: " 
                                       + addressList.get(0).getLatitude() + " lng: " 
                                       + addressList.get(0).getLongitude()); 
        
                              
                           
                           } 
                       } catch (Exception e) { 
                           e.printStackTrace(); 
                       } 
                   } 
               }); 
           } 
        
           @Override 
           public boolean isLocationDisplayed() { 
               return false; 
           } 
        
           @Override 
           public boolean isRouteDisplayed() { 
               return false; 
           } 
       } 

 
Step 3
   AndroidManifest.xml  ... Add user permission

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.adrianfindaddress"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="4" />
   
     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
 

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
       
           <uses-library android:name="com.google.android.maps" />
       
       
        <activity
            android:name=".AdrianLocationFinderActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Step 4

Now Run the App....


Good Luck!

 Tutorial List