我想在用户点击链接时更改地图的纬度和经度。 我曾尝试在用户点击链接时要求maps.js,但它不起作用
maps.js
function maps(){
lats = 53.430967;
longs = -2.960835;
var mapProp = {
center:new google.maps.LatLng(lats, longs),
zoom: 17,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("googleMap"), mapProp);
}
module.exports = maps;
stadium.js
function stadium(){
$( "a:contains('Stadium of Light')" ).on("click", function(){
lats = 54.914740;
longs = -1.388371;
google.maps.event.addDomListener(window, 'load', maps);
window.location.href='maps.html';
});
答案 0 :(得分:3)
您需要设置google.maps.Map对象的center
属性。
function stadium() {
$("a:contains('Stadium of Light')").on("click", function() {
lats = 54.914740;
longs = -1.388371;
map.setCenter(new google.maps.LatLng(lats,longs));
});
}
代码段
var map;
function maps() {
lats = 53.430967;
longs = -2.960835;
var mapProp = {
center: new google.maps.LatLng(lats, longs),
zoom: 17,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("googleMap"), mapProp);
stadium();
}
function stadium() {
$("a:contains('Stadium of Light')").on("click", function() {
lats = 54.914740;
longs = -1.388371;
map.setCenter(new google.maps.LatLng(lats, longs));
});
}
google.maps.event.addDomListener(window, "load", maps);

html,
body,
#googleMap {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}

<script src="https://maps.googleapis.com/maps/api/js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#">Stadium of Light</a>
<div id="googleMap"></div>
&#13;