谷歌地图显示多个位置(错误:locations [i]未定义)

时间:2017-11-03 13:28:03

标签: javascript php google-maps while-loop

我正在尝试使用Google Map API显示多个用户位置,我必须显示master_service_provider表中的所有值,因为我正在使用while循环。

但我收到JavaScript错误类型错误:位置[i]未定义

 <div id="map"></div>
        <script>
          var map;
          function initMap() {
            var i = '0';
    <?php
    $result=$conn->query("SELECT * FROM `master_service_provider`");

       $a=0;
       while($row = mysqli_fetch_array($result)) {
    $service_provider_id = $row['id'];
              $fullname = $row['fullname'];
           $fulladdress = $row['fulladdress'];
                 $phone = $row['phone'];
                   $lat = $row['lat'];
                   $lng = $row['lng'];

    ?>
     var <?php echo 's'.$phone; ?> = {
               info: '<h3><?php echo $fullname; ?></h3>\
                      <h4><?php echo $fulladdress; ?></h4>\
                      <a href="view_service_provider.php?service_provider_id=<?php echo $service_provider_id; ?>">View Info</a>',
                      lat: <?php echo $lat; ?>,
                      long: <?php echo $lng; ?>
      };


      var locations = [
          [<?php echo 's'.$phone; ?>.info, <?php echo 's'.$phone; ?>.lat, <?php echo 's'.$phone; ?>.long, <?php echo $a++; ?>],
        ];

        marker = new google.maps.Marker({
          position: new google.maps.LatLng(locations[i][1], locations[i][2]),
          map: map
        });

        google.maps.event.addListener(marker, 'click', (function (marker, i) {
          return function () {
            infowindow.setContent(locations[i][0]);
            infowindow.open(map, marker);
          }
        })(marker, i));

        i++;


     <?php } ?>

      var map = new google.maps.Map(document.getElementById('map'), {
        zoom: 11,
        center: new google.maps.LatLng(19.198313, 72.893533),
        mapTypeId: google.maps.MapTypeId.ROADMAP
      });

      var infowindow = new google.maps.InfoWindow({});

      var marker;


    }

        </script>

1 个答案:

答案 0 :(得分:1)

我认为应该是这样的。

自己填写......部分,但保留代码的一般结构。

如果你让它工作,请告诉我

<?php
...
// do this on top, not in the middle of javascript
$result = $conn->query("SELECT id, lat, lng, fullname, fulladdress, phone FROM master_service_provider");  // don't use *, specify the columns you want

$mydata = array();
while($row = mysqli_fetch_assoc($result)) { // use _assoc rather than _array
  $mydata[] = $row ;   // push current row to the data object
}
// now we translate this php array to a javascript array of objects.
// it should resemble something like:
// var locations = [ {"id": "1", "lat":"4.51", "lng":"50.53", "fullname":"John Smith", ... }, {"id": "2", lat":"5.14", ...} ] ;
// use json_encode() to realize this

echo '<script>var locations = '. json_encode($mydata) .' ;</script>';
?>

<script>
  var map;
  function initMap() {

    ...
   for(var i=0; i<locations.length; i++) {

    marker = new google.maps.Marker({
      position: new google.maps.LatLng(locations[i]['lat'], locations[i]['lng']),
      title: locations[i]['fullname'],
      map: map
    });

    ...

    google.maps.event.addListener(marker, 'click', (function (marker, i) {
      return function () {
        infowindow.setContent('phone: ' + locations[i]['phone']);
        infowindow.open(map, marker);
      }
    })(marker, i));
   }
   ...

  }
</script>