2011-01-06 43 views
0

我有一个使用W3C geoLocation获取个人位置的Google地图。我想知道如何将他们初始位置的经度和纬度变成隐藏的领域。Google地图V3 - 将geoPostition数据导入隐藏字段

 // 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); 

} 

回答

1

就像我在这个question使用的方法:
getCurrentPosition方法:

populateInputs(initialLocation); 

如果函数看起来像:

function populateInputs(pos) { 
    document.getElementById("t1").value=pos.lat() 
    document.getElementById("t2").value=pos.lng(); 
} 

其中t1和t2是你的隐藏字段

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

...我假设您的网页上只有1个窗体,但您应该可以轻松地调整此窗体。