This is my blog. Learn more about me at my personal web site [kindohm.com]

breath mints

mike hodnick's posts and finds

  • Archive
  • About

GPS and Geocoding with Android

I did a little development on Android last night and wrote a very simple app that does two things: monitors the current location (latitude and longitude) and gets the street address from the current location data.

The Android location and geocoding APIs were trivial, but this was my first experience doing any GPS programming on a real, physical mobile device (not an emulator) - so it was pretty fun. Everything I needed to implement the app I got from these two sources:

  • Obtaining User Location
  • how to get city name from latitude and longitude values in google map android

I was hesitant to post the source code because it’s just so dang easy… but here is the entire app including the layout and activity:

<?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:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    android:id="@+id/locationText"
    />
<Button 
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:id="@+id/addressButton"
  android:text="Get Address"
    />    
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    android:id="@+id/addressText"
   />
</LinearLayout>
import java.io.IOException;
import java.util.*;
import android.widget.*;
import android.view.View;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.os.Bundle;
import android.location.*;
import android.content.*;

public class LocatahActivity extends Activity {
    
    Button addressButton;
    TextView locationText;
    TextView addressText;
    Location currentLocation;
    double currentLatitude;
    double currentLongitude;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        addressText = (TextView)findViewById(R.id.addressText);
        locationText = (TextView)findViewById(R.id.locationText);
        addressButton = (Button)findViewById(R.id.addressButton);

        this.addressText.setText("ready");

        LocationManager locationManager = 
            (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);

        LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
                updateLocation(location);
            }
            public void onStatusChanged(
                    String provider, int status, Bundle extras) {}
            public void onProviderEnabled(String provider) {}
            public void onProviderDisabled(String provider) {}
        };

        locationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

        this.addressButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v){
                getAddress();
            }
        });
    }
    
    void getAddress(){
        try{
            Geocoder gcd = new Geocoder(this, Locale.getDefault());
            List<Address> addresses = 
                gcd.getFromLocation(currentLatitude, currentLongitude,100);
            if (addresses.size() > 0) {
                StringBuilder result = new StringBuilder();
                for(int i = 0; i < addresses.size(); i++){
                    Address address =  addresses.get(i);
                    int maxIndex = address.getMaxAddressLineIndex();
                    for (int x = 0; x <= maxIndex; x++ ){
                        result.append(address.getAddressLine(x));
                        result.append(",");
                    }               
                    result.append(address.getLocality());
                    result.append(",");
                    result.append(address.getPostalCode());
                    result.append("\n\n");
                }
                addressText.setText(result.toString());
            }
        }
        catch(IOException ex){
            addressText.setText(ex.getMessage().toString());
        }
    }
    
    void updateLocation(Location location){
        currentLocation = location;
        currentLatitude = currentLocation.getLatitude();
        currentLongitude = currentLocation.getLongitude();
        locationText.setText(currentLatitude + ", " + currentLongitude);
    }
}

One thing I learned about the Java language was the ability to implement interfaces anonymously and in-line. When you just need a simple, lightweight type to implement a required interface type, it just keeps things brief. The LocationListener interface used above was coded in this manner. Handy!

Oct 6 2010
  • #android
  • #code
  • #phone
  • #gps
  • #programming
Copyright © 2008–2012 breath mints ‒ HD Exhibit Theme by Dustin Hoffman