Breaking News
Loading...
Monday, 8 April 2013

Search address by name with Geocoder

06:32
The getFromLocationName() method of android.location.Geocoder returns an array of Addresses that are known to describe the named location.

Example of searching address by name with Geocoder:

Search address by name with Geocoder


package com.example.androidgeocoder;

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

import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {

EditText searchIn;
Button searchButton;
ListView searchOut;

private ArrayAdapter<String> adapter;

Geocoder geocoder;
final static int maxResults = 5;
List<Address> locationList;
List<String> locationNameList;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
searchIn = (EditText)findViewById(R.id.serchin);
searchButton = (Button)findViewById(R.id.serch);
searchOut = (ListView)findViewById(R.id.serchout);

searchButton.setOnClickListener(searchButtonOnClickListener);

geocoder = new Geocoder(this, Locale.ENGLISH);

locationNameList = new ArrayList<String>(); //empty in start
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, locationNameList);
searchOut.setAdapter(adapter);
}

OnClickListener searchButtonOnClickListener =
new OnClickListener(){

@Override
public void onClick(View arg0) {
String locationName = searchIn.getText().toString();

Toast.makeText(getApplicationContext(),
"Search for: " + locationName,
Toast.LENGTH_SHORT).show();

if(locationName == null){
Toast.makeText(getApplicationContext(),
"locationName == null",
Toast.LENGTH_LONG).show();
}else{
try {
locationList = geocoder.getFromLocationName(locationName, maxResults);

if(locationList == null){
Toast.makeText(getApplicationContext(),
"locationList == null",
Toast.LENGTH_LONG).show();
}else{
if(locationList.isEmpty()){
Toast.makeText(getApplicationContext(),
"locationList is empty",
Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(),
"number of result: " + locationList.size(),
Toast.LENGTH_LONG).show();

locationNameList.clear();

for(Address i : locationList){
if(i.getFeatureName() == null){
locationNameList.add("unknown");
}else{
locationNameList.add(i.getFeatureName());
}
}

adapter.notifyDataSetChanged();
}
}


} catch (IOException e) {
Toast.makeText(getApplicationContext(),
"network unavailable or any other I/O problem occurs" + locationName,
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
}};

}


<LinearLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<EditText
android:id="@+id/serchin"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/serch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Search" />
<ListView
android:id="@+id/serchout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>


download filesDownload the files.

0 comments:

Post a Comment

 
Toggle Footer