Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: code_hacker on Oct 26th, 2011  |  syntax: Java  |  size: 3.87 KB  |  views: 27  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package com.example.gui;
  2.  
  3.  
  4.  
  5. import java.io.IOException;
  6. import java.util.List;
  7.  
  8. import android.app.Activity;
  9. import android.content.Intent;
  10. import android.graphics.drawable.Drawable;
  11. import android.location.Address;
  12. import android.location.Geocoder;
  13. import android.location.Location;
  14. import android.location.LocationListener;
  15. import android.location.LocationManager;
  16. import android.os.Bundle;
  17. import android.util.Log;
  18. import android.view.View;
  19. import android.widget.Button;
  20. import android.widget.TextView;
  21. import android.widget.Toast;
  22.  
  23.  
  24. import com.google.android.maps.GeoPoint;
  25. import com.google.android.maps.MapActivity;
  26. import com.google.android.maps.MapController;
  27. import com.google.android.maps.MapView;
  28. import com.google.android.maps.Overlay;
  29. import com.google.android.maps.OverlayItem;
  30.  
  31. public class LocationActivity extends MapActivity implements LocationListener {
  32.     /** Called when the activity is first created. */
  33.         private static final String TAG = "LocationActivity";
  34.         LocationManager locationManager;
  35.           Geocoder geocoder;
  36.           TextView locationText;
  37.           MapView map;  
  38.           MapController mapController;
  39.           GeoPoint point;
  40.          
  41.     @Override
  42.     public void onCreate(Bundle savedInstanceState) {
  43.         super.onCreate(savedInstanceState);
  44.         setContentView(R.layout.main);
  45.        
  46.      
  47.        
  48.         locationText = (TextView)this.findViewById(R.id.lblLocationInfo);
  49.         map = (MapView)this.findViewById(R.id.mapview);
  50.         map.setBuiltInZoomControls(true);
  51.        
  52.         mapController = map.getController();
  53.         mapController.setZoom(16);
  54.        
  55.         locationManager = (LocationManager)this.getSystemService(LOCATION_SERVICE);
  56.        
  57.         geocoder = new Geocoder(this);
  58.        
  59.         Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
  60.         if (location != null) {
  61.           Log.d(TAG, location.toString());
  62.           this.onLocationChanged(location);
  63.         }
  64.        
  65.        
  66.        
  67.     }
  68.  
  69.     @Override
  70.     protected void onResume() {
  71.       super.onResume();
  72.       locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, this); //<7>
  73.     }
  74.  
  75.     @Override
  76.     protected void onPause() {
  77.       super.onPause();
  78.       locationManager.removeUpdates(this); //<8>
  79.     }
  80.  
  81.     @Override
  82.     public void onLocationChanged(Location location) { //<9>
  83.       Log.d(TAG, "onLocationChanged with location " + location.toString());
  84.       String text = String.format("Lat:\t %f\nLong:\t %f\nAlt:\t %f\nBearing:\t %f", location.getLatitude(),
  85.                     location.getLongitude(), location.getAltitude(), location.getBearing());
  86.       this.locationText.setText(text);
  87.      
  88.       try {
  89.         List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 10); //<10>
  90.         for (Address address : addresses) {
  91.           this.locationText.append("\n" + address.getAddressLine(0));
  92.         }
  93.        
  94.         int latitude = (int)(location.getLatitude() * 1000000);
  95.         int longitude = (int)(location.getLongitude() * 1000000);
  96.  
  97.         point = new GeoPoint(latitude,longitude);
  98.        
  99.        
  100.        mapController.animateTo(point); //<11>
  101.       //  mapController.setCenter(point);
  102.         map.invalidate();
  103.        
  104.       } catch (IOException e) {
  105.         Log.e("LocateMe", "Could not get Geocoder data", e);
  106.       }
  107.     }
  108.    
  109.         @Override
  110.         protected boolean isRouteDisplayed() {
  111.                 // TODO Auto-generated method stub
  112.                 return false;
  113.         }
  114.  
  115.         @Override
  116.         public void onProviderDisabled(String arg0) {
  117.                 // TODO Auto-generated method stub
  118.                
  119.         }
  120.  
  121.         @Override
  122.         public void onProviderEnabled(String provider) {
  123.                 // TODO Auto-generated method stub
  124.                
  125.         }
  126.  
  127.         @Override
  128.         public void onStatusChanged(String provider, int status, Bundle extras) {
  129.                 // TODO Auto-generated method stub
  130.                
  131.         }
  132. }
  133.