How to insert Runtime permission into a webview that gets the user’s location?

Asked

Viewed 122 times

2

So.. I’m starting to create a webview and the site I’m basing on has an option to get the coordinates from a certain location, on the site this works perfectly, but when I put on APP he opens the Google maps (which is what I want) only that it fails to resolve the current user location to set the route, etc.

Message appears:

"Google Maps did not detect your exact location".

I did a little research and I think just add one Runtime permission when the APP open, saying that I need to access the user’s location, but since I’m a beginner I’m not sure.

public class MainActivity extends AppCompatActivity {

    //chamada para webview
    private WebView wv;

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

        //conditional to check network
        if(!isConnected(MainActivity.this)) buildDialog(MainActivity.this).show();
        else {
            Toast.makeText(MainActivity.this,"Bem vindo.", Toast.LENGTH_SHORT).show();
            setContentView(R.layout.activity_main);
        }

        wv = (WebView) findViewById(R.id.wv);
        wv.getSettings().setJavaScriptEnabled(true);
        wv.setFocusable(true);
        wv.setFocusableInTouchMode(true);
        wv.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        wv.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        wv.getSettings().setDomStorageEnabled(true);
        wv.getSettings().setDatabaseEnabled(true);
        wv.getSettings().setAppCacheEnabled(true);
        wv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        //Carregar URL
        wv.loadUrl("https://www.google.com/");
        
        wv.setWebViewClient(new MyWebViewClient());
    }

    //Check INTERNET connectivity
    public boolean isConnected(Context context) {

        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netinfo = cm.getActiveNetworkInfo();

        if (netinfo != null && netinfo.isConnectedOrConnecting()) {
            android.net.NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            android.net.NetworkInfo mobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

            if((mobile != null && mobile.isConnectedOrConnecting()) || (wifi != null && wifi.isConnectedOrConnecting())) return true;
            else return false;
        } else
            return false;
    }

    public AlertDialog.Builder buildDialog(Context c) {

        AlertDialog.Builder builder = new AlertDialog.Builder(c);
        builder.setTitle("Sem conexão de Internet");
        builder.setMessage("Ative os Dados móveis ou acesse a rede WIFI para navegar. Pressione Ok para sair.");

        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

                finish();
            }
        });

        return builder;
    }

    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.startsWith("tel:")) {
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
                startActivity(intent);
                view.reload();
                return true;
            }

            view.loadUrl(url);
            return false;
        }
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction()==KeyEvent.ACTION_DOWN){
            switch (keyCode){
                case KeyEvent.KEYCODE_BACK:
                    if(wv.canGoBack()){
                        wv.goBack();
                    }
                    else {
                        finish();
                    }
                    return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}

  • What opens is the Google Maps page on your webview or the Google Maps app installed on your device?

  • The Google Maps page inside the webview, but, any of the 2 options is valid in my case. I just want it to work... It doesn’t matter if you open in the Google Maps app installed or inside the webview.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.