javascript - 如何在不重新加载页面的情况下使用经度和纬度坐标动态更改谷歌地图上的位置点
问题描述
看一看。
尝试/实现方式:
左侧:当我在(右侧)选择表格行时,应该显示 LON、LAT 底部的位置的谷歌地图。但无需重新加载页面。
右手边:有一个表,其中包含来自 MySQL DB 的 LATITUDE 和 LONGITUDE 值。
我的方法:
<div class="row">
<div class="col-md-6">
<div class="mapouter">
<div class="gmap_canvas">
<iframe width="600" height="500" id="gmap_canvas" src="https://maps.google.com/maps?q=32.7333,74.8667&hl=es;z=14&output=embed" frameborder="0" scrolling="no" marginheight="0" marginwidth="0"></iframe>
</div>
</div>
</div>
<div class="col-md-6" >
<table class="table " style="margin-left: 30px;">
<tbody>
<?php
while($row=mysqli_fetch_array($res))
{
$lat =$row['lat'];
$lon =$row['lon'];
<tr class="bg-success">
<td><?php echo $lat;?></td>
<td><?php echo $lon;?></td>
</tr >
<?php
}
?>
</tbody>
</table>
</div>
我不确定如何在 jquery 中实现这一点,很抱歉,但我是 jquery 的初学者。
问题:
- 我完全不确定这种方法是否是最佳实践。
- 有可能吗,如果可以,怎么做?
- 任何人都可以帮助我阐明这一点吗?
解决方案
您需要为所有“tr”标签创建一个点击事件,从“td”获取 lon 和 lat 的值,替换非数字文本并更改 .gmap_canvas div 中的 html,它包装了你的 iframe。
这里你的代码https://jsfiddle.net/mshz0gvu/
<div class="row">
<div class="col-md-6">
<div class="mapouter">
<div class="gmap_canvas">
<iframe width="100%" height="500" id="gmap_canvas" src="https://maps.google.com/maps?q=32.7333,74.8667&hl=es;z=14&output=embed" frameborder="0" scrolling="no" marginheight="0" marginwidth="0"></iframe>
</div>
</div>
</div>
<div class="col-md-6" >
<table class="table " style="margin-left: 30px;">
<tbody>
<tr>
<td>LAT: 42.3145186</td>
<td>LON: -71.110367</td>
</tr >
</tbody>
</table>
</div>
</div>
<script>
$('tr').click(function(){
let lat = $(this).find('td:first-child').text().replace('LAT: ', '');
let lon = $(this).find('td:last-child').text().replace('LON: ', '');
let new_map = "<iframe width=\"100%\" height=\"500\" id=\"gmap_canvas\" src=\"https://maps.google.com/maps?q="+lat+","+lon+"&hl=es;z=14&output=embed\" frameborder=\"0\" scrolling=\"no\" marginheight=\"0\" marginwidth=\"0\"></iframe>";
$('div.gmap_canvas').html(new_map);
});
</script>
推荐阅读
- django - 将 paramquery 包含到 django 应用程序中
- c - 从 ILE C (AS/400 C) 中的其他 c 程序调用 c 程序
- ruby - 冰糕泛型问题 - 在参数位置具有类型变量的方法
- typescript - 如何使用 for 循环将具有值的元素添加到对象数组中
- java - 请求正文为空的 PATCH API - AssertionFailedError:预期为 204 但为 415
- regex - Ruby 正则表达式锚与字符类 && 相结合
- javascript - Webpack 5 如何禁用压缩并只保留缩小
- java - Iterator.iter() 返回相同的值
- json - 使用 Unix 从输入文件中提取 JSON 数据集中的匹配字符串 id
- reactjs - Redux - InitialState - 数组与对象问题