Javascript 如何将映射信息窗口值传递到HTML输入表单
好的,我快到了。下面的脚本获取PHP SQL查询的输出,并在结果中的每个点上创建多个标记 输出字符串liste_des_points如下所示:Javascript 如何将映射信息窗口值传递到HTML输入表单,javascript,google-maps-api-3,infowindow,Javascript,Google Maps Api 3,Infowindow,好的,我快到了。下面的脚本获取PHP SQL查询的输出,并在结果中的每个点上创建多个标记 输出字符串liste_des_points如下所示: [43.818298,-69.809998, "Phippsburg, Kennebec River, Maine"],[43.755001,-69.785004, "Fort Popham, Hunniwell Point, Kennebec River, Maine"] 最后一步,我无法做到,当用户点击一个标记时,它将通过在底部的三个HTML输入字
[43.818298,-69.809998, "Phippsburg, Kennebec River, Maine"],[43.755001,-69.785004, "Fort Popham, Hunniwell Point, Kennebec River, Maine"]
最后一步,我无法做到,当用户点击一个标记时,它将通过在底部的三个HTML输入字段中显示信息窗口内容来确认选择;单击其他标记,输入字段的内容将发生更改
我发现我需要使用类似于:
document.getElementById('txtLat').value = liste_des_points[i][0];
document.getElementById('txtLng').value = liste_des_points[i][1];
document.getElementById('tideStation').value = (liste_des_points[i][2]);
但无法确定在何处将其集成到现有脚本中
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html {
height: 100%
}
body {
height: 100%;
margin: 0;
padding: 0
}
#map_canvas {
height: 100%
}
</style>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=MY_API_KEY">
</script>
<script type="text/javascript">
var infos = [];
function initMap() {
var optionsCarte = {
center: new google.maps.LatLng(<?php echo $lat; ?>, <?php echo $lon; ?>),
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), optionsCarte);
var myicon = "images/tidesicons-line-32.ico";
var liste_des_points = [<?php echo $listeDesPoints;?>];
var liste_des_points_Station = [<?php echo $listeDesPointsStation;?>];
var i = 0,
li = liste_des_points.length;
var infowindow = new google.maps.InfoWindow();
currentPopup = infowindow;
/* set the markers */
while (i < li) {
new google.maps.LatLng(liste_des_points[i][0]);
new google.maps.LatLng(liste_des_points[i][1]);
new google.maps.LatLng(liste_des_points[i][2]);
marker = new google.maps.Marker({
position: new google.maps.LatLng(liste_des_points[i][0], liste_des_points[i][1]),
map: map,
icon: myicon,
});
var content = liste_des_points[i][0] + " ; " + liste_des_points[i][1] + " ; " + liste_des_points[i][2];
i++;
google.maps.event.addListener(marker, 'click', (function(marker, content, infowindow) {
return function() {
/* close the previous info-window */
closeInfos();
infowindow.setContent(content);
infowindow.open(map, marker);
/* keep the handle, in order to close it on next click event */
infos[0] = infowindow;
};
})(marker, content, infowindow));
}
}
function closeInfos() {
if (infos.length > 0) {
/* detach the info-window from the marker ... undocumented in the API docs */
infos[0].set("marker", null);
/* and close it */
infos[0].close();
/* blank the array */
infos.length = 0;
}
}
</script>
<title>Search NOAA Stations by ZIPcode</title>
</head>
<div align="center">
<body onLoad="initMap()">
<div id="map_canvas" style="width:50%; height:50%"> </div>
</div>
<hr size=".5" width="75%" />
<div align="center">
<div>
<form action="output.php" method="get" name="locationinfo">
<table cellpadding="3" cellspacing="3" align="center" width="40%">
<tr>
<th colspan="2" align="center">You selected the tide station shown below.<br />If this not correct, click on another one in the map to change the selection</th>
</tr>
<tr>
<th colspan="2" align="center">When you click on the "Create Calendar Files..." link you will generate the files for the tide station you selected which will be used to create your calendar.</th>
</tr>
<tr>
<td colspan=3>
<div id="map_canvas" style="background-color: #ffffff"></div>
</td>
</tr>
<tr>
<td>Latitude:</td>
<td><input type="text" id="txtLat" name="txtLat" width="70" size="40"></td>
<tr>
<td>Longitude:</td>
<td><input type="text" id="txtLng" name="txtLng" width="70" size="40"></td>
</tr>
<tr>
<td>Location:</td>
<td><input type="text" id="tideStation" name="txtDesc" width="100" size="40"></td>
</tr>
<tr>
<td align="center" colspan="2"><input type="submit" value="Create Calendar Files..."> <INPUT type="reset" value="Clear Entry"></td>
</tr>
</table>
</form>
</div>
</body>
</html>
修复现有代码的最简单方法是在marker click listener函数中的i上添加函数闭包,并将设置表单中值的代码放入其中:
google.maps.event.addListener(marker, 'click', (function(marker, content, infowindow, i) {
return function() {
/* close the previous info-window */
closeInfos();
infowindow.setContent(content);
infowindow.open(map, marker);
document.getElementById('txtLat').value = liste_des_points[i][0];
document.getElementById('txtLng').value = liste_des_points[i][1];
document.getElementById('tideStation').value = (liste_des_points[i][2]);
/* keep the handle, in order to close it on next click event */
infos[0] = infowindow;
};
})(marker, content, infowindow, i));
i++;
代码段:
html{
身高:100%
}
身体{
身高:100%;
保证金:0;
填充:0
}
地图画布{
身高:50%
}
var-infos=[];
函数初始化映射{
var期权单={
中心:新google.maps.LatLng43.818298,-69.809998,
缩放:12,
mapTypeId:google.maps.mapTypeId.ROADMAP
};
var map=new google.maps.Mapdocument.getElementByIdmap_画布,选项点;
var liste_des_points=[
[43.818298,-69.809998,缅因州肯纳贝克河菲普斯堡],
[43.755001,-69.785004,缅因州肯尼贝克河亨尼维尔角波姆堡]
];
//var liste_des_points_Station=[];
var i=0,
li=列出点的长度;
var infowindow=new google.maps.infowindow;
currentPopup=信息窗口;
/*设置标记*/
而我{
marker=新的google.maps.marker{
职位:新google.maps.LatLngliste des_points[i][0],liste des_points[i][1],
地图:地图,
};
var content=liste_des_points[i][0]+;+liste_des_points[i][1]+;+liste_des_points[i][2];
google.maps.event.addListenermarker,“单击”,functionmarker,内容,信息窗口,i{
返回函数{
/*关闭“上一个信息”窗口*/
closeInfos;
infowindow.setContentcontent;
infowindow.openmap,标记;
document.getElementById'txtLat'.value=liste_des_points[i][0];
document.getElementById'txtLng'.value=liste_des_points[i][1];
document.getElementById'tideStation'。value=liste_des_points[i][2];
/*保留该句柄,以便在下次单击事件时关闭它*/
infos[0]=infowindow;
};
}标记,内容,信息窗口,i;
i++;
}
}
函数closeInfos{
如果infos.length>0{
/*将信息窗口与API文档中未记录的标记分离*/
infos[0]。setmarker,空;
/*然后关上它*/
infos[0]。关闭;
/*清空数组*/
infos.length=0;
}
}
您选择了如下所示的潮汐站。如果不正确,请单击地图中的另一个潮汐站以更改选择
单击“创建日历文件”按钮时。。。链接您将为您选择的潮汐站生成用于创建日历的文件。
纬度:
经度:
地点: