javascript - 在谷歌地图信息窗口中添加输入和提交字段
问题描述
我的谷歌地图上有多个标记,用户在单击并打开标记信息窗口时必须输入一些注释。会有一个文本框和提交按钮,我想从输入框中获取值。
// This example displays a marker at the center of Australia.
// When the user clicks the marker, an info window opens.
function initMap() {
var uluru = {lat: -25.363, lng: 131.044};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: uluru
});
var contentString = '<div id="content">'+
'<div id="siteNotice">'+
'</div>'+
'<h1 id="firstHeading" class="firstHeading">Uluru</h1>'+
'<div id="bodyContent">'+
'<p><b>Uluru</b>, also referred to as <b>Ayers Rock</b>, is a large ' +
'sandstone rock formation in the southern part of the '+
'Northern Territory, central Australia. It lies 335 km (208 mi) '+
'south west of the nearest large town, Alice Springs; 450 km '+
'(280 mi) by road. Kata Tjuta and Uluru are the two major '+
'features of the Uluru - Kata Tjuta National Park.'+
'</div><input type="text"><button>submit</button>'+
'</div>';
var infowindow = new google.maps.InfoWindow({
content: contentString
});
var marker = new google.maps.Marker({
position: uluru,
map: map,
title: 'Uluru (Ayers Rock)'
});
marker.addListener('click', function() {
infowindow.open(map, marker);
});
}
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
height: 100%;
margin: 0;
padding: 0;
}
<div id="map"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCGzw8fgSuNnFhem_sQ6OmnaqvcczlMbtk&callback=initMap">
</script>
请检查 jsfiddle 链接https://jsfiddle.net/sankarmahadevan/vjce1w63/6/
解决方案
我做了以下更新,我添加了 google map domready 的侦听器并添加了按钮单击功能。
在这里更新了小提琴链接
marker.addListener('click', function() {
infowindow.open(map, marker);
isGoogleDomReady(infowindow);
});
function isGoogleDomReady(infowindow){
google.maps.event.addListener(infowindow, 'domready', function () {
$('#SaveBtn').on('click', function() {
alert($('#mapNotes').val());
});
});
}
推荐阅读
- javascript - 重用内置数组方法 - JS - 困惑
- python - 无法使用 OpenCV 使用 cv2.CAP_FFMPEG 在 GPU 上解码视频
- c - 尽管没有显示输出,但编码正在运行。也许这将是scanf问题?我不确定
- javascript - RxJs - 不能从合并的 Observable 中创建两个新的 Observable
- node.js - 使用 NPM 构建的 Gradle Zip 任务
- c - 我在崇高文本中遇到了一些问题
- scala - 使用 group by 中的标志选择一行
- python - 我的滚动条不适用于鼠标的滚动条
- c# - VSTO 办公室插件中的 Tesseract
- python - 将非关键字参数限制为 discord.py 中的列表