我是Google Maps API的新手。我写了一个程序,根据where.js中给出的纬度和经度,将标记放在所有点上。 它加载地图但没有放置标记。我不明白我做错了什么。
HTML代码如下 -
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Markets of Kolkata</title>
<link href="http://google-developers.appspot.com/maps/documentation/javascript/examples/default.css" rel="stylesheet">
<script src="http://maps.google.cn/maps/api/js" type="text/javascript"></script> -->
<script src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script src="where.js"></script>
<script>
function initialize() {
//alert("To see the title of a marker, hover over the marker but don't click.");
var myLatlng = new google.maps.LatLng(22.39961,88.101350)
var mapOptions = {
zoom: 3,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
i = 0;
var markers = [];
for ( pos in myData ) {
i = i + 1;
var row = myData[pos];
window.console && console.log(row);
// if ( i < 3 ) { alert(row); }
var newLatlng = new google.maps.LatLng(row[0], row[1]);
var marker = new google.maps.Marker({
position: newLatlng,
map: map,
title: row[3]
});
markers.push(marker);
}
}
</script>
</head>
<body onload="initialize()">
<div id="map_canvas" style="height: 600px"></div>
<p>
<b>Developed By </b>
<a href="http://www.linkedin.com/in/arnab-chakravarty-8a329111b?
trk=nav_responsive_tab_profile"><b>Arnab Chakravarty</b></a>.
</p>
</body>
</html>
这是'where.js'的Javascript文件 -
myData = [
[22.5193768,88.3656851, 'K.M.C. Market, 3rd Floor Market Complex, 212, Rash Behari Avenue Road, Hindustan Park, Gariahat, Kolkata, West Bengal 700019, India','COLLEGE STREET MARKET(SPORTS GOOD)'],
[22.4977887,88.3796042, 'Garfa Main Rd, Kolkata, West Bengal 700075, India','COLLEGE STREET MARKET'],
[22.5048463,88.3882325, 'Purbachal Main Rd, Ramlal Bazar, Haltu, Kolkata, West Bengal 700078, India','COLLEGE STREET MARKET'],
[22.5867867,88.3564619, 'Kolkata, West Bengal 700073, India', 'BAITHAK KHANA PAPER MARKET'],
[22.5867867,88.3564619, 'Kolkata, West Bengal 700073, India', 'SAKUNTALA PARK VEGETABLE MARKET'],
[22.5867867,88.3564619, 'Kolkata, West Bengal 700073, India', 'SANTOSHPUR MUNICIPAL MARKET']];
答案 0 :(得分:1)
Javascript数组是零索引的。您的代码将它们称为一个索引。
即。你的代码:
i = 0;
var markers = [];
for ( pos in myData ) {
i = i + 1;
var row = myData[pos];
...
}
您指的是myData[1]
(第2项),myData[2]
(第3项)等,直至并包括myData[6]
。这将是你的标记数组中的第7项,除了你只有6项。所以你可能会收到一个JS错误(检查你的控制台),防止标记加载。
只需将i = i + 1;
行放在循环的结束处即可。或者改变循环风格,例如
for (var pos = 0; pos < myData.length; pos++) {
var row = myData[pos];
window.console && console.log(row);
var newLatlng = new google.maps.LatLng(row[0], row[1]);
var marker = new google.maps.Marker({
position: newLatlng,
map: map,
title: row[3]
});
markers.push(marker);
}
答案 1 :(得分:0)
更正此行:
//在for循环中添加此行:
markers.setMap(地图);
markers.push(标记);
并检查此链接:它没有打开 http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/src/markerclusterer_compiled.js
检查console.log是否有任何错误:很高兴为您提供帮助