在Google地图上为每个位置设置位置标记

时间:2017-09-20 04:48:56

标签: javascript php mysql

我有我正在使用的代码(我自己没有写过),它根据我在数据库中的lon和lat条目在Google地图上创建了一条折线。它现在运作良好。我想为地图上构成线的每个位置添加一个标记,这样我就可以点击它,然后查看该点的DB记录中的时间,lon和lat信息。

以下是当前代码:

<?php

function flightpath($days = 1) {



  if (($days > 100) || ($days < 1) || (!is_numeric($days))) {
    echo 'Please specify a correct number of days (maximum 100 days)';
    return;
  }


  $flightpath_part[0] = "<script>
      function initialize() {
        var myLatLng = new google.maps.LatLng(";

  $flightpath_part[1] = ");
        var mapOptions = {
          zoom: 11,
          center: myLatLng,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

        var flightPlanCoordinates = [
";

  $flightpath_part[2] = "
        ];
        var flightPath = new google.maps.Polyline({
          path: flightPlanCoordinates,
          strokeColor: '#FF0000',
          strokeOpacity: 1.0,
          strokeWeight: 3
        });

        flightPath.setMap(map);
      }

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

    </script>
";



  $conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
  if (mysqli_connect_errno()) {
    echo 'Connection with MySQL database failed!';
    exit();
  }
  $query = "SELECT `log`, `lat`, `lon` FROM `location` WHERE `log` >= ( DATE_SUB( CURDATE( ), INTERVAL " . ($days -1) . " DAY ) ) AND user_id = '$user_idA' ORDER BY `log` ASC";
  $result = mysqli_query($conn, $query);
  $rowcount = mysqli_num_rows($result);

  if ($rowcount == 0) {
    echo "<br /><br /><br /><center><p>You have no locations in your database for this period!</p></center>";

  } else {

    $lat_sum = 0;
    $lon_sum = 0;
    $loc_sum = 0;
    $flightpath = '';


    while ($line = mysqli_fetch_array($result, MYSQLI_ASSOC)) {

        foreach ($line as $col_value) {

            $lon = $line['lon'];
            $lat = $line['lat'];

        $lon_sum = $lon_sum + $lon;
        $lat_sum = $lat_sum + $lat;
        $loc_sum = $loc_sum + 1;

            if ($lon && $lat) {
                $flightpath .= "          new google.maps.LatLng(";
                $flightpath .= $line['lat'];
                $flightpath .= ", ";
                $flightpath .= $line['lon'];
                $flightpath .= "),\r\n";
            }
        }
    }


    $lon_center = $lon_sum / $loc_sum;
    $lat_center = $lat_sum / $loc_sum;
    $flightpath = substr_replace($flightpath, "", -3);


    $flightpath = $flightpath_part[0] . $lat_center . ', ' . $lon_center . $flightpath_part[1] . $flightpath . $flightpath_part[2];    
  }


  mysqli_close($conn);


  return $flightpath;
}

?>

    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&amp;libraries=visualization"></script>    
    <?php echo flightpath($days); ?>

1 个答案:

答案 0 :(得分:0)

由于数据库存储了lon和lat坐标,因此可以使用这些坐标创建地图标记。你可以尝试类似的东西:

function flightmarkers($days=1){
     $conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
      if (mysqli_connect_errno()) {
        echo 'Connection with MySQL database failed!';
        exit();
      }
      $query = "SELECT `log`, `lat`, `lon` FROM `location` WHERE `log` >= ( DATE_SUB( CURDATE( ), INTERVAL " . ($days -1) . " DAY ) ) AND user_id = '$user_idA' ORDER BY `log` ASC";
      $result = mysqli_query($conn, $query);
      $rowcount = mysqli_num_rows($result);

      if ($rowcount == 0) {
        echo "<br /><br /><br /><center><p>You have no locations in your database for this period!</p></center>";

      } else {
        $flightmarkers= '';
        while ($line = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
            foreach ($line as $col_value) {
                $lon = $line['lon'];
                $lat = $line['lat'];
                if ($lon && $lat) {
                    $flightmarkers.= 
                         'marker = new google.maps.Marker({
                          position: new google.maps.LatLng('.$line['lat'].','.$line['lon'].'),
                          map: map
                          });
                          ';
                }
        }
        return $flightmarkers;
  }
 <?php echo flightpath($days); ?>
 <?php echo flightmarkers($days); ?>

我无法测试它,我不确定* _sum vars的重点,因为他们静态设置为0所以他们真的没有做任何事情。为简单起见我删除了它们。这至少可以让你开始朝着正确的方向前进。

此链接还提供了有关地图标记的一些信息Google Maps JS API v3 - Simple Multiple Marker Example