我正在尝试通过 Google Loader 检索当前用户位置。但我不知道为什么它停止工作。任何时候加载页面时,google.loader.ClientLocation 都是 null 并且无法获取任何位置。我的代码:
<script type="text/javascript" src="https://www.google.com/jsapi?key=MY_APP_KEY"></script>
然后是javascript代码:
google.load("search", "1", {callback: initialize});
function initialize() {
if (google.loader.ClientLocation) {
var userCountry = google.loader.ClientLocation.address.country;
var userLocation = google.loader.ClientLocation.address.city;
var userRegion = google.loader.ClientLocation.address.region;
var userCountryCode = google.loader.ClientLocation.address.country_code;
$.ajax({
url: "${createLink(controller: 'util', action: 'userLocation')}",
data: { city: userLocation, region: userRegion, country: userCountry, countryCode: userCountryCode }
})
.done(function(data) {
$("#userLocation").html(userLocation + ", " + userRegion + " - " + userCountry +
' <img src="' + data + '"/>'
);
});
}
}
谢谢!