1

My problem is that I can't not get GPS coordinates by using LocationManager class and LocationListener. Maybe if I show you my code, you will understand. 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?

Robotnik
  • 3,643
  • 3
  • 31
  • 49
Kenny Cao
  • 11
  • 1

0 Answers0