Advertisement
code_hacker

Untitled

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