我们如何获得访问者的位置使用谷歌API

How can we get visitor location using google API

本文关键字:谷歌 API 位置 何获得 访问者 我们      更新时间:2023-09-26
<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返回空值。还有其他方法可以找到访客的位置吗?

try this: HTML5 - Using Geolocation

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