2013-07-02 13 views
8

Nota: La fonte originale per l'html citato di seguito è venuto da http://www.w3schools.com/html/tryit.asp?filename=tryhtml5_geolocation_map e Google Maps Javascript V3 geolocation - cannot access initialLocation variable unless alertedGoogle Maps JavaScript GeoLocation - Non funziona su Chrome

ho raccolto tre diverse versioni di esempi di geolocalizzazione. Tutto funziona correttamente in Firefox. Nessuno funziona in Chrome. Ho impostato l'autorizzazione di Chrome per consentire a tutti i siti di utilizzare la mia posizione, ma senza successo. Deve essere un'impostazione di Chrome, ma non quella ovvia: Impostazioni/Mostra impostazioni avanzate/Privacy/Impostazioni contenuti/Posizione/Consenti a tutti i siti di tracciare la mia posizione fisica.

In Firefox vedo la mia posizione corrente dopo essere stato invitato a condividere posizione.

In Chrome I get Utente ha respinto la richiesta di Geolocalizzazione o Errore: il servizio di geolocalizzazione non è riuscito a seconda dell'HTML.

Dato che funziona in Firefox e non in Chrome, penso che debba essere un'impostazione di Chrome, ma non so cosa posso fare che è più permissivo di consentire a tutti i siti di tracciare la mia posizione fisica.

Qui ci sono i tre file HTML5:

geoGoogle.html:

<!DOCTYPE html> 
<html> 
<head> 
    <title>Geolocation</title> 
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> 
    <meta charset="utf-8"> 
    <link href="https://google-developers.appspot.com/maps/documentation/javascript/examples/default.css" rel="stylesheet"> 
    <!-- 
    Include the maps javascript with sensor=true because this code is using a 
    sensor (a GPS locator) to determine the user's location. 
    See: https://developers.google.com/apis/maps/documentation/javascript/basics#SpecifyingSensor 
    --> 
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=true"></script> 

    <script> 
var map; 

function initialize() { 
var mapOptions = { 
    zoom: 6, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
}; 
map = new google.maps.Map(document.getElementById('map-canvas'), 
    mapOptions); 

// Try HTML5 geolocation 
if(navigator.geolocation) { 
    navigator.geolocation.getCurrentPosition(function(position) { 
      initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude); 
      map.setCenter(initialLocation); 
      var infowindow = new google.maps.InfoWindow({ 
      map: map, 
      position: initialLocation , 
      content: 'Location found using W3C standard' 
      }); 
      var marker = new google.maps.Marker({ 
      position: initialLocation, 
      map: map 
      }); 
     }, function() { 
      handleNoGeolocation(true); 
     }); 
} else { 
    // Browser doesn't support Geolocation 
    handleNoGeolocation(false); 
} 
} 

function handleNoGeolocation(errorFlag) { 
if (errorFlag) { 
    var content = 'Error: The Geolocation service failed.'; 
} else { 
    var content = 'Error: Your browser doesn\'t support geolocation.'; 
} 

var options = { 
    map: map, 
    position: new google.maps.LatLng(60, 105), 
    content: content 
}; 

var infowindow = new google.maps.InfoWindow(options); 
map.setCenter(options.position); 
} 

google.maps.event.addDomListener(window, 'load', initialize); 

    </script> 
</head> 
<body> 
    <div id="map-canvas"></div> 
</body> 
</html> 

geoGoogle2.html:

<!DOCTYPE html> 
<html> 
<head> 
    <title>Geolocation</title> 
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> 
    <meta charset="utf-8"> 
    <link href="https://google-developers.appspot.com/maps/documentation/javascript/examples/default.css" rel="stylesheet"> 
    <!-- 
    Include the maps javascript with sensor=true because this code is using a 
    sensor (a GPS locator) to determine the user's location. 
    See: https://developers.google.com/apis/maps/documentation/javascript/basics#SpecifyingSensor 
    --> 
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=true"></script> 

    <script> 
var map; 

function initialize() { 
var mapOptions = { 
    zoom: 6, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
}; 
map = new google.maps.Map(document.getElementById('map-canvas'), 
    mapOptions); 

// Try HTML5 geolocation 
if(navigator.geolocation) { 
    navigator.geolocation.getCurrentPosition(function(position) { 
    var pos = new google.maps.LatLng(position.coords.latitude, 
             position.coords.longitude); 

    var infowindow = new google.maps.InfoWindow({ 
     map: map, 
     position: pos, 
     content: 'Location found using HTML5.' 
    }); 

    map.setCenter(pos); 
    }, function() { 
    handleNoGeolocation(true); 
    }); 
} else { 
    // Browser doesn't support Geolocation 
    handleNoGeolocation(false); 
} 
} 

function handleNoGeolocation(errorFlag) { 
if (errorFlag) { 
    var content = 'Error: The Geolocation service failed.'; 
} else { 
    var content = 'Error: Your browser doesn\'t support geolocation.'; 
} 

var options = { 
    map: map, 
    position: new google.maps.LatLng(60, 105), 
    content: content 
}; 

var infowindow = new google.maps.InfoWindow(options); 
map.setCenter(options.position); 
} 

google.maps.event.addDomListener(window, 'load', initialize); 

    </script> 
</head> 
<body> 
    <div id="map-canvas"></div> 
</body> 
</html> 

geoGoogle3.html:

<!DOCTYPE html> 
<html> 
<body> 
<p id="demo">Click the button to get your position:</p> 
<button onclick="getLocation()">Try It</button> 
<div id="mapholder"></div> 
<script> 
var x=document.getElementById("demo"); 
function getLocation() 
    { 
    if (navigator.geolocation) 
    { 
    navigator.geolocation.getCurrentPosition(showPosition,showError); 
    } 
    else{x.innerHTML="Geolocation is not supported by this browser.";} 
    } 

function showPosition(position) 
    { 
    var latlon=position.coords.latitude+","+position.coords.longitude; 

    var img_url="http://maps.googleapis.com/maps/api/staticmap?center=" 
    +latlon+"&zoom=14&size=400x300&sensor=false"; 
    document.getElementById("mapholder").innerHTML="<img src='"+img_url+"'>"; 
    } 

function showError(error) 
    { 
    switch(error.code) 
    { 
    case error.PERMISSION_DENIED: 
     x.innerHTML="User denied the request for Geolocation." 
     break; 
    case error.POSITION_UNAVAILABLE: 
     x.innerHTML="Location information is unavailable." 
     break; 
    case error.TIMEOUT: 
     x.innerHTML="The request to get user location timed out." 
     break; 
    case error.UNKNOWN_ERROR: 
     x.innerHTML="An unknown error occurred." 
     break; 
    } 
    } 
</script> 
</body> 
</html> 

Grazie

+1

possibile duplicato del [W3C Geolocation API non funziona in Chrome] (http://stackoverflow.com/questions/6181379/w3c-geolocation-api-not-working-in-chrome) –

+0

Sì, è un duplicato. Stavo usando il file: /// ... Grazie David. A proposito, poiché non c'è un segno di spunta verde accanto alla tua risposta, cosa dovrei fare? Dovrei cancellare la domanda? – user825628

+0

Nah, lascia stare. Il tuo codice probabilmente aiuterà qualcuno in fondo alla strada. Se la comunità pensa che non sia necessario, si prenderà cura di lui. –

risposta

8

Chrome e Safari (browser Webkit) non consentono l'API di geolocalizzazione per i file presenti nel file system.

solo creare un'applicazione Web e cercare di accedere con localhost come

http://localhost/geoGoogle.html 
+0

non è ancora in grado di farlo in Safari 5.1.7 – saikiran

0

basta usare HTTPS anziché HTTP e funzionerà

Problemi correlati