简体   繁体   中英

Google maps, on click not adding lat/lng to form

Just a follow up from a previous question. I've got the following form..

     <body onload="initialize()" onunload="GUnload()">

     <form action="welcome.php" method="post">
     Name: <input type="text" name="fname" /><br/>
     <br/>Description: <BR>
     <TEXTAREA NAME="description" COLS=40 ROWS=6></TEXTAREA><br/>
     <div id="map_canvas" style="width: 500px; height: 300px"></div><br/>
     Latitude:<INPUT NAME="lat" SIZE=20 MAXLENGTH=40
     onKeyPress="return numbersonly(this, event)"><br/>
     Longitude:<INPUT NAME="lng" SIZE=20 MAXLENGTH=40
     onKeyPress="return numbersonly(this, event)">
     <input type="submit" />
     </form>

     </body>

and the following Google API javascript...

 <script type="text/javascript">

 function initialize() {
  if (GBrowserIsCompatible()) {
    var map = new GMap2(document.getElementById("map_canvas"));
    map.setCenter(new GLatLng(53.347247,-6.259031), 13);
    map.setUIToDefault();

    GEvent.addListener(map, "click", function(overlay, latLng)
{
    // display the lat/lng in your form's lat/lng fields
    document.getElementById("lat").value = latLng.lat();
    document.getElementById("lng").value = latLng.lng();
});
  }
 }


  </script>

Basically I'm looking to put the lat and long value of a location in the lat + lng fields of the form but it doesn't seem to be copying.. why? what am I doing wrong?

If using document.getElementById(), Your input elements have to have an id of "lat" and "lng", along with having a name of lat and long for when you pass the form.. ie:

<input type='text' id='lat' name='lat'>

You can think of the id as what the DOM uses to identify the element, and the name, as what the browser passes in a GET/POST.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM