且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何获取android移动设备的当前位置的经度和纬度?

更新时间:2023-01-27 17:30:49

prasad尝试使用此代码.

prasad try this code ..by using this i am successfully getting lat long

        private Location location = null;
        private LocationManager locationManager = null;
    locationManager = (LocationManager) context.getSystemService                              (Context.LOCATION_SERVICE);

    Criteria locationCritera = new Criteria();
    locationCritera.setAccuracy(Criteria.ACCURACY_FINE);
    locationCritera.setAltitudeRequired(false);
    locationCritera.setBearingRequired(false);
    locationCritera.setCostAllowed(true);
    locationCritera.setPowerRequirement(Criteria.NO_REQUIREMENT);

    String providerName = locationManager.getBestProvider(locationCritera,
            true);
    location = locationManager.getLastKnownLocation(providerName);
    locationListener = new MyLocationListener();

    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
            0, locationListener);

    currentLocation = context.getSharedPreferences(PREFS_NAME, 0);
    editor = currentLocation.edit();
}
public String getCurrentLatitude() {
    try {
        if (!currentLocation.getString("currentLatitude", "")
                .equalsIgnoreCase(""))
            return currentLocation.getString("currentLatitude", "");
        else if (location.getLatitude() != 0.0)
            return Double.toString(location.getLatitude());
        else
            return "0.0";
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "0.0";
}

public String getCurrentLongitude() {
    try {


        if (!currentLocation.getString("currentLongitude", "")
                .equalsIgnoreCase(""))
            return currentLocation.getString("currentLongitude", "");
        else if (location.getLongitude() != 0.0)
            return Double.toString(location.getLongitude());
        else
            return "0.0";
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "0.0";
}