我有一张Google地图,它使用W3C geoLocation来获取人员位置。我想知道如何将他们的初始位置的lat和lng放入隐藏的区域。
// Try W3C Geolocation (Preferred)
if(navigator.geolocation) {
browserSupportFlag = true;
navigator.geolocation.getCurrentPosition(function(position) {
initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
map.setCenter(initialLocation);
}, function() {
handleNoGeolocation(browserSupportFlag);
});
// Try Google Gears Geolocation
} else if (google.gears) {
browserSupportFlag = true;
var geo = google.gears.factory.create('beta.geolocation');
geo.getCurrentPosition(function(position) {
initialLocation = new google.maps.LatLng(position.latitude,position.longitude);
map.setCenter(initialLocation);
}, function() {
handleNoGeoLocation(browserSupportFlag);
});
// Browser doesn't support Geolocation
} else {
browserSupportFlag = false;
handleNoGeolocation(browserSupportFlag);
}
function handleNoGeolocation(errorFlag) {
if (errorFlag === true) {
alert("Geolocation service failed.");
initialLocation = newyork;
} else {
alert("Your browser doesn't support geolocation. We've placed you in beautiful Minneapolis.");
initialLocation = siberia;
}
map.setCenter(initialLocation);
}
答案 0 :(得分:1)
就像我在question中使用的方法一样:
在getCurrentPosition
方法中:
populateInputs(initialLocation);
功能如下:
function populateInputs(pos) {
document.getElementById("t1").value=pos.lat()
document.getElementById("t2").value=pos.lng();
}
t1和t2是你的隐藏字段
答案 1 :(得分:0)
在获得initialLocation之前(两个地方),您将获得纬度和经度(如:position.coords.latitude,position.coords.longitude或position.latitude,position.longitude)。
只需调用一个函数在该点设置隐藏字段(传入lat和long)。类似的东西:
var setHiddenField = function(lat, long) {
document.forms[0].hiddenLatField.value = lat;
document.forms[0].hiddenLongField.value = long;
}
...我假设您的页面上只有一个表单,但您应该可以轻松调整此表单。