有2行数据库在Google Map上显示相同的点

时间:2012-02-09 18:42:43

标签: php javascript

我正在尝试在地图上显示profFNameprofLName的多个值,其中单个点应显示此值。

所以,如果我有(2)pID连接到同一个标记......它应该在一个点上显示profFNameprofLName

**现在它正在以完全相同的纬度/经度创建两个标记,因此它绘制了3个点(从数据库中可以看出)但如果在该位置存在多个教授则不显示(1)点** < / p>

的Javascript

    <script type="text/javascript">
    //<![CDATA[

    var iconBlue = new GIcon(); 
    iconBlue.image = 'http://labs.google.com/ridefinder/images/mm_20_blue.png';
    iconBlue.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
    iconBlue.iconSize = new GSize(12, 20);
    iconBlue.shadowSize = new GSize(22, 20);
    iconBlue.iconAnchor = new GPoint(6, 20);
    iconBlue.infoWindowAnchor = new GPoint(5, 1);

    var iconRed = new GIcon(); 
    iconRed.image = 'http://labs.google.com/ridefinder/images/mm_20_red.png';
    iconRed.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
    iconRed.iconSize = new GSize(12, 20);
    iconRed.shadowSize = new GSize(22, 20);
    iconRed.iconAnchor = new GPoint(6, 20);
    iconRed.infoWindowAnchor = new GPoint(5, 1);

    var customIcons = [];
    customIcons["restaurant"] = iconBlue;
    customIcons["bar"] = iconRed;

    function load() {
      if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map"));
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        map.setCenter(new GLatLng(47.614495, -122.341861), 13);

        GDownloadUrl("phpsqlajax_genxml.php", function(data) {
          var xml = GXml.parse(data);
          var markers = xml.documentElement.getElementsByTagName("marker");
          for (var i = 0; i < markers.length; i++) {
            var name = markers[i].getAttribute("name");
            var address = markers[i].getAttribute("address");
            var profFName = markers[i].getAttribute("profFName");
             var profLName = markers[i].getAttribute("profLName");
            var type = markers[i].getAttribute("type");
            var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
                                    parseFloat(markers[i].getAttribute("lng")));
            var marker = createMarker(point, name, address, profFName, profLName, type);
            map.addOverlay(marker);
          }
        });
      }
    }

    function createMarker(point, name, address, profFName, profLName, type) {
            var marker = new GMarker(point, customIcons[type]);
            var html = "<b>" + name + "</b> <br/>" + address + "<br><br>" + profFName + profLName;
            GEvent.addListener(marker, 'click', function() {
              marker.openInfoWindowHtml(html);
            });
              return marker;
    }
    //]]>
  </script>

  </head>

  <body onload="load()" onunload="GUnload()">
    <div id="map" style="width: 110 0px; height: 500px"></div>
  </body>

PHP

<?php
require("phpsqlajax_dbinfo.php");

function parseToXML($htmlStr) 
{ 
$xmlStr=str_replace('<','&lt;',$htmlStr); 
$xmlStr=str_replace('>','&gt;',$xmlStr); 
$xmlStr=str_replace('"','&quot;',$xmlStr); 
$xmlStr=str_replace("'",'&#39;',$xmlStr); 
$xmlStr=str_replace("&",'&amp;',$xmlStr); 
return $xmlStr; 
} 

// Opens a connection to a MySQL server
$connection=mysql_connect ($host, $username, $password);
if (!$connection) {
  die('Not connected : ' . mysql_error());
}

// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
  die ('Can\'t use db : ' . mysql_error());
}

// Select all the rows in the markers table
$query = "SELECT * FROM markers M, professor P
WHERE P.id = M.id;";
if($row = mysql_fetch_array($result)) {
    do {
        echo $row;
    } 
    while($row = mysql_fetch_array($result));
} else {
    die('No results.');
}

header("Content-type: text/xml");

// Start XML file, echo parent node
echo '<markers>';

// Iterate through the rows, printing XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
  // ADD TO XML DOCUMENT NODE
  echo '<marker ';
  echo 'name="' . parseToXML($row['name']) . '" ';
  echo 'address="' . parseToXML($row['address']) . '" ';
  echo 'profFName="' . parseToXML($row['profFName']) . '" ';
  echo 'profLName="' . parseToXML($row['profLName']) . '" ';
  echo 'lat="' . $row['lat'] . '" ';
  echo 'lng="' . $row['lng'] . '" ';
  echo 'type="' . $row['type'] . '" ';
  echo '/>';
}

// End XML file
echo '</markers>';

?>

img1

img2

1 个答案:

答案 0 :(得分:0)

快速解决方案:

  1. 使用GROUP_CONCAT功能 Can I concatenate multiple MySQL rows into one field?
  2. 仅显示标记并为每个标记添加属性ID。当您单击标记时 从数据库中选择并连接收到的行
  3. 创建添加标记的关联数组{id:marker reference}。在函数createMarker中 检查这个数组,如果标记id已经存在,请将数组中标记的html连接起来 标记在函数中传递。如果没有将标记添加到数组。