我正在尝试使用this sample code提供的数据替换我的数据,但我的代码不起作用。我刚刚添加了一个名为results的数组,并尝试将其lat和lan值传递给地图,但由于某些原因它无效。
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>MarkerClusterer v3 Simple Example</title>
<style >
body {
margin: 0;
padding: 10px 20px 20px;
font-family: Arial;
font-size: 16px;
}
#map-container {
padding: 6px;
border-width: 1px;
border-style: solid;
border-color: #ccc #ccc #999 #ccc;
-webkit-box-shadow: rgba(64, 64, 64, 0.5) 0 2px 5px;
-moz-box-shadow: rgba(64, 64, 64, 0.5) 0 2px 5px;
box-shadow: rgba(64, 64, 64, 0.1) 0 2px 5px;
width: 600px;
}
#map {
width: 600px;
height: 400px;
}
</style>
<script src="https://maps.googleapis.com/maps/api/js"></script>
<script>
var script = '<script type="text/javascript" src="https://googlemaps.github.io/js-marker-clusterer/src/markerclusterer';
if (document.location.search.indexOf('compiled') !== -1) {
script += '_compiled';
}
script += '.js"><' + '/script>';
document.write(script);
</script>
<script>
function initialize() {
var results = [
[
'Alexs loc', '37.9908372',
'23.7383394', '0',
'id-90'], [
'John Jamess loc', '37.9908372',
'23.7383394', '1',
'id-89'], [
'Normans loc', '38.075352',
'23.807885', '3',
'id-12'], [
'Jack Moores loc', '37.9908372',
'23.7383394', '2',
'id-67'], ];
var center = new google.maps.LatLng(37.9908372, 23.7383394);
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 3,
center: center,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var markers = [];
for (var i = 0; i < results.length; i++) {
var placeLoc = new google.maps.LatLng(results[i][1], results[i][2]);
var marker = new google.maps.Marker({
position: latLng
});
markers.push(marker);
}
var markerCluster = new MarkerClusterer(map, markers);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<p>
<a href="?compiled">Compiled</a> |
<a href="?">Standard</a> version of the script.
</p>
<div id="map-container"><div id="map"></div></div>
</body>
</html>
答案 0 :(得分:2)
未选中但我看到您在循环中创建标记的代码。这里的位置应该引用placeLoc而不是latLng
var marker = new google.maps.Marker({
position: placeLoc
});