我想在两个不同的数据库中在谷歌地图上显示两种不同类型的标记

时间:2012-01-22 21:59:47

标签: php mysql google-maps google-maps-markers

我很难搞清楚如何在地图上显示两组不同的标记。我有两个不同的数据库,每个数据库都有一个表,我需要从中提取数据才能显示。对于一个表格,格式为id,street,city,state,zip_code,type和lat / lng,如下所示。我可以很好地显示这个属性列表,但是当我尝试从第二个表中添加一个不同的集时,它不起作用。我已粘贴了我的初始代码,该代码适用于下表:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>propertylocations</title>
    <script src="http://maps.google.com/maps?file=api&v=2&key=mykey" 
       type="text/javascript"></script>
    <script type="text/javascript">

    var iconFlag = new GIcon(); 
    iconFlag.image = 'images/redflag.png';
    iconFlag.iconSize = new GSize(12, 20);
    iconFlag.iconAnchor = new GPoint(6, 20);
    iconFlag.infoWindowAnchor = new GPoint(5, 1);

    var iconBlue = new GIcon(); 
    iconBlue.image = 'images/glossy-ball.png';
    iconBlue.iconSize = new GSize(12, 20);
    iconBlue.iconAnchor = new GPoint(6, 20);
    iconBlue.infoWindowAnchor = new GPoint(5, 1);

    var iconRed = new GIcon(); 
    iconRed.image = 'images/redbutton.png';
    iconRed.iconSize = new GSize(12, 20);
    iconRed.iconAnchor = new GPoint(6, 20);
    iconRed.infoWindowAnchor = new GPoint(5, 1);

    var customIcons = [];
    customIcons["rental"] = iconFlag;
    customIcons["commercial"] = iconBlue;
    customIcons["residential"] = iconRed;

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

        // Change this depending on the name of your PHP file
        GDownloadUrl("markers4.php", function(data) {
          var xml = GXml.parse(data);
          var markers = xml.documentElement.getElementsByTagName("marker");
          for (var i = 0; i < markers.length; i++) {
            var id = markers[i].getAttribute("id");
            var street = markers[i].getAttribute("street");
            var city = markers[i].getAttribute("city");
            var state = markers[i].getAttribute("state");
            var zip_code = markers[i].getAttribute("zip_code");
            var type = markers[i].getAttribute("type");
            var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
                                    parseFloat(markers[i].getAttribute("lng")));
            var marker = createMarker(point, id, street, city, state, zip_code, type);
            map.addOverlay(marker);
           }
        });
      }
    }

    function createMarker(point, id, street, city, state, zip_code, type) {
      var marker = new GMarker(point, customIcons[type]);
      var html = "<b>" + id + "</b> <br/>" + street + "<br/>" + city + "<br/>" + state + "<br/>" + zip_code +"<br/>" + type;
      GEvent.addListener(marker, 'click', function() {
        marker.openInfoWindowHtml(html);
      });
      return marker;
    }
    //]]>
  </script>
  </head>
<body background="images/bg2.jpg">
  <body onload="load()" onunload="GUnload()">
    <center><div id="map" style="width: 800px; height: 600px"></div></center>
  </body>
</html>

我试图执行以下操作,它显示了第二个表中的数据,但它将图标更改为默认图标并将html标注框格式化为相同 - 因此显示正确,并显示其他表格中的所有图标作为“未定义。”

这里的所有内容都与上面的相同,只是我添加了第二个GdownloadURL并创建了标记功能(所以我省略了上面可以看到的顶部)。

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

         // Change this depending on the name of your PHP file
        GDownloadUrl("markers4.php", function(data) {
          var xml = GXml.parse(data);
          var markers = xml.documentElement.getElementsByTagName("marker");
          for (var i = 0; i < markers.length; i++) {
            var id = markers[i].getAttribute("id");
            var street = markers[i].getAttribute("street");
            var city = markers[i].getAttribute("city");
            var state = markers[i].getAttribute("state");
            var zip_code = markers[i].getAttribute("zip_code");
            var type = markers[i].getAttribute("type");
            var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
                                    parseFloat(markers[i].getAttribute("lng")));
            var marker = createMarker(point, id, street, city, state, zip_code, type);
            map.addOverlay(marker);
           }
        });
    GDownloadUrl("markers6.php", function(data) {
          var xml = GXml.parse(data);
          var markers = xml.documentElement.getElementsByTagName("marker");
          for (var i = 0; i < markers.length; i++) {
            var realtor_phone = markers[i].getAttribute("realtor_phone");
            var availability_date = markers[i].getAttribute("availability_date");
            var type = markers[i].getAttribute("type");
            var point = new GLatLng(parseFloat(markers[i].getAttribute("latitude")),
                                    parseFloat(markers[i].getAttribute("longitude")));
            var marker = createMarker(point, realtor_phone, availability_date, type);
            map.addOverlay(marker);
           }
        });
      }
    }

    function createMarker(point, id, street, city, state, zip_code, type) {
      var marker = new GMarker(point, customIcons[type]);
      var html = "<b>" + id + "</b> <br/>" + street + "<br/>" + city + "<br/>" + state + "<br/>" + zip_code +"<br/>" + type;
      GEvent.addListener(marker, 'click', function() {
        marker.openInfoWindowHtml(html);
      });
      return marker;
    }

    function createMarker(point, realtor_phone, availability_date, type) {
      var marker = new GMarker(point, customIcons[type]);
      var html = "<b>" + realtor_phone + "</b><br/>" + availability_date;
      GEvent.addListener(marker, 'click', function() {
        marker.openInfoWindowHtml(html);
      });
      return marker;
    }
    //]]>
  </script>
  </head>
<body background="images/bg2.jpg">
  <body onload="load()" onunload="GUnload()">
    <center><div id="map" style="width: 800px; height: 600px"></div></center>
  </body>
</html>

我可以通过移动第一个“返回标记”下方的花括号来改变我看到的图标html格式,并将其放在第二个“返回标记”之后作为双精度,但无论哪种方式我都有格式问题。如果有人有一些想法,我会非常感激他们。我被卡住了,有几种不同的数据类型,我需要加载并显示在我的地图上。谢谢。

0 个答案:

没有答案