Webview中的当前位置

时间:2015-07-24 16:25:32

标签: android android-webview

我使用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();
        }
        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) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

1 个答案:

答案 0 :(得分:1)

尝试覆盖默认的WebChromeClient并启用地理定位权限,如here所述

    webView.setWebChromeClient(new WebChromeClient(){

       @Override
       public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
        callback.invoke(origin, true, false);
       }
    });