2016-01-19 389 views
0

我試圖通過webview使用傳單和mapbox API在片段中獲取地圖。當我加載片段時,會加載HTML元素,但它會給我提供錯誤「地理位置錯誤:用戶拒絕地理定位」,然後向我顯示地圖,但每個地圖都是整個世界的地圖。你能幫我發現我要去哪裏嗎?我真的很感激它。地理位置錯誤:用戶拒絕地理位置android

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 &copy; <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

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; 
    } 

} 
+1

您是否在應用清單中請求了位置權限? – chrki

回答

0

嘗試把這些權限在您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" />