1

I have the following function to display two markers on the maps. you can check it online - http://raveen.comlu.com/300.html;

        <script src="http://maps.googleapis.com/maps/api/js">
    </script>

    <script>
        function initialize(){
            var latlng = new google.maps.LatLng(6.242635,80.051840); //latitude and longitude respectively
            //xxxvar image = 'office2.ico';
            //xxxvar image2 = 'children.ico';
            var contentString = '<h1> Volunteer House </h1>'+
                '<p>Many people also refer to this home as <b>VH</b>. This is a must see place in the city.'+ 
                    '<br><br>Direction - Pass the <i>Ambalangoda </i> and the <i>Kanda Road</i>. '+
                    'nd walk just 25m.</p>'+
                    '<br>Address - Ambalangoda, Sri Lanka'+
                    '<br>Website: Volunteer House, <a href="http://raveen.comlu.com" target="_blank">'+
                    'http://raveen.comlu.com</a>';

            var infowindow = new google.maps.InfoWindow({
                content: contentString
            });             

            var mapProp = {
                center:latlng,
                zoom:15, //4 - displays in world map zoom
                mapTypeId:google.maps.MapTypeId.ROADMAP
            };
            var map = new google.maps.Map(document.getElementById("googleMap"), mapProp);


            //------------------------------ code for multiple markers using loops
            var places = [
                ['Ambalangoda Children Park', 6.241713, 80.052307, 2], //2 is the zIndex - order in which these markers should display on top of each other
                ['My Home', 6.242693, 80.051855, 1]
            ];

            var images = ['office2.ico', 'children.ico'];

            for( var i=0; i<places.length; i++ ){
                    var place = places[i];
                    var myMarker = new google.maps.Marker({
                            position: {lat: places[1], lng:places[2]},
                            map: map,
                            icon: images[i],
                            title: place[0],
                            zIndex: place[3]
                    });
            }

            //---------------------------------------------------------------------


            myMarker.addListener('click', function(){ 
                infowindow.open(map, myMarker); 
            })
        }

        google.maps.event.addDomListener(window, 'load', initialize)

    </script>

But without using loops, markers shows correctly. Here is code without loops;

var myMarker = new google.maps.Marker({
                position: {lat: 6.242693, lng: 80.051855},
                //position: latlng,
                map: map,
                title: "My Home",
                icon: image //instead of default Google Maps pushpin icon
            });

            var myMarker2 = new google.maps.Marker({
                position: {lat: 6.241713, lng: 80.052307},
                //position: latlng,
                map: map,
                title: "Ambalangoda Children Park",
                icon: image2 //instead of default Google Maps pushpin icon
            });

New Problem

Sir, this is my new problem when I am going to store markers in an array so that later I can easily access a particular marker. But when I modify the code, it does not work. Here is my array to store markers;

                var myMarker = new Array(); //store marker in array

            for( var i=0; i<places.length; i++ ){
                    var place = places[i];
                    var myMarker[i] = new google.maps.Marker({
                            position: {lat: place[1], lng:place[2]},
                            map: map,
                            title: place[0],
                            icon: images[i],                                
                            zIndex: place[3]
                    });

                    /*if( i == 1 ){
                            myMarker[1].setAnimation(google.maps.Animation.BOUNCE);

                            myMarker[1].addListener('click', function(){ 
                                infowindow.open(map, myMarker[1]); 
                            })
                    }*/
            }
ndsmyter
  • 6,535
  • 3
  • 22
  • 37
Amali Perera
  • 115
  • 1
  • 1
  • 8
  • 2
    In your "New Problem": what is the `var` doing in front of `myMarker[i]` ? Have you tried `push` for adding it to the array? Should work then. – ndsmyter May 20 '16 at 11:23
  • 2
    And by the way, if you have a new problem, it makes more sense to start a new question. And maybe do some searching on the internet yourself before you ask a question on SO – ndsmyter May 20 '16 at 11:25
  • @ndsmyter, Now it works! I have very poor javascript knowledge. Your comments are appreciated. I already googled javascript arrays. Thanks. Meanwhile I put my question until I read those articles. – Amali Perera May 20 '16 at 11:34

1 Answers1

1

You use wrong array for place (you use places instead of place)

           for( var i=0; i<places.length; i++ ){
                var place = places[i];
                var myMarker = new google.maps.Marker({
                        position: {lat: place[1], lng:place[2]},
                        map: map,
                        icon: images[i],
                        title: place[0],
                        zIndex: place[3]
                });
        }
geocodezip
  • 158,664
  • 13
  • 220
  • 245
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
  • thanks a lot lot. how stupid I am. I couldn't figure out even I am using spectacles. – Amali Perera May 20 '16 at 09:37
  • One more problem. If I want to animate only one icon, this code logic is not working if( place[0] == "My Home" ){ animation: google.maps.Animation.BOUNCE, } – Amali Perera May 20 '16 at 09:47
  • 1
    You can use markerObj.setAnimation(google.maps.Animation.BOUNCE); where markeObjr is a marker object so.. declare a var for the marker object you want animate .. assign to this var the object you want and after call the setAnimation – ScaisEdge May 20 '16 at 10:06
  • thanks. that works very well. simple and easy to understand – Amali Perera May 20 '16 at 10:30
  • Sir, I have faced a new problem when I tried to store markers in an array. I have mentioned it on my post about with new code modification. Please have a look. Thanks – Amali Perera May 20 '16 at 11:15
  • I suggest you of start a new question .. in this way all the community membre can help you .. eventually you can comment me the new url and if i'm not busy a can take a look .. – ScaisEdge May 20 '16 at 11:31
  • sir I have a new problem about maps direction service. I have put it in a new question. Please help me. I coded that myself reading SO posts. But now I am stuck: http://stackoverflow.com/questions/37351656/google-maps-direction-service-does-not-show – Amali Perera May 20 '16 at 16:38
  • It is very difficult for me to identify even syntax errors in javascript unlike PHP. Is there anyway that shows syntax error, to what point of code get executed successfuly, etc? – Amali Perera May 20 '16 at 16:46