我有一个带gps点的sql数据库,我想在google maps api上将它们设置为标记。 在初始化sql连接之后,我启动了一个while循环,其中我先实现一个php脚本,然后是javascript。但由于某种原因它不起作用。我不知道我犯了什么错误? 代码如下所示:
......stuff before
<?php
$i=1;
while($i <48):
?>
<!-- SELECT GPS DATA FROM SQL DATABASE AND CONVERT INTO DECIMALS ------------------->
<?php
//select 4 numbers (longitude, altitude) from database
$result = mysql_query("SELECT kglat, kmlat, kglon, kmlon FROM falpeg WHERE indix='$i'");
....
....
.... (converting into decimals for google maps, used mysql_fetch_row, this part should be alright)
...
$klat = ($kmlat / 60) + $kglat;
round ($kord, 5);
$klon = ($kmlon / 60) + $kglon;
?>
<!-- START JAVASCRIPT --------------------------------------->
<script type="text/javascript">
var "<?php echo $i; ?>" = {lat: "<?php echo $klat; ?>", lng: "<?php echo $klon; ?>";
var marker=new google.maps.Marker({
position: "<?php echo $i; ?>",
icon:'cross.png'
});
marker.setMap(map);
var infowindow = new google.maps.InfoWindow({
content:"Hello World!"
});
marker.addListener('click', function() {
infowindow.open(map, marker);
});
</script>
<!-- END WHILE LOOP, SET I++ ------------------->
<?php
$i++;
endwhile;
?>
答案 0 :(得分:0)
让我们看看我们是否可以清理一下。
•使用PDO(很多好处,但我们只是说它更容易一点)
•构建一个查询,获取所需的所有数据,而不是循环运行单个查询
•尽量不要在整个地方回应PHP ..!
•当你说echo $ i时,我看到你要做什么,但是,$ i只是一个整数值,所以你需要先将数据存储到数组中,然后使用$ myArray [$ i]; <登记/>
•利用json_encode获取所需格式的信息
PHP:
<?php
// connect to the DB using PDO
$db = new PDO('mysql:host=host;dbname=dbname','username','password');
// Create a SQL statement that will return ALL of the rows you need
$min = 0;
$max = 47;
$sql = "SELECT kglat, kmlat, kglon, kmlon FROM falpeg WHERE indix BETWEEN $min AND $max;");
$query = $db->query($sql); // run the query
$results = $query->fetchall(); // fetch the results
// these results are in an array..! You can access each item
// $results[index]['columnname'] index is just an integer starting a 0
// ex. $results[0]['kglat'] is the kglat value of the first element
$markers = Array(); // we'll use this to store the marker info in the same structure
// you're using in the google.maps.create
// Now, let's loop through each item in the results and populate our $markers array
foreach($results as $i=>$m){
// $m is just a single row from $result [a single (m)arker if you will..]
// so $m['kmlat'] is how you access the 'kmlat' property
// of the current row as we loop through. $i is the row number.
// structuring this so it matches the format you
// need when you pass the info to google maps
$markers[$i]['position']['lat'] = ($m['kmlat'] / 60) + $m['kglat']; // add 'lat' value to $markers
$markers[$i]['position']['lng'] = ($m['kmlon'] / 60) + $m['kglon']; // add 'lng' value to $markers
$markers[$i]['icon'] = 'cross.png';
}
// this will format the data so javascript can use it easily
// you don't need to manualy do it.
$markers = json_encode($markers);
?>
使用Javascript:
<script>
var markerData = <?php echo $markers ?>; //just plop in our structure data, shouldn't need to format (hopefully)
markerData.forEach(function($i){ // we shouldn't need to format $i because json_encode did that for us
markers[] = new google.maps.Marker($i); // 'markers' will be an array of goole map markers
});
</script>