- My javascript is populating markers on map and for that i need locations ,which i have generated from php file in the format given below
- i want to dynamically get the result of the php array in the javascript array
<script type="text/javascript">
var locations = /// i want here the output as shown below from the php file
var locations = [["Vidyasagar","28.6139391","77.20902120000005"],
["Pushpadantsagar","21.4598","80.195"],
["Tarunsagar","28.638","77.2936"],
["Samyaktbhushan","20.593684","78.96288000000004"],
["Pavitrasagar","23.2836","79.2318"],
["Prayogsagar","23.2836","79.2318"],
["Arunsagar","30.016","77.4"]];
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 7,
center: new google.maps.LatLng(23.2836,79.2318),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
var marker, i;
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
</script>
and the php file is this
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "jainmunilocator";
$connection=mysqli_connect($servername, $username, $password, $dbname);;
if (!$connection) {
die("Connection failed: " . mysqli_connect_error());
}
$array = array();
$sql = "SELECT name,id,lat,lng FROM muni_location,munishri WHERE mid=id AND lat<>0";
$result=mysqli_query($connection,$sql);
$i=0;
while($row = mysqli_fetch_assoc($result)){
if(isset($row)){
$array[$i][0]=$row['name'];
$array[$i][1]=$row['lat'];
$array[$i][2]=$row['lng'];
$i++;
}
}
echo json_encode($array);
?>