【问题标题】:Coordinates Variables from Google Maps API to a php form [duplicate]将 Google Maps API 中的变量协调到 php 表单 [重复]
【发布时间】:2014-03-15 21:20:43
【问题描述】:

所以我正在寻找一种将纬度和经度从谷歌地图 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>

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

【问题讨论】:

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


    【解决方案1】:

    你可以把右键事件的经纬度放到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);
    });
    

    如果你给你的&lt;input&gt;标签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 />
    

    【讨论】:

    • 太棒了,成功了,谢谢 =)
    猜你喜欢
    • 2016-01-26
    • 2011-06-01
    • 2018-03-01
    • 1970-01-01
    • 2012-05-24
    • 2016-01-13
    • 2018-02-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多