【问题标题】:Current Location Android using WebView当前位置 Android 使用 WebView
【发布时间】:2018-09-17 14:07:27
【问题描述】:

我使用WebView 为Android 设计了一个简单的浏览器。一切正常,但是当我打开谷歌地图时,我的浏览器无法访问设备的当前位置。我不明白出了什么问题。 同样在清单中,我已授予 ACCESS FINE LOCATION
许可 我的代码是:

@SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends Activity  {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final WebView ourBrow=(WebView)findViewById(R.id.wvBrowser);
        Button bgo=(Button)findViewById(R.id.button3);
        Button res=(Button)findViewById(R.id.button4);
        Button gfo=(Button)findViewById(R.id.button2);
        ourBrow.getSettings().setJavaScriptEnabled(true);

        try{
            ourBrow.loadUrl("https://www.google.co.in/maps/dir///@20.3464436,85.8127819,15z");
        }
        catch(Exception e)
        {
            e.printStackTrace();
            /*String myHtml = "<html><body>A very basic <b>WebView</b> demo!</body></html>";
            ourBrow.loadData(myHtml, "text/html", null);*/
        }
        bgo.setOnClickListener(new View.OnClickListener() { 
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(ourBrow.canGoBack())
                    ourBrow.goBack();
            }
        });
        gfo.setOnClickListener(new View.OnClickListener() { 
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(ourBrow.canGoForward())
                    ourBrow.goForward();
            }
        });
        res.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                ourBrow.loadUrl("https://www.google.co.in/maps/dir///@20.3464436,85.8127819,15z");
            }
        });
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

【问题讨论】:

  • 如果您不发布代码来解释问题区域,没有人会理解并且可以帮助您!
  • 欢迎来到 Stack Overflow!事实上,发生这种情况可能有很多原因;最好在提问时向我们提供Minimal, Complete, Verifiable Example。这有助于我们帮助您。附带说明一下,您是否正确设置了权限?
  • 谢谢你,先生,我是堆栈溢出的新手

标签: android android-webview


【解决方案1】:
  • 必须在WebView 中启用JavaScript,使用 WebSettings.setJavaScriptEnabled(true); 应用需要权限 ACCESS_FINE_LOCATION WebView必须使用自定义WebChromeClient 实现 WebChromeClient.onGeolocationPermissionsShowPrompt()。这种方法 由 WebView 调用以获取公开用户的权限 位置到 JavaScript。 (在浏览器的情况下,我们显示一个提示 给用户。)默认实现什么都不做,所以权限 永远不会获得,并且永远不会将位置传递给 JavaScript。webView.setWebChromeClient(new WebChromeClient() { public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) { callback.invoke(origin, true, false); } });

地理定位使用数据库在会话之间保持缓存的位置和权限。使用 WebSettings.setGeolocationDatabasePath(...) 设置数据库的位置。如果未设置数据库的位置,则持久存储将不可用,但 Geolocation 将继续正常工作,否则。要设置数据库的位置,请使用 ...

webView.getSettings().setGeolocationDatabasePath( context.getFilesDir().getPath() );

【讨论】:

    猜你喜欢
    • 2023-03-15
    • 1970-01-01
    • 2011-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多