没有得到的位置

时间:2015-05-26 08:13:13

标签: android gps location locationmanager

我正在制作一个应用程序,其中我必须显示启动画面1/2秒。虽然加载启动我必须得到用户location.location准确性不关心。所以为此我写了一个代码但总是我从位置得到零。

代码

private Context context;

    private LocationManager locationManager;

    Location location;

    double latitude;
    double longitude;

    public GetLocation(Context context) {
        this.context = context;
        getLocation();
    }


    private Location getLocation() {


        locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        location = locationManager.getLastKnownLocation(getProviderName());
        if (location == null) {

            locationManager.requestLocationUpdates(getProviderName(), 0, 0, this);
        }

        return location;


    }


    String getProviderName() {
         locationManager = (LocationManager) context
                .getSystemService(Context.LOCATION_SERVICE);

        Criteria criteria = new Criteria();
        criteria.setPowerRequirement(Criteria.POWER_LOW); // Chose your desired power consumption level.
        criteria.setAccuracy(Criteria.ACCURACY_FINE); // Choose your accuracy requirement.
        criteria.setSpeedRequired(true); // Chose if speed for first location fix is required.
        criteria.setAltitudeRequired(false); // Choose if you use altitude.
        criteria.setBearingRequired(false); // Choose if you use bearing.
        criteria.setCostAllowed(false); // Choose if this provider can waste money :-)

        // Provide your criteria and flag enabledOnly that tells
        // LocationManager only to return active providers.
        return locationManager.getBestProvider(criteria, true);
    }

    @Override
    public void onLocationChanged(Location location) {

        Toast.makeText(context,""+location,Toast.LENGTH_LONG).show();

    }

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

        Toast.makeText(context,provider,Toast.LENGTH_LONG).show();

    }

    @Override
    public void onProviderEnabled(String provider) {

        Toast.makeText(context,provider,Toast.LENGTH_LONG).show();

    }

    @Override
    public void onProviderDisabled(String provider) {

        Toast.makeText(context,provider,Toast.LENGTH_LONG).show();

    }

    public void stopUsingGPS() {
        if (locationManager != null) {
            locationManager.removeUpdates(this);
        }
    }

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

        // return latitude
        return latitude;
    }

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

        // return longitude
        return longitude;
    }

权限

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

用法

private void getLocation() {

        getLocation = new GetLocation(contextActivity);

        latitude = getLocation.getLatitude();
        longitude = getLocation.getLongitude();

        if (latitude != 0 && longitude != 0) {

            getLocation.stopUsingGPS();
        }
    }

请帮帮我。 感谢。

2 个答案:

答案 0 :(得分:1)

如果尚未获取位置,则

getLastKnownLocation返回null。如果适用于您的设备,请使用以下代码段

if (location == null) {
    locationManager.requestLocationUpdates(getProviderName(), 0, 0, this);
}

阻止您注册位置管理员的LocationListener回调。

答案 1 :(得分:0)

我猜你没有在onLocationChanged中更新位置。您是否在Toast文本中获得了位置非null对象?

我的答案是使用onLocationChange事件收到的位置更新location member变量。