GoogleMaps with Geocoder Class
This tutorial is also available in german at my android weblog Androidianer.de
Hey there,
I've written this tutorial, because there were a lot of questions in the android-group and in this forum about the functionality of the Geocoder Class and the possibility to convert an address in the longitude and latitude.
I hope this tutorial can clarify the most problems.
What the application can do:
Convert an entered address in longitude and latitude and display it in a map.
How it looks like:

Here we go:
1. Create a nice layout in the main.xml. I will not explain XML-layouting here, cause there are a lot of good tutorials from plusminus in this forum. Donwload the attached source code to see my main.xml
2. Add the following permissions to the Manifest.xml over the <application> Tag
Using xml Syntax Highlighting
- <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
- <uses-permission android:name="android.permission.INTERNET" />
Parsed in 0.001 seconds, using GeSHi 1.0.8.4
3. Have fun with java. It's so easy with GeoCoder class:
All you have to do is:
- Create a new Geocoder instance
Using java Syntax Highlighting
- gc = new Geocoder(this); //create new geocoder instance
Parsed in 0.030 seconds, using GeSHi 1.0.8.4
- Write a OnClickListener for the search button (btnSearch) and get the entered address from the EditText-field (address)
Using java Syntax Highlighting
- gc = new Geocoder(this); //create new geocoder instance
- btnSearch.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- String addressInput = adress.getText().toString(); //Get input text
- }
- });
Parsed in 0.030 seconds, using GeSHi 1.0.8.4
- Let the GeoCoder class search for the entered address (don't forget the try-catch-statement)
Using java Syntax Highlighting
- gc = new Geocoder(this); //create new geocoder instance
- btnSearch.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- String addressInput = adress.getText().toString(); //Get input text
- try {
- List<Address> foundAdresses = gc.getFromLocationName(addressInput, 5); //Search addresses
- }
- catch (Exception e) {
- //@todo: Show error message
- }
- }
- });
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
- Get the longitude and latitude from the found addresses and display the map (see full source code for the navigateToLocation function)
Using java Syntax Highlighting
- gc = new Geocoder(this); //create new geocoder instance
- btnSearch.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- String addressInput = adress.getText().toString(); //Get input text
- try {
- List<Address> foundAdresses = gc.getFromLocationName(addressInput, 5); //Search addresses
- for (int i = 0; i < foundAdresses.size(); ++i) {
- //Save results as Longitude and Latitude
- //@todo: if more than one result, then show a select-list
- Address x = foundAdresses.get(i);
- lat = x.getLatitude();
- lon = x.getLongitude();
- }
- navigateToLocation((lat * 1000000), (lon * 1000000), myMap); //display the found address
- }
- catch (Exception e) {
- //@todo: Show error message
- }
- }
- });
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
Full Source
The full source also displays an error if the address wasn't found (check also the attachment for manifest and main.xml):
Using java Syntax Highlighting
- //Lot's of imports here... see full source
- public class simpleGoogleMaps extends MapActivity {
- protected boolean isRouteDisplayed() { return false; }
- private MapView myMap;
- private Button btnSearch;
- private EditText adress;
- private Geocoder gc;
- private double lat;
- private double lon;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- myMap = (MapView) findViewById(R.id.simpleGM_map); //Get map from XML
- btnSearch = (Button) findViewById(R.id.simpleGM_btn_search); //Get button from XML
- adress = (EditText) findViewById(R.id.simpleGM_adress); //Get address from XML
- gc = new Geocoder(this); //create new geocoder instance
- btnSearch.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- String addressInput = adress.getText().toString(); //Get input text
- try {
- List<Address> foundAdresses = gc.getFromLocationName(addressInput, 5); //Search addresses
- if (foundAdresses.size() == 0) { //if no address found, display an error
- Dialog locationError = new AlertDialog.Builder(simpleGoogleMaps.this)
- .setIcon(0)
- .setTitle("Error")
- .setPositiveButton(R.string.ok, null)
- .setMessage("Sorry, your address doesn't exist.")
- .create();
- locationError.show();
- }
- else { //else display address on map
- for (int i = 0; i < foundAdresses.size(); ++i) {
- //Save results as Longitude and Latitude
- //@todo: if more than one result, then show a select-list
- Address x = foundAdresses.get(i);
- lat = x.getLatitude();
- lon = x.getLongitude();
- }
- navigateToLocation((lat * 1000000), (lon * 1000000), myMap); //display the found address
- }
- }
- catch (Exception e) {
- //@todo: Show error message
- }
- }
- });
- }
- /**
- * Navigates a given MapView to the specified Longitude and Latitude
- */
- public static void navigateToLocation (double latitude, double longitude, MapView mv) {
- GeoPoint p = new GeoPoint((int) latitude, (int) longitude); //new GeoPoint
- mv.displayZoomControls(true); //display Zoom (seems that it doesn't work yet)
- MapController mc = mv.getController();
- mc.animateTo(p); //move map to the given point
- int zoomlevel = mv.getMaxZoomLevel(); //detect maximum zoom level
- mc.setZoom(zoomlevel - 1); //zoom
- mv.setSatellite(false); //display only "normal" mapview
- }
- }
Parsed in 0.048 seconds, using GeSHi 1.0.8.4
I hope this demonstrates how easy it is to convert an address in long/lat with GeoCoder.
See the GoogleMaps Mini Tutorial from lordhong if you want to add more features to your application.
But with GeoCoder you mustn't use strange Yahoo-API requests

Thanks to plusminus for his great tutorials. Hope I can give something back with this one.
[Edit:] When you read the APIs you will see that it is recommended to put the getFromLocation() method in an own thread. Read the Simple GoogleMaps with Threads tutorial to see how it works.
Greetings from Germany
Mic




and I thought it's too complicated to explain GeoCoder and threads in one tutorial.



