Leaflet显示从数据库到地图的所有标记

时间:2014-11-19 01:31:41

标签: javascript php

首先,我想在问我的问题之前展示我的代码。

以下代码将显示从数据库到我的网页的所有数据

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dbmarker";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * from tblmarker";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
     // output data of each row
     while($row = $result->fetch_assoc()) {
         echo  "<br>",$row["lat"],"     &nbsp     &nbsp    &nbsp    &nbsp    &nbsp      ",$row["lng"] ;
     }
} else {
     echo "0 results";
}

$conn->close();
?>  

这是我的代码的输出,它将显示所有Lat / Long

14.704659335030026           121.02397441864012
14.704659335030026           121.02397441864012
14.704659335030026           121.02397441864012
14.704659335030026           121.02397441864012
14.704659335030026           121.02397441864012
14.704659335030026           121.02397441864012
14.704659335030026           121.02397441864012
14.704659335030026           121.02397441864012
14.704659335030026           121.02397441864012

我的问题是如何在此代码中连接此输出?

var note = document.getElementById('note');
var datepick = document.getElementById('demo1');
var timepick = document.getElementById('timepick');
        layerpoly.on('click', function(e){
        var markerA = new L.Marker(e.latlng,{icon: Icon1});
        markerA.bindPopup("</a><br><strong>FIRE</strong></br><strong>Date:</strong>"+datepick.value+"</br><strong>Time:</strong>"+timepick.value+"</br><strong>Address:</strong>"+note.value+"<strong><br><strong>Suspect Sketch</strong><br><a href=legends/suspect.jpg rel=lightbox><img src = legends/suspect.jpg height=100 width = 100/>").addTo(map);
        closure1 (markerA)

或者为了简化我的问题,我如何获取这些输出并将其转换为Leaflet Lat / long of my marker?当网页完成加载时,将获得lat / long并在地图上放置标记会发生什么。 TY

1 个答案:

答案 0 :(得分:0)

您可以在php数据中创建数组,例如:

$data = array();
    echo " var latlong = [ " ;

for ( $x = 0; $x < mysql_num_rows( $query); $x++) {
    $data[] = mysql_fetch_assoc( $query);
    echo " [ " , $data[ $x][ 'lat' ], " , " , $data[ $x][ 'long' ], " ] " ;
    if ( $x <= ( mysql_num_rows( $query) - 2) ) {
        echo " , " ;
    }
}
    echo " ]; " ;

调整到您的字段的这段代码创建了var latlong,在您的情况下获得输出:

latlong = [
[14.704659335030026,121.02397441864012],
[14.704659335030026,121.02397441864012],
[14.704659335030026,121.02397441864012],
[14.704659335030026,121.02397441864012],
[14.704659335030026,121.02397441864012],
[14.704659335030026,121.02397441864012],
[14.704659335030026,121.02397441864012],
[14.704659335030026,121.02397441864012],
[14.704659335030026,121.02397441864012]];

然后你只需将你的php添加到地图页面中,如:

<?php include 'latlong.php' ?>

和要显示的点的功能如:

for ( var i = 0; i < latlong.length; i ++) {
marker = new L.marker([latlong[i][ 1 ],latlong[i][ 2]])
.addTo(map);
}