ArnabC
ArnabC

Reputation: 181

Markers not working in Google Maps API

I'm new to Google Maps API. I wrote a program that will put markers on all the points according to their latitude and longitude given in where.js . It loads the map but no markers are being placed. I don't understand what I am doing wrong.

The HTML code is as follows-

<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>

Here is the Javascript file for 'where.js'-

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']];

Upvotes: 0

Views: 1466

Answers (2)

duncan
duncan

Reputation: 31930

Javascript arrays are zero-indexed. Your code is referring to them as if they were one-indexed.

i.e. your code:

i = 0;
var markers = [];
for ( pos in myData ) {
    i = i + 1;
    var row = myData[pos];
    ...
}

You're referring to myData[1] (the 2nd item), myData[2] (the 3rd item), etc, up to and including myData[6]. Which would be the 7th item in your array of markers, except you've only got 6 items. So you're probably getting a JS error (check your console), preventing the markers from loading at all.

Just put the i = i + 1; line at the end of your loop. Or change the loop style, e.g.

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);
}

Upvotes: 1

Raj Chavan
Raj Chavan

Reputation: 41

Correct this line : // add this line in for loop:
markers.setMap(map); markers.push(marker);

And also check this Link : It doesn't open http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/src/markerclusterer_compiled.js

check console.log if any error : happy to help you

Upvotes: 0

Related Questions