获得自己的位置

时间:2012-04-23 10:13:59

标签: android gps location

  

可能重复:
  How do I get the current GPS location programmatically in Android?

下面有一个代码..这会让我得到我自己的位置..?

public class MyLocationListener implements LocationListener {  

public static double latitude;  
public static double longitude;  

public void onLocationChanged(Location loc)  
{  
    loc.getLatitude();  
    loc.getLongitude();  
    latitude=loc.getLatitude();  
    longitude=loc.getLongitude();  
}  

public void onProviderDisabled(String provider)  
{  
    //print "Currently GPS is Disabled";  
}  
public void onProviderEnabled(String provider)  
{  
    //print "GPS got Enabled";  
}  
public void onStatusChanged(String provider, int status, Bundle extras)  
{  
}  
}  

我知道这已被问过几次..请帮我解释一下代码......

1 个答案:

答案 0 :(得分:0)

您可以使用此代码开始列出GPS更新,并对您收到的数据执行某些操作。它还会在日志文件中打印消息以便于调试。请注意,此功能不会返回任何结果,它只会开始收听GPS 。稍后将在// do something here with the new location data部分提供结果,此时您可以将它们保存在某个地方。

private void getGpsData() {
    locationManager = (LocationManager) owner.getSystemService(Context.LOCATION_SERVICE);

    locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            Log.i(TAG, "location change:" + location.toString());

            String longitude = "Londitude: " + location.getLongitude();
            String latitude = "Latitude: " + location.getLatitude();

            if( location.hasAccuracy() ) {  // good enough?
                // do something here with the new location data
                ....
                //
                Log.i(TAG, "GPS listener done");
                locationManager.removeUpdates(this);    // don't forget this to save battery
            }
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
            Log.i(TAG, provider + " status:" + status);
        }

        public void onProviderEnabled(String provider) {
            Log.i(TAG, provider + " enabled");
        }

        public void onProviderDisabled(String provider) {
            Log.i(TAG, provider + " disabled");
        }
    };
    Log.i(TAG,"GPS listener started");
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener );
}