1

so I'm looking for a way to pass the latitude and longitude from google maps api to a php textfield. So far I have the following code but I'm stuck, any help would be much appreciated.

<!DOCTYPE html>
<html>
<head>
<script src="https://maps.googleapis.com/maps/api/js?sensor=false">
</script>

<script>
function initialize()
{
var mapProp = {
center:new google.maps.LatLng(51.8978719,-8.471087399999988),  
zoom:12,
mapTypeId:google.maps.MapTypeId.ROADMAP
};
var map=new google.maps.Map(document.getElementById("googleMap")
,mapProp);

citMarker=new google.maps.Marker({
position:new google.maps.LatLng(51.885403000000000000,-8.534554100000037000),
animation:google.maps.Animation.BOUNCE
});

var infowindow = new google.maps.InfoWindow({
content:'<b>Cork IT</b>'
});

infowindow.open(map,citMarker);
citMarker.setMap(map);

google.maps.event.addListener(citMarker, 'click', function() {
map.setZoom(15);
map.setCenter(citMarker.getPosition());
infowindow.open(map,citMarker);
});

google.maps.event.addListener(map, 'rightclick', function(event) {
placeMarker(event.latLng);
});

function placeMarker(location) {
var marker = new google.maps.Marker({
position: location,
map: map,
});

var lat = location.lat();
var long = location.lng();
window.location.href = "testmap.php?dlat=" + lat + "&dlong=" + long;

var infowindow = new google.maps.InfoWindow({
content: 'Latitude: ' + location.lat() +
'<br>Longitude: ' + location.lng()
});
infowindow.open(map,marker);

google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map,marker);
});
}
}

google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>

<body>
<div id="googleMap" style="width:500px;height:380px;"></div>
<?php

function getLatLong() {
// Check if we have parameters dlat and dlong being passed to the script through the URL
if (isset($_GET["dlat"]) && isset($_GET["dlong"])) {

  // Put the two words together with a space in the middle to form "hello world"
    $lat = $_GET["dlat"];
    $long = $_GET["dlong"];
  // Print out some JavaScript with $hello stuck in there which will put "hello world"    into the javascript.
  echo $hello;
  echo $lat;
  echo $long;
}
}
?>
getLatLong();
Lat 1:<input type="text" size="20" maxlength="50" name="displayLat" value="<?php echo $lat; ?>"><br />
Long 1:<input type="text" size="20" maxlength="50" name="displayLong"><br />
</body>
</html>

So far it is displaying a google map, when you right click on the map an infowindow pops up giving the coordinates for the space, and 2 textfields are displayed below the map, what I am looking to do is to display the latitude and longitude in the form when you right click the map.

1 Answers1

3

You can put the latitude and longitude of the right click event in the HTML form like this:

google.maps.event.addListener(map, 'rightclick', function(event) {
  document.getElementById('displayLat').value = event.latLng.lat();
  document.getElementById('displayLong').value = event.latLng.lng();
  placeMarker(event.latLng);
});

If you give your <input> tags ids:

Lat 1:<input type="text" size="20" maxlength="50" name="displayLat" id="displayLat" value=""><br />
Long 1:<input type="text" size="20" maxlength="50" name="displayLong" id="displayLong"><br />
geocodezip
  • 158,664
  • 13
  • 220
  • 245