如何在Android中通过GPS获取经度和纬度?

时间:2011-06-14 11:41:51

标签: android

我可以通过网络提供商获得经度和纬度,但无法通过GPS获取它。我怎么能这样做?

public void onCreate(Bundle savedInstanceState) {
...
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (mLocation != null) {

            gp1 = getGeoByLocation(mLocation);
            gp2 = gp1;

            refreshMapView();

            if( !mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
                mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 2000,
                        10, mLocationListener);
            }else{
                mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000,
                                10, mLocationListener);
            }

    private GeoPoint getGeoByLocation(Location location) {
        GeoPoint gp = null;
        try {
            if (location != null) {
                double geoLatitude = location.getLatitude() * 1E6;
                double geoLongitude = location.getLongitude() * 1E6;
                gp = new GeoPoint((int) geoLatitude, (int) geoLongitude);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return gp;
    }

1 个答案:

答案 0 :(得分:0)

每10分钟使用GPS进行长时间的拉动。

 //  Des: Start Device's GPS and get current latitude and longitude
    public void GPS() throws IOException {

//  Des: This is a background service and called after every 10 minutes and fetch                    latitude-longitude values
    background = new Thread(new Runnable() {
    @Override
    public void run() {             
        for (int i = 0; i < j; i++) {
            if (ProjectStaticVariable.GPSExit == true ) {
            try {
                Thread.sleep(600000);   //10 minutes
                mainhandler.sendMessage(mainhandler.obtainMessage());
                j++;
            }
            catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException(e);
             }
           }
        }
     }
});

background.start();
mainhandler = new Handler() {
    public void handleMessage(Message msg) {

 //   Check Internet status
        isInternetPresent = cd.isConnectingToInternet();
        if (isInternetPresent) {

            lat_long_Service_flag = true;                   
            mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            mlocListener = new MyLocationListener(getApplicationContext());
            mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 0,  mlocListener);

            mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        }
    }
   };
 }

    //   Des: Location Listener through which we get current latitude and longitude
  public class MyLocationListener implements LocationListener {

public MyLocationListener(Context mContext) {}

public MyLocationListener(Runnable runnable) {}

@Override
public void onLocationChanged(Location loc) {

    longitude = loc.getLongitude();
    latitude = loc.getLatitude();
    final_latitude = Double.toString(latitude);
    final_longitude = Double.toString(longitude);

}

@Override
public void onProviderDisabled(String provider) {
}

@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
  }
 }