Saturday, September 8, 2012

Gecoder Example In Android

This example explains how show how to get latitude, longitude values of an address.
Algorithm:
1.) Create a new project by File-> New -> Android Project name it GecoderExample2.
2.) You will see some default code into your main.xml and android manifest file.
3.) Write following into main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="fill_parent">
     

        <TextView android:id="@+id/Lable" android:textStyle="bold"
                android:gravity="left" android:text="Enter address"
                android:layout_width="wrap_content"android:layout_height="wrap_content"
                android:layout_marginTop="16dp"></TextView>
        <EditText android:id="@+id/editbox"
                android:lines="2" android:maxLength="200"android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:textSize="18sp"/>
        <Button android:text="PreView" android:id="@+id/Button"
                android:layout_height="wrap_content"android:layout_width="wrap_content"/>
</LinearLayout>
4.) Run for output.
Steps:
1.) Create a project named GecoderExample2 and set the information as stated in the image.
Build Target: Android 2.1 (Google API)
Application Name: GecoderExample2
Package Name: com.example. Gecoder
Activity Name: Gecoder2Activity
Min SDK Version: 7
2.) Open Gecoder2Activity.java file and write following code there:
package com.example.Geocoder;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import android.app.Activity;
import android.app.AlertDialog;
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 android.widget.Toast;
public class Gecoder2Activity extends Activity {
        private String DestinationAddress;
        private TextView destinationText;
        /** Called when the activity is first created. */
          @Override
          public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.main);
              destinationText = (EditText) findViewById(R.id.editbox);
              Button cancelRecordButton = (Button) findViewById(R.id.Button);
                        cancelRecordButton.setOnClickListener(newOnClickListener() {
                                @Override
                                public void onClick(View v) {
                                checkAddress();
                                }
                        });
          }
          void checkAddress()
                {
                        Geocoder geoCoder = new Geocoder(Gecoder2Activity.this,Locale.getDefault());
                        DestinationAddress =destinationText.getText().toString();
                        List<Address> addressList = null;
                        try {
                                addressList =geoCoder.getFromLocationName(DestinationAddress, 1);
                        } catch (IOException e) {
                                Toast.makeText(Gecoder2Activity.this,"Location not found!!! Please change address and try again.",Toast.LENGTH_SHORT).show();
                                e.printStackTrace();
                        }
                        if(addressList.isEmpty())
                        {
                                AlertDialog.Builder dlgAlert  = newAlertDialog.Builder(Gecoder2Activity.this);
                                dlgAlert.setMessage("Location not found!!! Please change address and try again.");
                                dlgAlert.setTitle("Gecoder2Activity");
                                dlgAlert.setPositiveButton("OK"null);
                                dlgAlert.setCancelable(true);
                                dlgAlert.create().show();
                                destinationText.requestFocus();
                        }
                        else
                        {
                        Address address = addressList.get(0);
                        if(address.hasLatitude() && address.hasLongitude()){
                             
                                Toast.makeText(Gecoder2Activity.this,"Address : "+address.getAddressLine(0)+"\nCountry : "+address.getCountryName()+"\nLatitute : "+address.getLatitude()+"\nLongitute : "+address.getLongitude(),Toast.LENGTH_LONG).show();
                        }
                        }
                }
}
3.) Compile and build the project for output.
Output

No comments:

Post a Comment