如何在android中使用gps或网络获取准确的gps位置

时间:2015-09-08 10:20:09

标签: android

我希望获得最准确的Gps位置与谷歌地图应用程序相同,但在我的应用程序中,它有时不匹配或显示错误的位置

这是我的代码:

private static GPSTracker singletonObject; private static Context mContext;

// flag for GPS status
boolean isGPSEnabled = false;

// flag for network status
boolean isNetworkEnabled = false;

// flag for GPS status
boolean canGetLocation = false;

Location location; // location

// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters

// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 20000; // 20 Seconds

// Declaring a Location Manager
protected LocationManager locationManager;

public static GPSTracker getGPSTrackerObject(Context context) {
    mContext = context;
    if (singletonObject == null) {
        singletonObject = new GPSTracker();
    }
    singletonObject.initializeLocationManager();
    return singletonObject;
}

private GPSTracker() {
    location = null;
    try {
        locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
    } catch (Exception e) {
        e.printStackTrace();
    }

}

private void initializeLocationManager() {
    canGetLocation = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
    location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); // don't Change provider -  Arul

}

private double decimalTrim(double value) {
    try {
        Double d = value;
        String[] splitter = d.toString().split("\\.");
        int integerValues = splitter[0].length();   // Before Decimal Count
        int decimalValues = splitter[1].length();
        if (decimalValues > 5) {
            return Double.parseDouble(new DecimalFormat("##.#####").format(value));
        } else {
            return value;
        }
    }catch(Exception exception){
        exception.printStackTrace();
        return value;
    }
}

/**
 * Function to get latitude
 */
public double getLatitude() {
    if (location != null) {
        return decimalTrim(location.getLatitude());
    }

    return 0.0;
}

/**
 * Function to get longitude
 */
public double getLongitude() {
    if (location != null) {
        return decimalTrim(location.getLongitude());
    }
    return 0.0;
}


public Location getLocation() {
    return location;
}

/**
 * Function to check GPS/wifi enabled
 *
 * @return boolean
 */
public boolean canGetLocation() {
    return this.canGetLocation;
}

/**
 * Function to show settings alert dialog
 * On pressing Settings button will lauch Settings Options
 */
public AlertDialog showSettingsAlert() {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

    // Setting Dialog Title
    alertDialog.setTitle("GPS Settings");

    // Setting Dialog Message
    alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");

    // On pressing Settings button
    alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            mContext.startActivity(intent);
        }
    });

    // on pressing cancel button
    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });
    AlertDialog alert = null;
    // Showing Alert Message
    if (alertDialog != null) {
        alert = alertDialog.show();
    }
    return alert;
}

@Override
public void onLocationChanged(Location location) {
    Log.d("Accuracy", "Loc:" + location.getAccuracy());
    Log.d("speed", "Loc:" + location.getSpeed());
    Log.d("provider","Loc:provider"+location.getProvider());
    Log.d("Time","Loc:time"+location.getTime());
    Log.d("LocationUpdate: ", "Loc:" + location.getLatitude() + "," + location.getLongitude());
    if (location.getAccuracy() < 100.0 && location.getSpeed() < 6.0) {
        this.location = location;

    }
}

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

@Override
public void onProviderDisabled(String provider) {
}

@Override
public void onProviderEnabled(String provider) {
}


@Override
public IBinder onBind(Intent arg0) {
    return null;
}
}

1 个答案:

答案 0 :(得分:0)

mGoogleMap.setMyLocationEnabled(true); 是肯定使用网络时最快最好的方法之一。 此外,还有mGoogleMap.setLocationSource(source);用于更多自定义用途。