首页 > 解决方案 > 如何通过 Android Studio 中的 WebView 实现 Google 地图

问题描述

我在将 Google 地图应用到我的应用时遇到了问题。使用普通的 API,我可以完美地完成它,直到我想进行搜索。它只是不加载。跨论坛阅读似乎需要 Google Cloud 上的结算帐户才能正常工作。这就是为什么我试图在某个 Activity 上实现一个 Google Maps WebView(显然,有一个正常工作的搜索栏)。我对 WebViews 一点也不熟悉,所以我会很感激对傻瓜的解释。提前致谢。

附带说明一下,我没有使用其他线程,因为大多数线程来自大约 5 年前,并且大部分代码已被弃用。

标签: javaandroidgoogle-mapsandroid-webview

解决方案


I implemented on a Constraint Layout a normal Web View. It had the ID of "webView". The following code was used on the Activity itself

package com.example.donafelicidad;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class ComedoresActivity2 extends AppCompatActivity {

    private WebView webView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comedores2);
        webView = (WebView) findViewById(R.id.webView);
        webView.setWebViewClient(new WebViewClient());
        webView.loadUrl("https://www.google.com/maps");

        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
    }

    @Override
    public void onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack();
        } else{
        super.onBackPressed();
    }
}
}

It allows the map to work perfectly (implementation of JavaScript), while also allowing the user to return to a previous state on the Web View (implementation of onBackPressed).


推荐阅读