如何提示用户打开位置?

时间:2016-04-17 19:59:53

标签: android google-maps geolocation google-maps-android-api-2

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

2 个答案:

答案 0 :(得分: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();
    }

答案 1 :(得分:0)

您应该在AndroidManifest.xml中添加permission

有关Android权限的一般信息here

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