我试图通过Google Loader检索当前用户位置。但我不知道为什么它会停止工作。无论何时加载页面,google.loader.ClientLocation都为空,无法获取任何位置。我的代码:
<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 + '"/>'
);
});
}
}
谢谢!