[英]geolocation error: user denied geolocation android
I'm trying to get a map in a fragment through a webview, using the leaflet and mapbox APIs. 我正在尝试使用传单和mapbox API通过Web视图在片段中获取地图。 When I load the fragment, the HTML element loads, but it gives me the error "Geolocation Error: User denied geolocation", and then it shows me a map, but every tile is a map of the whole world.
当我加载片段时,HTML元素加载,但是它给我错误“地理位置错误:用户拒绝地理位置”,然后向我显示地图,但是每个图块都是整个世界的地图。 Can you help me spot where I'm going wrong?
您能帮我找出我要去哪里了吗? I'd really appreciate it.
我真的很感激。
map.html map.html
<!DOCTYPE html>
<html>
<head>
<title>Leaflet mobile example</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="file:///android_asset/leaflet.css" />
<!--[if lte IE 8]><link rel="stylesheet" href="../dist/leaflet.ie.css" /><![endif]-->
<script src="file:///android_asset/leaflet.js"></script>
<style>
body {
padding: 0;
margin: 0;
}
html, body, #map {
height: 100%;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
var map = L.map('map');
L.tileLayer('https://api.tiles.mapbox.com/v4/mapbox.streets/0/0/0.png?access_token=pk.eyJ1IjoicGF4Ymx1ZXJpYmJvbiIsImEiOiJjaWpsdzV0cTUwMDVkdGhtNWpoYThwaDFmIn0.WlqGs3T0nqsgy9HBPDpWWw', {
attribution: 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://mapbox.com">Mapbox</a>',
maxZoom: 18
}).addTo(map);
map.locate({setView: true, maxZoom: 16});
function onLocationFound(e) {
var radius = e.accuracy / 2;
L.marker(e.latlng).addTo(map)
.bindPopup("You are within " + radius + " meters from this point").openPopup();
L.circle(e.latlng, radius).addTo(map);
}
map.on('locationfound', onLocationFound);
function onLocationError(e) {
alert(e.message);
}
map.on('locationerror', onLocationError);
</script>
</body>
</html>
fragment1.java 片段1.java
package me.paxana.alerta.fragment;
import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebViewFragment;
import android.widget.Toast;
import me.paxana.alerta.R;
public class Fragment1 extends Fragment {
private WebView mWebView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment1, container, false);
mWebView= (WebView) rootView.findViewById(R.id.webView);
mWebView.setWebChromeClient(new WebChromeClient());
mWebView.loadUrl("file:///android_asset/www/map.html");
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAppCacheEnabled(true);
webSettings.setDatabaseEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setGeolocationEnabled(true);
// Inflate the layout for this fragment
return rootView;
}
}
try putting these permissions in your AndroidManifest.xml
and see if it fixes it. 请尝试将这些权限放入您的
AndroidManifest.xml
,看看是否可以解决。
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.