Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何获取用户';GPS定位_Java_Android_Gps_Location - Fatal编程技术网

Java 如何获取用户';GPS定位

Java 如何获取用户';GPS定位,java,android,gps,location,Java,Android,Gps,Location,我有一个活动,其中有一个文本视图,我想在其中显示用户当前位置和特定地址之间的距离。我有地址的纬度和逻辑性,但我的问题是获取用户的位置。我的目标是当活动被创建时,也就是我想不按任何按钮就得到他的位置的时候。我尝试了多种方法:getLastKnownLocation、onLocation changed等,但返回的值始终为null 需要注意的是,我正在模拟器上运行应用程序 示例代码: protected void onCreate(Bundle savedInstanceState) {

我有一个活动,其中有一个文本视图,我想在其中显示用户当前位置和特定地址之间的距离。我有地址的纬度和逻辑性,但我的问题是获取用户的位置。我的目标是当活动被创建时,也就是我想不按任何按钮就得到他的位置的时候。我尝试了多种方法:getLastKnownLocation、onLocation changed等,但返回的值始终为null

需要注意的是,我正在模拟器上运行应用程序

示例代码:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_business_list);

        Bundle bundleUser = getIntent().getExtras();
        final String owneruser = bundleUser.getString("username");

        Bundle bundleType = getIntent().getExtras();
        String type = bundleType.getString("type");

        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        final TextView t = (TextView) findViewById(R.id.textView2);
        listener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                longitude = location.getLongitude();
                latitude = location.getLatitude();
                t.append("\n " + location.getLongitude() + " " + location.getLatitude());
            }

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

            }

            @Override
            public void onProviderEnabled(String s) {

            }

            @Override
            public void onProviderDisabled(String s) {

                Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(i);
            }
        };
        final Location userLocation = new Location("");
        userLocation.setLatitude(latitude);
        userLocation.setLongitude(longitude);
}

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode){
            case 10:
                update();
                break;
            default:
                break;
        }
    }

    void update(){
        // first check for permissions
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.INTERNET}
                        ,10);
            }
        }
        else {
            locationManager.requestLocationUpdates("gps", 5000, 0, listener);
        }
    }

将您的活动实施到LocationListener:

public class MyActivity extends AppCompatActivity implements LocationListener {
 private Location userLocation = new Location("");


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

      @Override
public void onLocationChanged(Location location) {
    if(location != null){
        userLocation.setLatitude(location.getLatitude());
        userLocation.setLongitude(location.getLongitude());
    }
}

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

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}
}
请尝试以下操作以授予权限:

        if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)) {
            // Show an expanation to the user *asynchronously* -- don't block this thread waiting for the user's response! After the user sees the explanation, try again to request the permission.
        } else {
            // No explanation needed, we can request the permission.
            ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, RC_ACCESS_FINE_LOCATION);
            // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an app-defined int constant. The callback method gets the result of the request.
        }
    }
并将其添加到您的清单中:

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


通过此操作,无需在位置发生变化时按下任何按钮,您就可以获得位置。位置表示我必须在ProviderEnabled上实现抽象方法,我也有,您从哪里获得位置?您可以将其添加到您的代码中吗?@Override public void onLocationChanged(Location-Location){if(Location!=null){double latitude=Location.getLatitude();double longitude=Location.getLongitude();}}我对“implements-LocationListener”有问题它说我必须实现onProviderEnabled,但我已经实现了。。。