使用GPS_PROVIDER / NETWORK_PROVIDER无法获取用户位置

时间:2015-11-28 16:13:43

标签: java android gps geolocation android-location

我正在尝试实施一个简单的位置应用程序,该应用程序将在按钮单击时显示GPS(如果GPS打开)的用户位置,并在另一个按钮单击时显示NETWORK_PROVIDER中的用户位置。我想要做的是在下面给出 -

AppLocationService.java

package com.android.imran.userlocation;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;

public class AppLocationService extends Service implements LocationListener{

    protected LocationManager locationManager;
    Location location;

    private static final long MIN_DISTANCE_FOR_UPDATE = 10;
    private static final long MIN_TIME_FOR_UPDATE = 1000 * 60 * 2;

    public AppLocationService(Context context) {
        locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
    }

    public Location getLocation(String provider) {
        if(locationManager.isProviderEnabled(provider))
        {
            locationManager.requestLocationUpdates(provider,MIN_TIME_FOR_UPDATE, MIN_DISTANCE_FOR_UPDATE, this);
            if (locationManager != null) {
                location = locationManager.getLastKnownLocation(provider);
                return location;
            }
        }
        return null;
    }

    @Override
    public void onLocationChanged(Location location) {
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

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

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

}

MainActivity.java

package com.android.imran.userlocation;

import android.location.Location;
import android.location.LocationManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    TextView showLocation;
    Button getLocationGPS;
    Button getLocationNW;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        showLocation=(TextView)findViewById(R.id.showLocation);
        getLocationGPS=(Button)findViewById(R.id.getLocationGPS);
        getLocationNW=(Button)findViewById(R.id.getLocationNW);

        getLocationGPS.setOnClickListener(this);
        getLocationNW.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId())
        {
            case R.id.getLocationGPS:

                Location gpsLocation = new AppLocationService(getApplicationContext()).getLocation(LocationManager.GPS_PROVIDER);

                if (gpsLocation != null) {
                    double latitude = gpsLocation.getLatitude();
                    double longitude = gpsLocation.getLongitude();
                    showLocation.setText("Latitude: " + latitude + "\nLongitude: " + longitude);
                    Toast.makeText(getApplicationContext(), "Mobile Location (GPS): \nLatitude: " + latitude + "\nLongitude: " + longitude, Toast.LENGTH_LONG).show();
                }
                break;

            case R.id.getLocationNW:

                Location nwLocation = new AppLocationService(getApplicationContext()).getLocation(LocationManager.NETWORK_PROVIDER);

                if (nwLocation != null) {
                    double latitude = nwLocation.getLatitude();
                    double longitude = nwLocation.getLongitude();
                    showLocation.setText("Latitude: " + latitude + "\nLongitude: " + longitude);
                    Toast.makeText(getApplicationContext(), "Mobile Location (NW): \nLatitude: " + latitude + "\nLongitude: " + longitude, Toast.LENGTH_LONG).show();
                }
                break;
        }
    }
}

此应用不显示任何输出。

1 个答案:

答案 0 :(得分:0)

您可以从覆盖的onLocationChanged方法获取位置

@Override
public void onLocationChanged(Location location) {

    // Listen for location

    double latitude = location.getLatitude();
    double longitude = location.getLongitude();

    Toast.makeText(getApplicationContext(), "Mobile Location (GPS): \nLatitude: " + latitude + "\nLongitude: " + longitude, Toast.LENGTH_LONG).show();
}

编辑:

我如何获得位置的简单代码示例 -

MainActivity.java

private LocationManager locationManager;
private double longitudeGPS, latitudeGPS;
private TextView locationView;
private final LocationListener locationListenerGPS = new LocationListener() {
    public void onLocationChanged(Location location) {
        longitudeGPS = location.getLongitude();
        latitudeGPS = location.getLatitude();

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                String loc = longitudeGPS + " , " + latitudeGPS;
                locationView.setText(loc);
                Toast.makeText(NewComplaintActivity.this, "GPS Provider update", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {

    }

    @Override
    public void onProviderEnabled(String s) {

    }

    @Override
    public void onProviderDisabled(String s) {

    }
}; 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationView = (TextView) findViewById(R.id.location_view);
    if (!checkLocation()){
    } else {
        locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, 1000 * 60, 10, locationListenerGPS);
    }
}

private boolean checkLocation() {
    if (!isLocationEnabled())
        // ask user to enable location 
    return isLocationEnabled();
}

private boolean isLocationEnabled() {
    return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
            locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}

不要忘记添加

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