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> Link to comment https://forums.phpfreaks.com/topic/297306-problem-with-js-in-building-google-map/ Share on other sites More sharing options...
scootstah Posted July 15, 2015 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), Link to comment https://forums.phpfreaks.com/topic/297306-problem-with-js-in-building-google-map/#findComment-1516403 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. Link to comment https://forums.phpfreaks.com/topic/297306-problem-with-js-in-building-google-map/#findComment-1516404 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.