【问题标题】:Only load 10 markers from database仅从数据库加载 10 个标记
【发布时间】:2017-02-26 20:13:24
【问题描述】:

我有一个问题。我使用谷歌地图制作了一个项目。我在数据库中放了 30 个数据。但是当我想在地图上显示标记时,它只加载 10 个数据。同样,当我根据类别过滤数据时,它仅加载 10 个数据。我在我的浏览器上检查了 inspact 元素(我使用 mozilla)并发现了这个错误:“SyntaxError:未终止的字符串文字”这是什么意思?

这是我在网络上显示标记和地图的代码:

<?php
$title = "Peta Masjid";
include_once "header.php";
?>

      <div class="row">

        <div class="col-md-12"> 
          <div class="panel panel-info panel-dashboard centered">
            <div class="panel-heading">
              <h2 class="panel-title"><strong>  <?php echo $title ?>  </strong></h2>
            </div>
            <div class="panel-body">
              <div id="map" style="width:100%;height:380px;"></div>

				
			  


	
<script src="https://maps.googleapis.com/maps/api/js?libraries=places&key=AIzaSyAbXF62gVyhJOVkRiTHcVp_BkjPYDQfH5w">
</script>
	
<script type="text/javascript">
  function initialize() {
    
    var mapOptions = {   
        zoom: 12,
        center: new google.maps.LatLng(-6.966667, 110.416664), 
        disableDefaultUI: true
    };

    var mapElement = document.getElementById('map');

    var map = new google.maps.Map(mapElement, mapOptions);

    setMarkers(map, officeLocations);

}

var officeLocations = [
<?php
$data = file_get_contents('http://localhost:8082/sig/getdata.php');
                $no=1;
                if(json_decode($data,true)){
                  $obj = json_decode($data);
                  foreach($obj->results as $item){
?>
['<?php echo $item->id_masjid ?>','<?php echo $item->nama_masjid ?>','<?php echo $item->alamat ?>', '<?php echo $item->longitude ?>', '<?php echo $item->latitude ?>','<?php echo $item->gambar ?>'],
<?php 
}
} 
?>    
];

function setMarkers(map, locations)
{
    var globalPin = 'img/tanda.png';

    for (var i = 0; i < locations.length; i++) {
       
        var office = locations[i];
        var myLatLng = new google.maps.LatLng(office[4], office[3]);
        var infowindow = new google.maps.InfoWindow({content: contentString});
         
        var contentString = 
			'<div id="content">'+
            '<div id="siteNotice">'+
            '</div>'+
            '<h5 id="firstHeading" class="firstHeading">'+ office[1] + '</h5>'+
            '<div id="bodyContent">'+ 
			'<p>' + office[2] + '</p>' + 
			'<p><img src="img/'+office[5]+'" style="width:50%;height:190px;" /></p>' +
			'<p><a href=detail.php?id='+office[0]+'>Info Detail</p></a>'+
            '</div>'+
            '</div>';

        var marker = new google.maps.Marker({
            position: myLatLng,
            map: map,
            title: office[1],
            icon:'img/tanda.png'
        });

        google.maps.event.addListener(marker, 'click', getInfoCallback(map, contentString));
    }
}

function getInfoCallback(map, content) {
    var infowindow = new google.maps.InfoWindow({content: content});
    return function() {
            infowindow.setContent(content); 
            infowindow.open(map, this);
        };
}

initialize();
</script>



            </div>
          </div></div></div>
      </div>
    </div>
<?php include_once "footer.php"; ?>

这是从数据库中获取数据的代码 (getdata.php)

<?php
include "connection.php";
$Q = mysql_query("SELECT * FROM mosques WHERE category = 'Mushola'")or die(mysql_error());
if($Q){
 $posts = array();
      if(mysql_num_rows($Q))
      {
             while($post = mysql_fetch_assoc($Q)){
                     $posts[] = $post;
             }
      }  
      $data = json_encode(array('results'=>$posts));
      echo $data;                     
}

?>

【问题讨论】:

标签: javascript php google-maps google-maps-api-3


【解决方案1】:

您可以在 while 循环中使用计数到 10 的计数器,然后中断循环或 if 语句,而不是仅在计数器不是 10 时运行

-阿隆 DC

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-28
    • 2012-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-01
    • 2012-12-06
    • 2014-11-14
    相关资源
    最近更新 更多