douyeyan0650 2016-05-11 17:05
浏览 24

Google地图仅使用PHP和JavaScript显示来自MySQL数据库的最后一个标记

When I try to show multiple markers from the database information, the map only shows the last of the markers returned with its infoWindow.

I read other post that talks about a clousure, but I can't resolve my problem.

This is the original code:

  <body>
    <?php
      include("conexion.php");
      $tipo = $_POST['type'];
    ?>
    <div class="container">
      <div class="row">
        <div class="col-md-12">
          <h1>Map</h1>
          <div id="map" style="width:100%;height:360px;"></div><br><br>
          <?php
            $con = mysqli_connect($host, $user, $pass, $db_name) or die("Error");
            $query = "select * from table where type_name='".$type."'";
            $result = mysqli_query($con,$query);
            $i=1;
            while ($data = mysqli_fetch_assoc($result)) {
          ?>
            <script type="text/javascript">
              var cat = new google.maps.LatLng(41.652393,1.691895);
              var mapOptions = {
                  center: cat,
                  zoom: 8,
                  mapTypeId: google.maps.MapTypeId.ROADMAP
                };
              map = new google.maps.Map(document.getElementById('map'), mapOptions);

              var marker<?php echo $i;?> = new google.maps.Marker({
                    position: new google.maps.LatLng(<?php echo $data['lat']; ?>, <?php echo $data['lng']; ?>),
                    map: map,
                    title: <?php echo "'".$data['name']."'"; ?>
              });

              var infowindow<?php echo $i;?> = new google.maps.InfoWindow({
                  content: "<h1><?php echo "".$data['name'].""; ?></h1><p><b>Addres</b><br> <?php echo "".$data['addres'].""; ?></p><p><b>Description</b><br><?php echo "".$data['description'].""; ?></p>"
              });

              google.maps.event.addListener(marker<?php echo $i;?>, 'click', function() {
                  infowindow<?php echo $i;?>.open(map,marker<?php echo $i;?>);
              });
            </script>

            <?php
                  $i++;
                }
              mysqli_close($con);
            ?>
        </div>
      </div>
    </div>
  </body>

nghngh

  • 写回答

1条回答 默认 最新

  • douyong1905 2016-05-11 17:08
    关注

    Just put all the markers on an array, also do the while inside the js, you are creating the map var for every interaction, or create the elements on the js with an ajax, this will improve a lot for performance.

    also take a look to this Google Maps JS API v3 - Simple Multiple Marker Example

    评论

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥15 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教