javascript - 从 Google Maps API 到 php 表单的坐标变量

标签 javascript php google-maps google-maps-api-3

<分区>

所以我正在寻找一种方法来将谷歌地图 API 中的纬度和经度传递到 PHP 文本字段。 到目前为止,我有以下代码,但我被卡住了,非常感谢任何帮助。

<!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>

到目前为止,它显示的是谷歌地图,当您右键单击 map 时,会弹出一个信息窗口,提供空间坐标, map 下方显示 2 个文本字段,我想做的是显示纬度当您右键单击 map 时,表格中会显示经度和经度。

最佳答案

你可以这样把右键事件的经纬度放在HTML表单中:

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

如果您提供 <input>标签 ID:

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

关于javascript - 从 Google Maps API 到 php 表单的坐标变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22429892/

相关文章:

javascript - 如果输入为空则禁用按钮

javascript - 如何清除客户端中服务器端数据表的行

php - 为什么 PHP 会干扰我的 CSS?

php - 动态设置导航的事件状态

android - Flutter iOS - 在加载谷歌地图小部件之前无法获取 initialCameraPosition 的当前位置

javascript - 无法在谷歌地图中保存多边形

javascript - React-Native:for 循环中的 this.something.map 可以访问 .json 文件中数据数组中的数据..有人可以建议一个示例代码吗?

php - SQLSTATE[HY000] : General error: 2053 error occurs at Laravel

javascript - 谷歌地图自定义覆盖点击事件

javascript - 我在打开页面时收到错误 "operation aborted, Internet explorer can'”