You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WebView loadUrl转HTTPS地址为intent引发ERR_UNKNOWN_URL_SCHEME错误求助

解决Compose WebView加载谷歌地图出现ERR_UNKNOWN_URL_SCHEME错误

问题原因

你使用的谷歌地图URL(http://maps.google.com/maps?z=16&t=h&q=loc:18.2569533+-66.006805&hl=en)在移动设备上会自动重定向到googlemaps://自定义协议,而WebView默认仅支持处理HTTP/HTTPS协议,无法识别这类自定义协议,因此触发ERR_UNKNOWN_URL_SCHEME错误。

解决方案

自定义WebViewClient,重写shouldOverrideUrlLoading方法,拦截并处理自定义协议:

  • 对于googlemaps://协议,调用系统地图应用打开;
  • 对于HTTP/HTTPS协议,继续用WebView加载。

修改后的完整代码

@OptIn(ExperimentalComposeUiApi::class)
@SuppressLint("SetJavaScriptEnabled")
@Composable
fun WebViewScreen(location: LatLng){ 
    val url = "http://maps.google.com/maps?z=16&t=h&q=loc:${location.latitude}+${location.longitude}&hl=${Locale.getDefault().language}"
    val context = LocalContext.current

    AndroidView(
        modifier = Modifier
            .pointerInteropFilter {
                when (it.action) {
                    MotionEvent.ACTION_DOWN -> {}
                    MotionEvent.ACTION_MOVE -> {}
                    MotionEvent.ACTION_UP -> {}
                    else -> false
                }
                true
            },
        factory = {
            WebView(it).apply {
                layoutParams = ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT
                )
                settings.javaScriptEnabled = true
                settings.loadWithOverviewMode = true
                settings.useWideViewPort = true
                settings.setSupportZoom(true)
                
                // 自定义WebViewClient处理协议跳转
                webViewClient = object : WebViewClient() {
                    override fun shouldOverrideUrlLoading(
                        view: WebView?,
                        request: WebResourceRequest?
                    ): Boolean {
                        request?.url?.let { uri ->
                            val urlStr = uri.toString()
                            // 处理googlemaps自定义协议
                            if (urlStr.startsWith("googlemaps://")) {
                                try {
                                    val intent = Intent(Intent.ACTION_VIEW, uri)
                                    context.startActivity(intent)
                                    return true
                                } catch (e: ActivityNotFoundException) {
                                    // 若无地图应用,回退加载网页版
                                    view?.loadUrl(url)
                                    return true
                                }
                            }
                            // HTTP/HTTPS协议正常加载
                            return !(urlStr.startsWith("http://") || urlStr.startsWith("https://"))
                        }
                        return super.shouldOverrideUrlLoading(view, request)
                    }
                }

                loadUrl(url)
            }
        },
        update = { webView ->
            webView.loadUrl(url)
        }
    )
}

额外注意事项

  • 确保在AndroidManifest.xml中添加网络权限:
    <uses-permission android:name="android.permission.INTERNET" />
    
  • 若希望强制加载网页版地图,可在URL后追加&force=web参数,尝试跳过应用跳转。

内容的提问来源于stack exchange,提问作者jetberrocal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 04:47:04