使用 WebView 的当前位置 Android
Current Location Android using WebView
我使用 WebView
为 Android 设计了一个简单的浏览器。一切正常,但当我打开 Google 地图时,我的浏览器无法访问设备的当前位置。我不明白出了什么问题。
同样在清单中,我已经给予了许可 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);
}
}
- JavaScript 必须在
WebView
中启用,使用
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(...) 设置数据库的位置。如果未设置数据库的位置,则持久存储将不可用,但地理定位将继续正常运行,否则。要设置数据库的位置,请使用 ...
webView.getSettings().setGeolocationDatabasePath( context.getFilesDir().getPath() );
我使用 WebView
为 Android 设计了一个简单的浏览器。一切正常,但当我打开 Google 地图时,我的浏览器无法访问设备的当前位置。我不明白出了什么问题。
同样在清单中,我已经给予了许可 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);
}
}
- JavaScript 必须在
WebView
中启用,使用WebSettings.setJavaScriptEnabled(true);
该应用需要权限 ACCESS_FINE_LOCATIONWebView
必须使用自定义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(...) 设置数据库的位置。如果未设置数据库的位置,则持久存储将不可用,但地理定位将继续正常运行,否则。要设置数据库的位置,请使用 ...
webView.getSettings().setGeolocationDatabasePath( context.getFilesDir().getPath() );