如何提示用户打开位置?

问题描述 投票:1回答:2

我有一个应用程序,当我们点击一​​个按钮时,应用程序启动带有搜索查询的谷歌地图。但我需要打开位置以提供准确的结果。可能吗?

android google-maps geolocation google-maps-android-api-2
2个回答
2
投票

我理解你的查询是一个代码,应该添加到你的onCreate方法上

LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
    if (!lm.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
            !lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
        // Build the alert dialog
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Location Services Not Active");
        builder.setMessage("Please enable Location Services and GPS");
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int i) {
                // Show location settings when the user acknowledges the alert dialog
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(intent);
            }
        });
        Dialog alertDialog = builder.create();
        alertDialog.setCanceledOnTouchOutside(false);
        alertDialog.show();
    }

0
投票

您应该在AndroidManifest.xml中添加permission

有关Android权限here的一般信息。

请注意Android 6的权限机制已更改(it became iOS like)。

© www.soinside.com 2019 - 2024. All rights reserved.