Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: None  |  size: 9.94 KB  |  hits: 33  |  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. Can't get current location using gps
  2. public class atm_atmogan extends MapActivity {
  3.  
  4. private LocationManager myLocationManager;
  5. private LocationListener myLocationListener;
  6. private MapView myMapView;
  7. private MapController myMapController;
  8.  
  9. @Override
  10. public void onCreate(Bundle savedInstanceState) {
  11.     super.onCreate(savedInstanceState);
  12.     setContentView(R.layout.atm_atmogan);
  13.     myMapView = (MapView)findViewById(R.id.map_view);
  14.     myMapView.setTraffic(true);
  15.     myMapController = myMapView.getController();
  16.  
  17.     myLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
  18.     myLocationListener = new MyLocationListener();
  19.     myLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,myLocationListener);
  20.  
  21.     //Get the current location in start-up
  22.     double lat = myLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLatitude();
  23.     double lot = myLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLongitude();
  24.     GeoPoint initGeoPoint = new GeoPoint((int)(lat*1E6),(int)(lot*1E6));
  25.     myMapController.animateTo(initGeoPoint);
  26.     myMapController.setZoom(18);
  27. }
  28.  
  29. private class MyLocationListener implements LocationListener{
  30.     public void onLocationChanged(Location loc) {
  31.         GeoPoint myGeoPoint = new GeoPoint((int)(loc.getLatitude()*1E6),(int)(loc.getLongitude()*1E6));
  32.         myMapController.animateTo(myGeoPoint);
  33.         myMapController.setZoom(18);
  34.     }
  35.  
  36.     public void onProviderDisabled(String provider) {
  37.         }
  38.  
  39.     public void onProviderEnabled(String provider) {
  40.         }
  41.  
  42.     public void onStatusChanged(String provider,int status, Bundle extras){
  43.         }
  44. }
  45.  
  46. @Override
  47. protected void onPause() {
  48.     super.onPause();
  49.     myLocationManager.removeUpdates(myLocationListener);
  50. }
  51.  
  52. @Override
  53. protected void onResume() {
  54.     super.onResume();
  55.     myLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,myLocationListener);
  56. }
  57.  
  58. @Override
  59. protected boolean isRouteDisplayed() {
  60.     return false;
  61. };
  62. }
  63.        
  64. 02-08 14:31:15.481: D/dalvikvm(2292): GC freed 727 objects / 60152 bytes in 148ms
  65. 02-08 14:31:17.012: D/dalvikvm(2292): GC freed 143 objects / 7752 bytes in 68ms
  66. 02-08 14:39:10.801: W/KeyCharacterMap(2292): No keyboard for id 0
  67. 02-08 14:39:10.801: W/KeyCharacterMap(2292): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
  68. 02-08 14:39:17.081: E/ActivityThread(2292): Failed to find provider info for com.google.settings
  69. 02-08 14:39:17.091: E/ActivityThread(2292): Failed to find provider info for com.google.settings
  70. 02-08 14:39:17.112: E/ActivityThread(2292): Failed to find provider info for com.google.settings
  71. 02-08 14:39:17.372: D/LocationManager(2292): Constructor: service = android.location.ILocationManager$Stub$Proxy@44ebe658
  72. 02-08 14:39:17.683: I/MapActivity(2292): Handling network change notification:CONNECTED
  73. 02-08 14:39:17.683: E/MapActivity(2292): Couldn't get connection factory client
  74. 02-08 14:39:20.012: D/dalvikvm(2292): GC freed 5768 objects / 476800 bytes in 87ms
  75. 02-08 14:39:20.731: W/KeyCharacterMap(2292): No keyboard for id 0
  76. 02-08 14:39:20.731: W/KeyCharacterMap(2292): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
  77. 02-08 14:39:20.791:  D/LocationManager(2292): removeUpdates: listener = tim_atm.namespace.atm_atmogan$MyLocationListener@44ec2170
  78. 02-08 14:39:21.172: D/dalvikvm(2292): threadid=23 wakeup: interrupted
  79.        
  80. public class MyMapClass extends MapActivity {
  81. MapController mapController;
  82. MyPositionOverlay positionOverlay;
  83.  
  84. /** Called when the activity is first created. */
  85. @Override
  86. public void onCreate(Bundle savedInstanceState) {
  87.     super.onCreate(savedInstanceState);
  88.     setContentView(R.layout.main);
  89.     MapView myMapView=(MapView)findViewById(R.id.myMapView);
  90.     myMapView.setSatellite(true);
  91.     myMapView.setBuiltInZoomControls(true);
  92.     mapController=myMapView.getController();
  93.     mapController.setZoom(17);
  94.     positionOverlay = new MyPositionOverlay();
  95.     List<Overlay> overlays = myMapView.getOverlays();
  96.     overlays.add(positionOverlay);
  97.  
  98.          LocationManager locationManager;
  99.         String context=Context.LOCATION_SERVICE;
  100.         locationManager=(LocationManager)getSystemService(context);
  101.  
  102.         Criteria criteria = new Criteria();
  103.         criteria.setAccuracy(Criteria.ACCURACY_FINE);
  104.         criteria.setAltitudeRequired(false);
  105.         criteria.setBearingRequired(false);
  106.         criteria.setCostAllowed(true);
  107.         criteria.setPowerRequirement(Criteria.POWER_LOW);
  108.         String provider = locationManager.getBestProvider(criteria, true);
  109.         Location location = locationManager.getLastKnownLocation(provider);
  110.  
  111.         final LocationListener locationListener = new LocationListener() {
  112.             public void onLocationChanged(Location location) {
  113.             updateWithNewLocation(location);
  114.             }
  115.             public void onProviderDisabled(String provider){
  116.             updateWithNewLocation(null);
  117.             }
  118.             public void onProviderEnabled(String provider){ }
  119.             public void onStatusChanged(String provider, int status,
  120.             Bundle extras){ }
  121.             };
  122.  
  123.         updateWithNewLocation(location);
  124.         locationManager.requestLocationUpdates(provider, 2000, 10,
  125.                 locationListener);  
  126. }
  127. private void updateWithNewLocation(Location location) {
  128.     if(location!=null)  {
  129.         // Update the map location.
  130.         positionOverlay.setLocation(location);
  131.         Double geoLat = location.getLatitude()*1E6;
  132.         Double geoLng = location.getLongitude()*1E6;
  133.         GeoPoint point = new GeoPoint(geoLat.intValue(),
  134.         geoLng.intValue());
  135.         mapController.animateTo(point);
  136.  
  137.     }
  138. }  
  139. @Override
  140. protected boolean isRouteDisplayed() {
  141.  
  142.     return true;
  143. }
  144.        
  145. public class MyPositionOverlay extends Overlay {
  146. Location location;
  147. private final int mRadius = 5;
  148. @Override
  149. public void draw(Canvas canvas, MapView mapView, boolean shadow) {
  150.   Projection projection = mapView.getProjection();
  151.   if (shadow == false) {
  152.   // Get the current location
  153.   Double latitude = location.getLatitude()*1E6;
  154.   Double longitude = location.getLongitude()*1E6;
  155.   GeoPoint geoPoint;
  156.   geoPoint = new GeoPoint(latitude.intValue(),longitude.intValue());
  157.   // Convert the location to screen pixels
  158.   Point point = new Point();
  159.   projection.toPixels(geoPoint, point);
  160.   RectF oval = new RectF(point.x - mRadius, point.y - mRadius,
  161.   point.x + mRadius, point.y + mRadius);
  162.   // Setup the paint
  163.   Paint paint = new Paint();
  164.   paint.setARGB(250, 255, 0, 0);
  165.   paint.setAntiAlias(true);
  166.   paint.setFakeBoldText(true);
  167.   Paint backPaint = new Paint();
  168.   backPaint.setARGB(175, 50, 50, 50);
  169.   backPaint.setAntiAlias(true);
  170.   RectF backRect = new RectF(point.x + 2 + mRadius,
  171.   point.y - 3*mRadius,
  172.   point.x + 65, point.y + mRadius);
  173.   // Draw the marker
  174.   canvas.drawOval(oval, paint);
  175.   canvas.drawRoundRect(backRect, 5, 5, backPaint);
  176.   canvas.drawText("Here I Am", point.x + 2*mRadius, point.y, paint);
  177.   }
  178.   super.draw(canvas, mapView, shadow);
  179. }
  180. @Override
  181. public boolean onTap(GeoPoint point, MapView mapView) {
  182. return false;
  183. }
  184. public Location getLocation() {
  185.   return location;
  186.   }
  187.   public void setLocation(Location location) {
  188.   this.location = location;
  189.   }
  190.        
  191. public class LocationDemo extends Activity implements LocationListener {
  192.     private static final String TAG = "LocationDemo";
  193.     private static final String[] S = { "Out of Service", "Temporarily Unavailable", "Available" };
  194.  
  195.     private TextView output;
  196.     private LocationManager locationManager;
  197.     private String bestProvider;
  198.  
  199.     @Override
  200.     public void onCreate(Bundle savedInstanceState) {
  201.         super.onCreate(savedInstanceState);
  202.         setContentView(R.layout.main);
  203.  
  204.         // Get the output UI
  205.         output = (TextView) findViewById(R.id.output);
  206.  
  207.         // Get the location manager
  208.         locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
  209.  
  210.         // List all providers:
  211.         List<String> providers = locationManager.getAllProviders();
  212.         for (String provider : providers) {
  213.             printProvider(provider);
  214.         }
  215.  
  216.         Criteria criteria = new Criteria();
  217.         bestProvider = locationManager.getBestProvider(criteria, false);
  218.         output.append("nnBEST Provider:n");
  219.         printProvider(bestProvider);
  220.  
  221.         output.append("nnLocations (starting with last known):");
  222.         Location location = locationManager.getLastKnownLocation(bestProvider);
  223.         printLocation(location);
  224.     }
  225.  
  226.     /** Register for the updates when Activity is in foreground */
  227.     @Override
  228.     protected void onResume() {
  229.         super.onResume();
  230.         locationManager.requestLocationUpdates(bestProvider, 20000, 1, this);
  231.     }
  232.  
  233.     /** Stop the updates when Activity is paused */
  234.     @Override
  235.     protected void onPause() {
  236.         super.onPause();
  237.         locationManager.removeUpdates(this);
  238.     }
  239.  
  240.     public void onLocationChanged(Location location) {
  241.         printLocation(location);
  242.     }
  243.  
  244.     public void onProviderDisabled(String provider) {
  245.         // let okProvider be bestProvider
  246.         // re-register for updates
  247.         output.append("nnProvider Disabled: " + provider);
  248.     }
  249.  
  250.     public void onProviderEnabled(String provider) {
  251.         // is provider better than bestProvider?
  252.         // is yes, bestProvider = provider
  253.         output.append("nnProvider Enabled: " + provider);
  254.     }
  255.  
  256.     public void onStatusChanged(String provider, int status, Bundle extras) {
  257.         output.append("nnProvider Status Changed: " + provider + ", Status="
  258.                 + S[status] + ", Extras=" + extras);
  259.     }
  260.  
  261.     private void printProvider(String provider) {
  262.         LocationProvider info = locationManager.getProvider(provider);
  263.         output.append(info.toString() + "nn");
  264.     }
  265.  
  266.     private void printLocation(Location location) {
  267.         if (location == null)
  268.             output.append("nLocation[unknown]nn");
  269.         else
  270.             output.append("nn" + location.toString());
  271.     }
  272. }
  273.        
  274. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  275. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />