Jump to content

problem with js in building google map


Lassie
Go to solution Solved by scootstah,

Recommended Posts

I am trying to make a google map with multiple markers. I have data drawn from mysql and passed to the js.

I cant however get the variables for lat and lng to be recognised in my script.

Any help appreciated.

<script>
                                
                                var coords = <?php echo $output?>;
                                
                                console.dir(coords);
                                
                                
                               
 
                            </script>
                            
                             <div id="map" style="width: 500px; height: 400px;"></div>
                             
                            <script src="http://maps.google.com/maps/api/js?sensor=false" 
                            type="text/javascript"></script>
                            
                            <script type="text/javascript">
                                //replace with coords, postcode, lat, lng
                                // verify we have the right info
                            for (var i = 0; i < coords.length; i++) {
                             document.write('Postcode: ');
                             document.write(coords[i].postcode);
                             document.write('<br />');
                             document.write('Latitude: ');
                             document.write(coords[i].latitude);
                             document.write('<br />');
                             document.write('Longitude: ');
                             document.write(coords[i].longitude);
                             document.write('<br />');
                            }
                                
                           
                            var map = new google.maps.Map(document.getElementById('map'), {
                             zoom: 10,
                             center: new google.maps.LatLng(0, -0),
                            mapTypeId: google.maps.MapTypeId.ROADMAP
                            });
                            var infowindow = new google.maps.InfoWindow();

                            var marker, i;

                            for (i = 0; i < coords.length; i++) {  
                            marker = new google.maps.Marker({
                            position: new google.maps.LatLng(coords[i].latitude[1], coords[i].longitude[2]),
                            map: map
                                });

                            google.maps.event.addListener(marker, 'click', (function(marker, i) {
                            return function() {
                            infowindow.setContent(coords[i].postcode[0]);
                            infowindow.open(map, marker);
                            }
                             })(marker, i));
                           }
                        </script> 
                                              
                            
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.