繁体   English   中英

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

[英]Not getting user location using GPS_PROVIDER / NETWORK_PROVIDER

我正在尝试实现一个简单的位置应用,该应用将在单击按钮时显示来自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;
        }
    }
}

此应用程序不显示任何输出。

您可以从覆盖的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" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM