javascript - 从输入字段设置谷歌地图从和到方向
问题描述
我有两个字段 from 和 to,我只是想在提交时在地图上显示 from 和 to。
我无法确定地图的origin
和destination
选项。
HTML:
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>test</title>
<link rel="stylesheet" href="main.css">
</head>
<body>
<div id="map"></div>
<script>
function myMap() {
var mapOptions = {
origin: new google.maps.LatLng(59.426862, 24.743414),
destination: new google.maps.LatLng(59.438337, 24.756494),
zoom: 10,
mapTypeId: google.maps.MapTypeId.HYBRID
}
var map = new google.maps.Map(document.getElementById("map"), mapOptions);
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDFOR-jYbVDU9ariN9UWKhldnV8M_nbrjQ&callback=myMap"></script>
<div id="input-container">
<input type="text" name="from" placeholder="From" class="special">
<input type="text" name="from" placeholder="To">
<input type="submit">
</div>
</body>
</html>
解决方案
// change form code like this
<form>
<div id="input-container">
<input type="text" name="from" placeholder="From" class="special">
<input type="text" name="to" placeholder="To">
<input type="submit" >
</div>
</form>
// using jquery update your map
<script>
$( "form" ).submit(function( event ) {
from = $( "input[name='from']" ).val();
to = $( "input[name='to']" ).val();
var myOptions = {
origin: new google.maps.LatLng(form),
destination: new google.maps.LatLng(to),
zoom: 10,
mapTypeId: google.maps.MapTypeId.HYBRID
};
map.set(myOptions);
event.preventDefault();
});
</script>
推荐阅读
- visual-studio-code - 自定义括号对着色(内置)VSCode
- c# - 使用 Protobuf-Net 序列化未知子类型
- javascript - 从 id 列表中动态加载组件
- r - 如何以一种方式为每个变量勾勒选择具有正态分布的图形
- flutter - 我想验证我的用户名和密码,比较我从 api 请求颤振中得到的信息
- lightgbm - LightGBM 如何评估加权数据集?它是给出加权评估指标还是未加权版本?
- python - 提取特定的 k-mean 图像量化簇
- python - 我的代码正在执行第一部分而不是第二部分。两个部分都可以自己正常工作
- c++ - 使用其基类重新创建对象是否定义良好?
- c# - ActiveX 旧版应用程序 IE11 生命周期结束