什么是我可以获得用户X坐标的最简单方法?

时间:2018-03-10 00:35:41

标签: java android

我试图获取用户的x坐标位置。我不想要活动片段或地图,我只想要坐标编号。我已经查看了stackoverflow,看来大多数方法对于像我这样的初学者来说都非常复杂,或者需要地图活动。

目前,我的代码

TextView textView = (TextView) findViewById(R.id.tView);

    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        return;
    }
    Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    double longitude = location.getLongitude();
    double latitude = location.getLatitude();

    textView.setText(String.valueOf(longitude) + " , " + String.valueOf(latitude));

这似乎应该有效。然而,它崩溃了应用程序。

1 个答案:

答案 0 :(得分:0)

在您检查到实际上没有它之后,您忘记请求许可。因此,当您尝试获取最后一个已知位置时,应用程序崩溃了。

您可以通过调用以下方法来请求权限(要进一步了解参数,请查看documentation):

ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);

可以在 onRequestPermissionsResult 方法中操纵对您的权限请求的响应。

这是一个混乱的,尽管是一个有效的代码片段:

public class MainActivity extends AppCompatActivity {
    private LocationManager lm;
    private TextView textView;

    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
            if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                double longitude = location.getLongitude();
                double latitude = location.getLatitude();
                textView.setText(String.valueOf(longitude) + " , " + String.valueOf(latitude));
            }
    }

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

        textView = (TextView) findViewById(R.id.tView);
        lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
        } else {
            Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            double longitude = location.getLongitude();
            double latitude = location.getLatitude();
            textView.setText(String.valueOf(longitude) + " , " + String.valueOf(latitude));
        }
    }
}

请注意,该位置仅在重新启动应用时更新,因为上述代码中没有实现位置监听器。

我还要提醒您不要忘记修改清单文件,说明您在应用中使用的权限。