簡體   English   中英

在Google地圖上為每個位置設置位置標記

[英]Setting location markers for each location on google map

我有正在使用的代碼(我自己沒有寫過),該代碼根據我數據庫中的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); ?>

由於數據庫存儲了經緯度坐標,因此您可以使用它們來創建地圖標記。 您可以嘗試類似:

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 var的意義,因為它們被靜態設置為0,所以它們實際上什么也沒做。 為了簡單起見,我刪除了它們。 這至少可以幫助您朝正確的方向入手。

此鏈接提供了有關地圖標記的大量信息,以及Google Maps JS API v3-簡單多個標記示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM