我有以下代码,它有两个按钮,一个用于显示地图,另一个用于隐藏它并显示一个段落。问题是,我第一次打开页面时会显示地图,但是一旦我尝试移动它以找到所选位置,它就会停止显示地图。
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css">
<link rel="stylesheet"
href="http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-social/4.2.1/bootstrap-social.css">
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script
src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true&libraries=places"></script>
</head>
<body>
<div id="myDiv" class="col-md-4 hidden-xs"
style="padding-left: 7px; background-color: white;">
<div class="row"
style="margin-left: 30px; margin-top: 36px; background-color: white;">
<div id="hideMap" class="col-md-5" onClick="hideMap()">Hide</div>
<div id="showMap" class="col-md-5" onClick="populateMap()">Show</div>
</div>
<div id="myMap" style="width: 380px; height: 400px"></div>
<div id="para"><p>This is it</p></div>
</div>
<script>
function hideMap() {
$("#myMap").hide();
$("#para").show();
}
function populateMap() {
$("#para").hide();
$("#myMap").show();
}
var map;
var markers = [];
var pinColor = "FE7569";
function pinImage(imagenum) {
return image = new google.maps.MarkerImage(
"http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld="
+ imagenum + "|" + pinColor);
}
function initialize() {
var centreLoc = new google.maps.LatLng(37.9908372,23.7383394);
var mapOptions = {
zoom : 13,
center : centreLoc,
disableDefaultUI : true,
mapTypeControlOptions : google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('myMap'),
mapOptions);
hideMap();
var results = [ [ 'Pos1', 37.9908372,23.7383394 ],
[ 'Pos2', 37.89,23.7383394 ] ];
processResults(results);
}
function processResults(results) {
for (var i = 0, marker; marker = markers[i]; i++) {
marker.setMap(null);
}
markers = [];
$("#marker-container").empty();
for (i = 0; i < results.length; i++) {
var place = results[i][0];
var placeLoc = new google.maps.LatLng(results[i][2], results[i][3]);
// Create a marker for each place.
var marker = new google.maps.Marker({
map : map,
title : place,
position : placeLoc,
icon : pinImage(i + 1)
});
markers.push(marker);
var description = $("<div class='marker-description'><image class='marker' src='" + marker.icon.url + "'></image><span class='place'>"
+ place + "</span>");
$("#marker-container").append(description);
}
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</body>
</html>
之前(加载演示之后)
之后(尝试在地图上找到地址)
答案 0 :(得分:1)
您需要触发&#39;调整大小&#39;显示后在地图上显示事件,然后设置中心的大小:
function populateMap() {
$("#para").hide();
$("#myMap").show();
google.maps.event.trigger(map,'resize');
map.setCenter(centreLoc);
}
代码段(已删除signed_in=true
,在SO代码段中播放效果不佳):
function hideMap() {
$("#myMap").hide();
$("#para").show();
}
function populateMap() {
$("#para").hide();
$("#myMap").show();
google.maps.event.trigger(map,'resize');
map.setCenter(centreLoc);
}
var map;
var centreLoc;
var markers = [];
var pinColor = "FE7569";
function pinImage(imagenum) {
return image = new google.maps.MarkerImage(
"http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld="
+ imagenum + "|" + pinColor);
}
function initialize() {
centreLoc = new google.maps.LatLng(37.9908372,23.7383394);
var mapOptions = {
zoom : 13,
center : centreLoc,
disableDefaultUI : true,
mapTypeControlOptions : google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('myMap'),
mapOptions);
hideMap();
var results = [ [ 'Pos1', 37.9908372,23.7383394 ],
[ 'Pos2', 37.89,23.7383394 ] ];
processResults(results);
}
function processResults(results) {
for (var i = 0, marker; marker = markers[i]; i++) {
marker.setMap(null);
}
markers = [];
$("#marker-container").empty();
for (i = 0; i < results.length; i++) {
var place = results[i][0];
var placeLoc = new google.maps.LatLng(results[i][1], results[i][2]);
// Create a marker for each place.
var marker = new google.maps.Marker({
map : map,
title : place,
position : placeLoc,
icon : pinImage(i + 1)
});
markers.push(marker);
var description = $("<div class='marker-description'><image class='marker' src='" + marker.icon.url + "'></image><span class='place'>"
+ place + "</span>");
$("#marker-container").append(description);
}
}
google.maps.event.addDomListener(window, 'load', initialize);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places.js"></script>
<div id="myDiv" class="col-md-4 hidden-xs"
style="padding-left: 7px; background-color: white;">
<div class="row"
style="margin-left: 30px; margin-top: 36px; background-color: white;">
<div id="hideMap" class="col-md-5" onClick="hideMap()">Hide</div>
<div id="showMap" class="col-md-5" onClick="populateMap()">Show</div>
</div>
<div id="myMap" style="width: 380px; height: 400px"></div>
<div id="para"><p>This is it</p></div>
</div>
&#13;
答案 1 :(得分:1)
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-social/4.2.1/bootstrap-social.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true&libraries=places"></script>
</head>
<body>
<div id="myDiv" class="col-md-4 hidden-xs" style="padding-left: 7px; background-color: white;">
<div class="row" style="margin-left: 30px; margin-top: 36px; background-color: white;">
<div id="hideMap" class="col-md-5" onClick="hideMap()">Hide</div>
<div id="showMap" class="col-md-5" onClick="populateMap()">Show</div>
</div>
<div id="myMap" style="width: 380px; height: 400px;"></div>
<div id="para"><p>This is it</p></div>
</div>
<script>
function hideMap() {
$("#myMap").css('display', 'none');
$("#para").css('display', 'block');
}
function populateMap() {
$("#para").hide();
$("#myMap").show();
}
var map;
var markers = [];
var pinColor = "FE7569";
function pinImage(imagenum) {
return image = new google.maps.MarkerImage("http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=" + imagenum + "|" + pinColor);
}
function initialize() {
var centreLoc = new google.maps.LatLng(37.9908372,23.7383394);
var mapOptions = {
zoom : 10,
center : centreLoc,
disableDefaultUI : true,
mapTypeControlOptions : google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('myMap'), mapOptions);
var results = [ [ 'Pos1', 37.9908372,23.7383394 ], [ 'Pos2', 37.89,23.7383394 ] ];
processResults(results);
google.maps.event.addListenerOnce(map, 'idle', function(){
hideMap();
});
}
function processResults(results) {
for (var i = 0, marker; marker = markers[i]; i++) {
marker.setMap(null);
}
markers = [];
$("#marker-container").empty();
for (i = 0; i < results.length; i++) {
var place = results[i][0];
var placeLoc = new google.maps.LatLng(results[i][1], results[i][2]);
// Create a marker for each place.
var marker = new google.maps.Marker({
map : map,
title : place,
position : placeLoc,
icon : pinImage(i + 1)
});
markers.push(marker);
var description = $("<div class='marker-description'><image class='marker' src='" + marker.icon.url + "'></image><span class='place'>" + place + "</span>");
$("#marker-container").append(description);
}
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</body>
</html>
我提出了错误的风格
<div id="myMap"></div>
<div id="para" style="width: 380px; height: 400px;"><p>This is it</p></div>
到这一个
<div id="myMap" style="width: 380px; height: 400px;"></div>
<div id="para"><p>This is it</p></div>