2016-11-22 152 views
1
<?php 
    include 'db.php'; 
    $sql_locations = "SELECT * FROM location";   
    $result = mysqli_query($conn,$sql_locations); 
    $count = mysqli_num_rows($result);  
    $markers = array(); 

    while($row = mysqli_fetch_array($result,MYSQL_ASSOC))   
    { 

     $markers = array(  
     array(
     'abc', 
     $row['latitude'], 
     $row['longitude'] 
     ));  

    } ?> 

這是我從數據庫中獲取位置的代碼。我有下面的腳本顯示地圖。While循環只顯示一行?

<script> 
function initialize() { 
    var map; 
    var bounds = new google.maps.LatLngBounds(); 
    var mapOptions = { 
     mapTypeId: "roadmap", 
     center: new google.maps.LatLng(20.5937, 78.9629), // somewhere in the uk BEWARE center is required 
     zoom: 1, 
    }; 
    // Display a map on the page 
    map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions); 
    map.setTilt(45); 

    // Multiple Markers 
    var markers = <?php echo json_encode($markers);?>; 



    // Display multiple markers on a map 
    var infoWindow = new google.maps.InfoWindow(); 
    var marker, i; 

    // Loop through our array of markers & place each one on the map 
    for (i = 0; i < markers.length; i++) { 
     var position = new google.maps.LatLng(markers[i][1], markers[i][2]); 
     bounds.extend(position); 
     marker = new google.maps.Marker({ 
      position: position, 
      map: map, 
      title: markers[i][0] 
     }); 

     // Allow each marker to have an info window 
     google.maps.event.addListener(marker, 'click', (function (marker, i) { 
      return function() { 
       infoWindow.setContent(infoWindowContent[i][0]); 
       infoWindow.open(map, marker); 
      } 
     })(marker, i)); 

     // Automatically center the map fitting all markers on the screen 
     map.fitBounds(bounds); 
    } 

    //Override our map zoom level once our fitBounds function runs (Make sure it only runs once) 
    var boundsListener = google.maps.event.addListener((map), 'bounds_changed', function (event) { 
     this.setZoom(5); 
     google.maps.event.removeListener(boundsListener); 
    }); 

} 
google.maps.event.addDomListener(window, 'load', initialize); 

我的地圖是工作的罰款。即時通訊面臨的問題,顯示我的數據庫中的所有記錄。我的while循環只能從表中首先顯示,請幫助我。 我想顯示數據庫的所有位置與他們的markers.on print_r o $ markers它只顯示錶的第一行。在$ rows的var_dump上顯示所有數據。但即時消息我的第一排只有一個標記。我的表中共有2行。提前致謝。

+5

您覆蓋每個循環 – 2016-11-22 06:24:46

+0

'$標記[] =陣列( 'ABC',$行[ '緯度'],$您的變量行['經度']);' –

回答

1

您將要覆蓋在while循環您$markers變量。所以,你需要做的象下面這樣: -

$markers[] = array('abc',$row['latitude'],$row['longitude']); 

或者(如果需要外部陣列)

$markers[] = array(array('abc',$row['latitude'],$row['longitude'])); 
+0

ammydon感謝標記。很高興幫助你:) :) –

1

您正在更新整個數組而不是向其添加元素。

變化

$markers = array(  
    array(
    'abc', 
    $row['latitude'], 
    $row['longitude'] 
    ));  

$markers[] = array(  
    array(
    'abc', 
    $row['latitude'], 
    $row['longitude'] 
    ));  
1

您將要覆蓋在每次迭代變量$markers

你只是需要添加一個新的數組來代替:

// Define the variable 
$markers = array(); 

while($row = mysqli_fetch_array($result,MYSQL_ASSOC))   
{ 
    // Push a new element to the array instead of overwrite it. 
    $markers[] = array(
     'abc', 
     $row['latitude'], 
     $row['longitude'] 
    );  

} 
+0

感謝兄弟它爲我工作。 – ammydon

+0

如果有效,請隨時將問題標記爲已解決 –