<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>
和php文件是这个
<?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);
?>
答案 0 :(得分:0)
您需要使用ajax调用。 Here是一个关于Stack Overflow的问题,它有一些关于php页面的ajax调用的例子。
请记住,您需要包含一个jQuery库才能执行此操作,或者您可以在this question中进行操作以在没有jQuery的情况下进行ajax调用。
答案 1 :(得分:0)
您可以使用PHP将变量输出到隐藏的输入字段value
属性中,并将它们编码为JSON。然后,使用Javascript使用JSON.parse
从隐藏的输入字段的value属性解码PHP变量。然后,您可以从DOM中删除该元素。
答案 2 :(得分:0)
诀窍是将PHP var回显到javascript中。由于之前处理过Php,输出正确的Js。
<script>
var locations = array(<?= $VAR ?>);
</script>
在这种情况下,$ VAR必须是$ VAR =“'barcelona','madrid','london'”; 调整$ VAR以便生成正确的输出。如果您只是需要它,则无需使用JSON编码。