package com.example.gui; import java.io.IOException; import java.util.List; import android.app.Activity; import android.content.Intent; import android.graphics.drawable.Drawable; import android.location.Address; import android.location.Geocoder; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import com.google.android.maps.GeoPoint; import com.google.android.maps.MapActivity; import com.google.android.maps.MapController; import com.google.android.maps.MapView; import com.google.android.maps.Overlay; import com.google.android.maps.OverlayItem; public class LocationActivity extends MapActivity implements LocationListener { /** Called when the activity is first created. */ private static final String TAG = "LocationActivity"; LocationManager locationManager; Geocoder geocoder; TextView locationText; MapView map; MapController mapController; GeoPoint point; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); locationText = (TextView)this.findViewById(R.id.lblLocationInfo); map = (MapView)this.findViewById(R.id.mapview); map.setBuiltInZoomControls(true); mapController = map.getController(); mapController.setZoom(16); locationManager = (LocationManager)this.getSystemService(LOCATION_SERVICE); geocoder = new Geocoder(this); Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); if (location != null) { Log.d(TAG, location.toString()); this.onLocationChanged(location); } } @Override protected void onResume() { super.onResume(); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, this); //<7> } @Override protected void onPause() { super.onPause(); locationManager.removeUpdates(this); //<8> } @Override public void onLocationChanged(Location location) { //<9> Log.d(TAG, "onLocationChanged with location " + location.toString()); String text = String.format("Lat:\t %f\nLong:\t %f\nAlt:\t %f\nBearing:\t %f", location.getLatitude(), location.getLongitude(), location.getAltitude(), location.getBearing()); this.locationText.setText(text); try { List
addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 10); //<10> for (Address address : addresses) { this.locationText.append("\n" + address.getAddressLine(0)); } int latitude = (int)(location.getLatitude() * 1000000); int longitude = (int)(location.getLongitude() * 1000000); point = new GeoPoint(latitude,longitude); mapController.animateTo(point); //<11> // mapController.setCenter(point); map.invalidate(); } catch (IOException e) { Log.e("LocateMe", "Could not get Geocoder data", e); } } @Override protected boolean isRouteDisplayed() { // TODO Auto-generated method stub return false; } @Override public void onProviderDisabled(String arg0) { // TODO Auto-generated method stub } @Override public void onProviderEnabled(String provider) { // TODO Auto-generated method stub } @Override public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub } }