2014-06-23 2 views
0
<script type="text/javascript" src="http://www.google.com/jsapi?key=API KEy"></script> 
if(google.loader.ClientLocation) { 
     visitor_lat = google.loader.ClientLocation.latitude; 
     visitor_lon = google.loader.ClientLocation.longitude; 
     visitor_city = google.loader.ClientLocation.address.city; 
    visitor_region = google.loader.ClientLocation.address.region; 
    visitor_country = google.loader.ClientLocation.address.country; 
     visitor_countrycode = google.loader.ClientLocation.address.country_code; 

     document.getElementById('yourinfo').innerHTML = visitor_lat; 

    } 
    else 
    { 

     // We implemented the Maxmind method (See source code) or you may want to just leave a message: 
     document.getElementById('yourinfo').innerHTML = '<p>Whoops we couldnt find you!</p>'; 
    } 

Я использовал этот код для поиска текущего местоположения каждого посетителя. Но google.loader.ClientLocation возвращает значение null. Есть ли другой способ найти местоположение посетителя .Пожалуйста, помогите мне.Как мы можем найти местоположение посетителя с помощью API Google

+1

Не был этот API заброшенные лет назад, браузеры имеют геолокации сейчас? – adeneo

ответ

0

попробовать это: HTML5 - Использование геолокации

<!DOCTYPE html> 
<html> 
<body> 

<p id="demo">Click the button to get your coordinates:</p> 

<button onclick="getLocation()">Try It</button> 

<script> 
var x = document.getElementById("demo"); 
function getLocation() { 
if (navigator.geolocation) { 
    navigator.geolocation.getCurrentPosition(showPosition); 
} else { 
    x.innerHTML = "Geolocation is not supported by this browser."; 
} 
} 

function showPosition(position) { 
x.innerHTML="Latitude: " + position.coords.latitude + 
"<br>Longitude: " + position.coords.longitude; 
} 

Смежные вопросы