Trying to add markers from mysql by retrieving them using php and then json_encode()to then use the results in javascript. Currently I the map is displaying for me, but the markers are not!
MarkersController.php - fetching information from database.
$list = array();
while ($row = mysqli_fetch_assoc($result)) {
$list[] = array(
array('lat' => $lat),
array('lng' => $lng));
}
echo json_encode($list);
Landing.html
<script type="text/javascript" src="js/jquery-1.11.2.js"></script>
<script type = "text/javascript"
src = "https://maps.googleapis.com/maps/api/js?
key=AIzaSyB76xBqfQdgOLV77VK3JZ09vWwk8brkMFs">
</script>
<script type="text/javascript">
var map = null;
function addMarker(lat, lng) {
var myLatLng = new google.maps.LatLng(lat, lng);
marker = new google.maps.Marker({
position: myLatLng,
map: map
});
}
function initialize() {
var mapOptions = {
center: {lat: 54.872128, lng: - 6.284874},
zoom: 13
};
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
$.getJSON('MarkersController.php', function(data){
$.each(data, function(lat, lng){
addMarker('lat', 'lng');
});
})
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
Trying to use the array from MarkersController.php to implement the addMarker() function.
Try header('Content-Type: application/json');
before your echo or the result you are getting from server will be plain text.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.