Lassie Posted July 15, 2015 Share Posted July 15, 2015 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> Quote Link to comment Share on other sites More sharing options...
Solution scootstah Posted July 15, 2015 Solution Share Posted July 15, 2015 Based on your other thread, latitude and longitude are not arrays. So you just need: position: new google.maps.LatLng(coords[i].latitude, coords[i].longitude), Quote Link to comment Share on other sites More sharing options...
Lassie Posted July 15, 2015 Author Share Posted July 15, 2015 Right. Many thanks I have a result, now to sort out the other isssues. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.