Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid development - GPS not working - Application hangs when co-ordinates sent
    text
    copied!<p><strong>Problem Background</strong>:</p> <p>I am somewhat new to Android development. All I'm trying to do, for now, is to get GPS coordinates from the GPS device of the phone and display them on Google Maps or just in a <code>TextView</code>, or whatever. The main task is to get them.</p> <p>I've read a number of tutorials. I'm using<br> - Android 2.3.3<br> - API level 10<br> - Eclipse 3.6.1<br> - Windows 7</p> <hr> <p><strong>Problem Description</strong> :</p> <p>I have written a class <code>GeoUpdateHandler</code> which implements <code>LocationListener</code> and implements its methods <code>onLocationChange()</code> etc. And in <code>MyMapsActivity</code>, I use the regular <code>requestLocationUpdates</code> of the <code>LocationManager</code> to get periodic updates.</p> <p>When I run the application, I send GPS coordinates from the Emulator Control of Eclipse (Window --> Show View --> Other --> Android --> Emulator Control) to send Longitude and Latitude. As soon as the emulated device gets the coordinates, the application hangs. Nothing happens and the cursor changes to the blue circle (which signifies thinking/stuck) and nothing happens. If I use <code>locationManager.getLastKnownLocation(String provider)</code>, it gets null. Obviously, because there IS no last known location. It gets stuck before knowing one!</p> <p>I have tried sending the coordinates through telnet as well. (cmd --> telnet localhost 5554 --> geo fix . But the same thing happens.</p> <p>I have tried starting the device independent of the application, sending the coordinates, and then starting the application. But the same thing happens when I run the application: it hangs.</p> <p>The following code <code>HelloItemizedOverlay</code> is taken from Android's MapView tutorial and works fine with manually given coordinates. The problem arises when GPS location is tried to be retrieved.</p> <pre><code>public class MyMapsActivity extends MapActivity { private MapController mapController; private MapView mapView; private LocationManager locationManager; LinearLayout linLayout; MapView mView; List&lt;Overlay&gt; mapOverlays; Drawable drawable; HelloItemizedOverlay itemizedOverlay; GeoUpdateHandler handler; Location location; public void onCreate(Bundle bundle) { super.onCreate(bundle); setContentView(R.layout.main); // bind the layout to the activity mView = (MapView) findViewById(R.id.mapview); mView.setBuiltInZoomControls(true); mapOverlays = mView.getOverlays(); drawable = this.getResources().getDrawable(R.drawable.icon); itemizedOverlay = new HelloItemizedOverlay(drawable); mapController = mView.getController(); locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); handler = new GeoUpdateHandler(); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 0, handler); location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); GeoPoint point = new GeoPoint((int)location.getLongitude(),(int)location.getLatitude()); OverlayItem overlayitem = new OverlayItem(point, "", ""); mView.setBuiltInZoomControls(true); itemizedOverlay.addOverlay(overlayitem); mapOverlays.add(itemizedOverlay); mapController.setZoom(8); mapController.animateTo(point); } @Override protected boolean isRouteDisplayed() { return false; } public class GeoUpdateHandler implements LocationListener { @Override public void onLocationChanged(Location location) { int lat = (int) (location.getLatitude()); int lng = (int) (location.getLongitude()); GeoPoint point = new GeoPoint(lat, lng); mapController.animateTo(point); // mapController.setCenter(point); } @Override public void onProviderDisabled(String provider) { } @Override public void onProviderEnabled(String provider) { } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } } @Override protected void onResume() { handler = new GeoUpdateHandler(); locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 0, handler); super.onResume(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload