Javascript 如何更改为HTML按钮上标记的中心单击
我正在使用GoogleMapsAPI,希望在单击(html)按钮时将地图中心设置为特定位置 我尝试添加一个名为newLocation的函数,该函数使用两个参数表示经度和纬度。然后,一个不同的功能连接到ID为“TestButton”的按钮 HTML按钮Javascript 如何更改为HTML按钮上标记的中心单击,javascript,c#,html,google-maps-api-3,Javascript,C#,Html,Google Maps Api 3,我正在使用GoogleMapsAPI,希望在单击(html)按钮时将地图中心设置为特定位置 我尝试添加一个名为newLocation的函数,该函数使用两个参数表示经度和纬度。然后,一个不同的功能连接到ID为“TestButton”的按钮 HTML按钮 <button class="item-button"><span class="glyphicon glyphicon-wrench" aria-hidden="true"></span></button
<button class="item-button"><span class="glyphicon glyphicon-wrench" aria-hidden="true"></span></button>
<button class="item-button" id="TestButton"><span class="glyphicon glyphicon-map-marker" aria-hidden="true"></span></button>
</div>
创建地图和设置
var map;
function initMap() {
//Set center map
var CenterLoc = { lat: 51.34, lng: 5.53 };
//Set map settings
map = new google.maps.Map(document.getElementById('map'),
{
center: CenterLoc,
disableDefaultUI: true,
zoom: 3,
});
创建功能和设置中心
//Center function
function newLocation(newLat, newLng) {
map.setCenter({
lat: newLat,
lng: newLng
});
}
google.maps.event.addDomListener(window, 'load', initMap);
//Setting location center
$(document).ready(function () {
$("TestButton").on('click', function () {
newLocation(48.1293954, 11.556663);
});
});
<script src="http://code.jquery.com/jquery.min.js"></script>
//中心函数
功能新建位置(新建LAT、新建LNG){
地图设置中心({
拉特:纽拉特,
液化天然气:新液化天然气
});
}
google.maps.event.addDomListener(窗口'load',initMap);
//设置定位中心
$(文档).ready(函数(){
$(“测试按钮”)。在('click',函数(){
新址(48.1293954,11.556663);
});
});
这些都是我认为与这个问题相关的代码部分。但是,整个代码如下所示:
我在浏览器控制台中没有收到任何错误。但是当我按下按钮时,什么也没有发生。在JQuery中,要使用id属性选择或查找唯一的HTML元素,需要使用id选择器 在您的情况下,请使用以下方法:
$(document).ready(function () {
$("#TestButton").on('click', function () {
newLocation(48.1293954, 11.556663);
});
});
这是中的一个工作示例。
有关JQuery#id选择器的详细信息
希望这有帮助 请提供一份说明问题的报告。
$(document).ready(function () {
$("#TestButton").on('click', function () {
newLocation(48.1293954, 11.556663);
});
});