gpt4 book ai didi

java - Fragment webView无法检测到Google map (您的位置)

转载 作者:行者123 更新时间:2023-12-01 18:41:32 25 4
gpt4 key购买 nike

使用 webView 浏览时,Google map 无法检测到“您的位置”。如何在 webView 环境中支持和启用 GeoLocation,它可以检测您的位置。也许我需要在 Java 上添加额外的代码?我在 fragment 中使用 webView。至此,google map 本身就可以成功加载了。

HomeFragment.java

import android.annotation.TargetApi;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import com.alidmisli.gcsdriver.R;
public class HomeFragment extends Fragment {

public HomeFragment() {

}

WebView webView;
String url = "https://google.com";

@Override
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState)
{

View v = inflater.inflate(R.layout.fragment_home, container, false);
webView = (WebView)v.findViewById(R.id.webView);
webView.loadUrl(url);
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setGeolocationEnabled(true);
settings.setAppCacheEnabled(true);
settings.setDatabaseEnabled(true);
settings.setDomStorageEnabled(true);
webView.setWebViewClient(new MyWebViewClient());
return v;
}


private class MyWebViewClient extends WebViewClient{
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}

@SuppressWarnings("deprecation")
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("mailto:")) {
//Handle mail Urls
startActivity(new Intent(Intent.ACTION_SENDTO, Uri.parse(url)));
} else if (url.startsWith("tel:")) {
//Handle telephony Urls
startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(url)));
} else {
view.loadUrl(url);
}
return true;
}

@TargetApi(Build.VERSION_CODES.N)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
final Uri uri = request.getUrl();
if (uri.toString().startsWith("mailto:")) {
//Handle mail Urls
startActivity(new Intent(Intent.ACTION_SENDTO, uri));
} else if (uri.toString().startsWith("tel:")) {
//Handle telephony Urls
startActivity(new Intent(Intent.ACTION_DIAL, uri));
} else {
//Handle Web Urls
view.loadUrl(uri.toString());
}
return true;
}
}
}

权限使用

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

最佳答案

那么,您检查过权限运行时吗?因为,一些权限会在运行时检查(如位置、相机等)。

关于java - Fragment webView无法检测到Google map (您的位置),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59924302/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com