我是Google Maps API的新手。我写了一个程序,它会根据纬度和经度在所有点上放置标记.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']];
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);
}
更正此行: 在 for loop:
markers.setMap(map( 中添加此行; markers.push(marker(;
并检查此链接:它无法打开 http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/src/markerclusterer_compiled.js
检查控制台.log是否有任何错误:很乐意为您提供帮助