如何通过单击按钮更改 Google 地图中心
How to change Google Map Center by clicking a button
我正在使用 Google Map API v3 和 jQuery 1.11.0.
我有一张Google地图在下面div:
<div id="googleMap" class="map_div"></div>
map.js 在 html 文件之外,已链接。
现在我在 html 的另一部分(地图外)有一个按钮,如下所示:
<button id="3">Change center</button>
现在我想添加一个点击事件,它将地图的中心更改为新的纬度和经度。
所以,我在 HTML 中有 JavaScript 是这样的:
<script>
$(document).ready(function()
{
$("#3").click(function(){
var center = new google.maps.LatLng(10.23,123.45);
map.panTo(center);
});
});
</script>
有人可以帮我吗?在此先感谢您的帮助。
这是您的问题的代码:
var map;
function initialize()
{
map = new google.maps.Map(document.getElementById('map-canvas'), {
center: new google.maps.LatLng(48.1293954,12.556663),//Setting Initial Position
zoom: 10
});
}
function newLocation(newLat,newLng)
{
map.setCenter({
lat : newLat,
lng : newLng
});
}
google.maps.event.addDomListener(window, 'load', initialize);
//Setting Location with jQuery
$(document).ready(function ()
{
$("#1").on('click', function ()
{
newLocation(48.1293954,11.556663);
});
$("#2").on('click', function ()
{
newLocation(40.7033127,-73.979681);
});
$("#3").on('click', function ()
{
newLocation(55.749792,37.632495);
});
});
html, body, #map-canvas {
height: 100%;
margin: 0px;
padding: 0px
}
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script>
<button id="1" style="padding:10px; cursor:pointer;">Munich</button>
<button id="2" style="padding:10px;cursor:pointer;">New York</button>
<button id="3" style="padding:10px;cursor:pointer;">Moscow</button>
<br>
<br>
<div style="height:100%;" id="map-canvas"></div>
Live demo is here 如果你需要更多:).
在您的 initialize
函数(或您创建地图的位置)中,在创建地图对象后添加以下代码:
google.maps.event.addDomListener(document.getElementById('3'), 'click', function () {
map.setCenter(new google.maps.LatLng(10.23,123.45));
});
我正在使用 Google Map API v3 和 jQuery 1.11.0.
我有一张Google地图在下面div:
<div id="googleMap" class="map_div"></div>
map.js 在 html 文件之外,已链接。
现在我在 html 的另一部分(地图外)有一个按钮,如下所示:
<button id="3">Change center</button>
现在我想添加一个点击事件,它将地图的中心更改为新的纬度和经度。
所以,我在 HTML 中有 JavaScript 是这样的:
<script>
$(document).ready(function()
{
$("#3").click(function(){
var center = new google.maps.LatLng(10.23,123.45);
map.panTo(center);
});
});
</script>
有人可以帮我吗?在此先感谢您的帮助。
这是您的问题的代码:
var map;
function initialize()
{
map = new google.maps.Map(document.getElementById('map-canvas'), {
center: new google.maps.LatLng(48.1293954,12.556663),//Setting Initial Position
zoom: 10
});
}
function newLocation(newLat,newLng)
{
map.setCenter({
lat : newLat,
lng : newLng
});
}
google.maps.event.addDomListener(window, 'load', initialize);
//Setting Location with jQuery
$(document).ready(function ()
{
$("#1").on('click', function ()
{
newLocation(48.1293954,11.556663);
});
$("#2").on('click', function ()
{
newLocation(40.7033127,-73.979681);
});
$("#3").on('click', function ()
{
newLocation(55.749792,37.632495);
});
});
html, body, #map-canvas {
height: 100%;
margin: 0px;
padding: 0px
}
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script>
<button id="1" style="padding:10px; cursor:pointer;">Munich</button>
<button id="2" style="padding:10px;cursor:pointer;">New York</button>
<button id="3" style="padding:10px;cursor:pointer;">Moscow</button>
<br>
<br>
<div style="height:100%;" id="map-canvas"></div>
Live demo is here 如果你需要更多:).
在您的 initialize
函数(或您创建地图的位置)中,在创建地图对象后添加以下代码:
google.maps.event.addDomListener(document.getElementById('3'), 'click', function () {
map.setCenter(new google.maps.LatLng(10.23,123.45));
});