gpt4 book ai didi

javascript - Google map API 调整大小

转载 作者:行者123 更新时间:2023-11-29 12:10:56 24 4
gpt4 key购买 nike

Google map API 全新。目前,我让它从 mySQL 数据库中提取数据并添加标记。

但是我想更改 map 的大小以适合我的网站布局。这是我目前的代码。

<!DOCTYPE html >
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>PHP/MySQL & Google Maps Example</title>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js"></script>
<script type="text/javascript">
//<![CDATA[

var customIcons = {
restaurant: {
icon: 'http://labs.google.com/ridefinder/images/mm_20_blue.png'
},
bar: {
icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png'
}
};

function load() {
var map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(0, 0),
zoom: 1,
mapTypeId: 'roadmap'
});
var infoWindow = new google.maps.InfoWindow;

// Change this depending on the name of your PHP file
downloadUrl("phpsqlajax_genxml.php", function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var name = markers[i].getAttribute("name");
var address = markers[i].getAttribute("address");
var type = markers[i].getAttribute("type");
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
var html = "<b>" + name + "</b> <br/>" + address;
var icon = customIcons[type] || {};
var marker = new google.maps.Marker({
map: map,
position: point,
icon: icon.icon
});
bindInfoWindow(marker, map, infoWindow, html);
}
});
}

function bindInfoWindow(marker, map, infoWindow, html) {
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(html);
infoWindow.open(map, marker);
});
}

function downloadUrl(url, callback) {
var request = window.ActiveXObject ?
new ActiveXObject('Microsoft.XMLHTTP') :
new XMLHttpRequest;

request.onreadystatechange = function() {
if (request.readyState == 4) {
request.onreadystatechange = doNothing;
callback(request, request.status);
}
};

request.open('GET', url, true);
request.send(null);
}

function doNothing() {}

//]]>

</script>

</head>

<body onload="load()">
<div id="map" style="width: 600px; height: 450px"></div>
</body>
<script>
google.maps.event.trigger(map, 'resize');
</script>

</html>

我已经阅读了一些内容并浏览了开发文档,发现我应该使用

google.maps.event.trigger(map, 'resize');

但是我无法弄清楚这需要去哪里,因为它要么破坏 map ,要么没有将 map 调整到所需的大小。

有什么想法吗?

最佳答案

您遇到的问题是您无法访问(范围)“map”变量,因为您已在函数 load() 中声明了它。

重构你的代码!有多种方法可以做到这一点,一种简单的方法是将其声明为全局变量。

var map = blah blah...

function load() {
//...

JS:

window.onresize = function() {
var currCenter = map.getCenter();
google.maps.event.trigger(map, 'resize');
map.setCenter(currCenter);
};

示例: http://codepen.io/alexgill/pen/NqjMma

关于javascript - Google map API 调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30713060/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com