简体   繁体   中英

How can I implement Google Maps through a WebView in Android Studio

I've been experiencing an issue with implementing Google Maps onto my app. With the normal API I get it perfectly done, until I want to make a search. It just doesn't load. Reading across forums it seems to need a Billing Account on Google Cloud to work properly. That's why I'm trying to implement a Google Maps WebView (with, obviously, a functioning search bar) on a certain Activity. I'm not familiarized at all with WebViews so I would appreciate a lot an explanation for dummies. Thanks in advance.

As a side note, I'm not using other threads because most are from about 5 years ago and most of the code is deprecated.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM