Reputation: 107
My problem is that I cannot get GPS coordinates by using LocationManager class and LocationListener. This is the code I call in OnCreate():
locMgr = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
currentLocListener = new MyLocationListener();
locMgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, currentLocListener);
locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, currentLocListener);
And I have a class MyLocationListener that implements class LocationListener
public class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
String coordinates[] = {""+location.getLatitude(),""+location.getLongitude()};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
GeoPoint p = new GeoPoint((int)(lat*1E6), (int)(lng*1E6));
mc.animateTo(p);
mc.setZoom(19);
MyMapOverlays marker = new MyMapOverlays(p);
List<Overlay> listofOverLays = mapview.getOverlays();
listofOverLays.clear();
listofOverLays.add(marker);
mapview.invalidate();
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "GPS is disabled", Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "GPS is Enabled", Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}}
I can get the location by mocking location in Emulator Control in DDMS. So I try to test on my real phone. when I install it on my phone, it can't get the current location automatically although I turned on GPS and wifi.
I try to run another google map app from market on to my real phone, it worked fine. I don't understand what is wrong with the code...can anyone help me?
LOGCAT & DEBUG
DalvikVM[localhost:8631]
Thread [<1> main] (Suspended (exception NullPointerException))
DashboardActivity$MyLocationListener.onLocationChanged(Location) line: 75
LocationManager$ListenerTransport._handleMessage(Message) line: 191
LocationManager$ListenerTransport.access$000(LocationManager$ListenerTransport, Message) line: 124
LocationManager$ListenerTransport$1.handleMessage(Message) line: 140
LocationManager$ListenerTransport$1(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 123
ActivityThread.main(String[]) line: 4627
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 521
ZygoteInit$MethodAndArgsCaller.run() line: 868
ZygoteInit.main(String[]) line: 626
NativeStart.main(String[]) line: not available [native method]
Thread [<6> Binder Thread #2] (Running)
Thread [<5> Binder Thread #1] (Running)
Thread [<7> MapService] (Running)
Thread [<8> TrafficService] (Running)
Daemon Thread [<10> [email protected]@4610b970] (Running)
LINE 75 giving error.
Upvotes: 2
Views: 2371
Reputation: 11756
Since you're also requesting updates for the NETWORK_PROVIDER, you also need to include the permission:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
My guess is that the line:
locMgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, currentLocListener);
...is throwing a SecurityException and therefore the following line that registers the GPS_PROVIDER is never executed.
To troubleshoot, I would recommend commenting out the line that requests updates from the NETWORK_PROVIDER, and then see if GPS works on a device. Or, you can try including the above permission in your manifest, and then checking to make sure that the Locations you're receiving in the onLocationChanged() method are from the GPS_PROVIDER.
Also, try eliminating the first line where you're converting to String array. Instead, directly assign values to variables:
double lat = location.getLatitude();
double lng = location.getLongitude();
Upvotes: 2