我错过了从Android webview打开谷歌地图的东西
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我错过了从Android webview打开谷歌地图的东西相关的知识,希望对你有一定的参考价值。
我是android编程环境的新手,正在创建我的第一个应用程序。我想在webview中点击谷歌地图链接时让webview打开谷歌地图。我错过了为什么这仍然打开webview中的链接而不是执行谷歌地图?当它在webview中打开时,它显示目的地正常,但它没有显示我的位置,并说明它无法确定我在显示的页面底部的位置
这是MainActivity
package com.example.micha.myapplication;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.CookieManager;
import android.webkit.DownloadListener;
import android.webkit.GeolocationPermissions;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
/**
* WebViewClient subclass loads all hyperlinks in the existing WebView
*/
public class GeoWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// When user clicks a hyperlink, load in the existing WebView
view.loadUrl(url);
return true;
}
}
/**
* WebChromeClient subclass handles UI-related calls
* Note: think chrome as in decoration, not the Chrome browser
*/
public class GeoWebChromeClient extends WebChromeClient {
@Override
public void onGeolocationPermissionsShowPrompt(String origin,
GeolocationPermissions.Callback callback) {
// Always grant permission since the app itself requires location
// permission and the user has therefore already granted it
callback.invoke(origin, true, false);
}
}
private WebView myWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = findViewById(R.id.webView);
WebView myWebView = findViewById(R.id.webView);
WebSettings webSettings = myWebView.getSettings();
myWebView.setWebViewClient(new GeoWebViewClient());
webSettings.setjavascriptEnabled(true);
myWebView.getSettings().setGeolocationEnabled(true);
CookieManager.getInstance().setAcceptCookie(true);
//webSettings.setDomStorageEnabled(true);
myWebView.clearHistory();
myWebView.clearFormData();
myWebView.clearCache(true);
myWebView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if( url.startsWith("http:") || url.startsWith("https:") ) {
return false;
}
if(url.contains("https://www.google.com/maps/"))
{
Uri gmmIntentUri = Uri.parse(url);
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
if (mapIntent.resolveActivity(getPackageManager()) != null) {
startActivity(mapIntent);
}
return true;
}
// Otherwise allow the OS to handle things like tel, mailto, etc.
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity( intent );
return true;
}
});
myWebView.loadUrl("http://www.example.com");
//THIS IS TO DOWNLOAD THE PDF FILES OR OTHER DOWNLOAD LINKS
myWebView.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
}
});
}
@Override
public void onBackPressed() {
if (myWebView.canGoBack()) {
myWebView.goBack();
return;
}
super.onBackPressed();
}
}
Here is the AndroidManifest.XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.micha.myapplication">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACTION_DIAL"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_principal"
android:label="Admin Tools"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.NoActionBar">
<activity android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
答案
发现了问题。
if( url.startsWith("http:") || url.startsWith("https:") ) {
阻止谷歌地图打开,因为它强制任何带https的网址都保留在网页浏览量中。将其修改为
if( url.startsWith("http:")) {
现在一切都很好。
以上是关于我错过了从Android webview打开谷歌地图的东西的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Flutter Web 中使用谷歌地图库? [关闭]
Android Studio 谷歌地图自定义地图,只想显示自定义地图但利用谷歌地图功能