Monday, 30 December 2013

Forward GeoCoding Example

Download project : ForwardGeoCodingExample.zip

activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="enter address to find location"
        android:inputType="textPersonName" />

    <Button
        android:id="@+id/button_find"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Find Location"
        android:layout_gravity="center"
        android:layout_marginTop="20dp" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Location is :"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

MainActivity.java:
package com.ram.forwardgeocoding;

import java.io.IOException;
import java.util.List;

import android.app.Activity;
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;

public class MainActivity extends Activity {
    EditText edit;
    TextView text;

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

        edit = (EditText) findViewById(R.id.editText);
        text = (TextView) findViewById(R.id.textView);

        Button findBtn = (Button) findViewById(R.id.button_find);

        findBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                String address = edit.getText().toString();

                Geocoder geoCoder = new Geocoder(getApplicationContext());

                try {
                    List<Address> locations = geoCoder.getFromLocationName(
                            address, 1);

                    for (Address a : locations) {
                        double latti = a.getLatitude();
                        double longi = a.getLongitude();

                        text.append(latti + "," + longi);
                    }

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        });

    }
}

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.ram.forwardgeocoding.MainActivity"
            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>

1 comment:

  1. It does not show lat long in TextView .But when i restart my phone and try again it shows lat long . where is problem???

    ReplyDelete