Advertisement
code_hacker

Untitled

Oct 20th, 2011
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.50 KB | None | 0 0
  1. package com.example.Maps;
  2.  
  3. import java.io.IOException;
  4. import java.util.List;
  5.  
  6. import android.app.Activity;
  7. import android.location.Address;
  8. import android.location.Geocoder;
  9. import android.location.Location;
  10. import android.location.LocationListener;
  11. import android.location.LocationManager;
  12. import android.os.Bundle;
  13. import android.util.Log;
  14. import android.widget.TextView;
  15. import android.widget.Toast;
  16.  
  17. import com.google.android.maps.GeoPoint;
  18. import com.google.android.maps.MapActivity;
  19. import com.google.android.maps.MapController;
  20. import com.google.android.maps.MapView;
  21.  
  22. public class LocationActivity extends MapActivity implements LocationListener {
  23.     /** Called when the activity is first created. */
  24.     private static final String TAG = "LocationActivity";
  25.     LocationManager locationManager;
  26.       Geocoder geocoder;
  27.       TextView locationText;
  28.       MapView map;  
  29.       MapController mapController;
  30.      
  31.     @Override
  32.     public void onCreate(Bundle savedInstanceState) {
  33.         super.onCreate(savedInstanceState);
  34.         setContentView(R.layout.main);
  35.         locationText = (TextView)this.findViewById(R.id.lblLocationInfo);
  36.         map = (MapView)this.findViewById(R.id.mapview);
  37.         map.setBuiltInZoomControls(true);
  38.        
  39.         mapController = map.getController();
  40.         mapController.setZoom(16);
  41.        
  42.         locationManager = (LocationManager)this.getSystemService(LOCATION_SERVICE);
  43.        
  44.         geocoder = new Geocoder(this);
  45.        
  46.         Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
  47.         if (location != null) {
  48.           Log.d(TAG, location.toString());
  49.           this.onLocationChanged(location);
  50.         }
  51.     }
  52.  
  53.     @Override
  54.     protected void onResume() {
  55.       super.onResume();
  56.       locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, this); //<7>
  57.     }
  58.  
  59.     @Override
  60.     protected void onPause() {
  61.       super.onPause();
  62.       locationManager.removeUpdates(this); //<8>
  63.     }
  64.  
  65.     @Override
  66.     public void onLocationChanged(Location location) { //<9>
  67.       Log.d(TAG, "onLocationChanged with location " + location.toString());
  68.       String text = String.format("Lat:\t %f\nLong:\t %f\nAlt:\t %f\nBearing:\t %f", location.getLatitude(),
  69.                     location.getLongitude(), location.getAltitude(), location.getBearing());
  70.       this.locationText.setText(text);
  71.      
  72.       try {
  73.         List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 10); //<10>
  74.         for (Address address : addresses) {
  75.           this.locationText.append("\n" + address.getAddressLine(0));
  76.         }
  77.        
  78.         int latitude = (int)(location.getLatitude() * 1000000);
  79.         int longitude = (int)(location.getLongitude() * 1000000);
  80.  
  81.         GeoPoint point = new GeoPoint(latitude,longitude);
  82.         mapController.animateTo(point); //<11>
  83.        
  84.       } catch (IOException e) {
  85.         Log.e("LocateMe", "Could not get Geocoder data", e);
  86.       }
  87.     }
  88.    
  89.     @Override
  90.     protected boolean isRouteDisplayed() {
  91.         // TODO Auto-generated method stub
  92.         return false;
  93.     }
  94.  
  95.     @Override
  96.     public void onProviderDisabled(String arg0) {
  97.         // TODO Auto-generated method stub
  98.        
  99.     }
  100.  
  101.     @Override
  102.     public void onProviderEnabled(String provider) {
  103.         // TODO Auto-generated method stub
  104.        
  105.     }
  106.  
  107.     @Override
  108.     public void onStatusChanged(String provider, int status, Bundle extras) {
  109.         // TODO Auto-generated method stub
  110.        
  111.     }
  112. }
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement