Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting longitude and latitude works in emulator but not on device?
    primarykey
    data
    text
    <p>I've been trying to work GPS coordinates but it's been a lot tougher than I thought. After a few hours of trial and error, I've managed to output the latitude and longitude (mocked) for my emulator. Below are the 2 ways I've done it:</p> <p>First way:</p> <pre><code>import java.io.IOException; import java.util.Calendar; import java.util.List; import java.util.Locale; import android.app.Activity; import android.content.Context; import android.location.Address; import android.location.Geocoder; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.widget.TextView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String text = ""; LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); if(location != null) { showMyAddress(location); } final LocationListener locationListener = new LocationListener() { public void onLocationChanged(Location location) { showMyAddress(location); } public void onProviderDisabled(String arg0) { } public void onProviderEnabled(String arg0) { } public void onStatusChanged(String arg0, int arg1, Bundle arg2) { } }; lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 10, locationListener); double longitude = location.getLongitude(); double latitude = location.getLatitude(); Log.i(TAG, "longitude: " + longitude); Log.i(TAG, "latitude: " + latitude); text = "longitude: " + longitude + ", " + "latitude: " + latitude; TextView textView = (TextView) findViewById(R.id.txtvwMain); textView.setText(text); } private void showMyAddress(Location location) { double latitude = location.getLatitude(); double longitude = location.getLongitude(); Geocoder myLocation = new Geocoder(getApplicationContext(), Locale.getDefault()); List&lt;Address&gt; myList; try { myList = myLocation.getFromLocation(latitude, longitude, 1); } catch (IOException e1) { e1.printStackTrace(); } } } </code></pre> <p>Second way:</p> <pre><code>import android.app.Activity; import android.content.Context; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.util.Log; import android.widget.TextView; public class MainActivity extends Activity implements LocationListener { private LocationManager lm; private TextView tv; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.txtvwMain); lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 1, this); Location location = lm.getLastKnownLocation(lm.GPS_PROVIDER); double longitude = location.getLongitude(); double latitude = location.getLatitude(); tv.setText("latitude="+latitude+", longitude="+longitude); } @Override public void onLocationChanged(Location arg0) { String lat = String.valueOf(arg0.getLatitude()); String lon = String.valueOf(arg0.getLongitude()); Log.e("GPS", "location changed: lat="+lat+", lon="+lon); tv.setText("lat="+lat+", lon="+lon); } public void onProviderDisabled(String arg0) { Log.e("GPS", "provider disabled " + arg0); } public void onProviderEnabled(String arg0) { Log.e("GPS", "provider enabled " + arg0); } public void onStatusChanged(String arg0, int arg1, Bundle arg2) { Log.e("GPS", "status changed to " + arg0 + " [" + arg1 + "]"); } } </code></pre> <p>Both ways above work and print out the mock latitude and longitude onto a TextView in the emulator (running Android 4.2.2). However, when I upload the .apk file onto my device (tablet running Android 4.0.4), it just crashes. I don't know what's wrong because I can't see any error messages. What is the source of the problem and how may I go about solving it? Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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