Jump to content

problem with js in building google map


Lassie

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
https://forums.phpfreaks.com/topic/297306-problem-with-js-in-building-google-map/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.