谷歌地图lng + lat到隐藏字段不起作用

时间:2010-12-26 00:37:09

标签: javascript google-maps

我正在尝试将Marker数据放入表单上的隐藏字段中。我不确定为什么这不起作用,它必须是我的js语法中的东西:

var initialLocation;
var siberia = new google.maps.LatLng(60, 105);
var newyork = new google.maps.LatLng(40.69847032728747, -73.9514422416687);
var browserSupportFlag =  new Boolean();




function initialize() {
  var myOptions = {
    zoom: 6,
    mapTypeId: google.maps.MapTypeId.HYBRID
  };
  var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

    myListener = google.maps.event.addListener(map, 'click', function(event) {
    placeMarker(event.latLng), google.maps.event.removeListener(myListener);

  });





  // 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 Siberia.");
      initialLocation = siberia;
    }
  }
  function placeMarker(location) {
  var marker = new google.maps.Marker({
      position: location, 
      map: map,
      draggable: true
  });

  map.setCenter(location);


}
}

var lat = latlng.lat();
  var lng = latlng.lng();
document.getElementById("t1").value=lat;
document.getElementById("t2").value=lng;

 <input type="hidden" name="lat" id="t1">
    <input type="hidden" name="long" id="t2">

2 个答案:

答案 0 :(得分:1)

JSLint是一个很棒的工具。

  

第17行的问题30:缺少分号。

placeMarker(event.latLng), google.maps.event.removeListener(myListener);
     

第17行的问题30:预期的标识符,而不是','。

placeMarker(event.latLng), google.maps.event.removeListener(myListener);

将该逗号更改为分号,再次通过JSLint运行并继续运行,直到没有错误为止。

答案 1 :(得分:1)

看看: on jsfiddle


var lat = initialLocation.lat(),lng = initialLocation.lng();