如何在WordPress中使用Google Maps SQL->XML->地图标记教程

时间:2013-01-23 作者:Kelly Mann

几天来,我一直在试图找出我的代码出了什么问题,但我似乎无法破解它。我正在努力跟随this tutorial

我尝试了所有建议的三种方法从sql server输出XML,但我只能使用echo使其工作。

我遇到的问题是我一直在犯错误

“TypeError:xml为空”

这发生在var markers = xml.documentElement.getElementsByTagName("marker"); 在下面发布的代码中。

我不知道如何继续。如果有任何问题我可以问你们,请告诉我。Here 是代码尝试运行的网页

代码如下:

(标题部分(编辑为相关代码))

<?php
        <!-- Google Maps Scrips -->
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />

    <script type="text/javascript"
      src="http://maps.googleapis.com/maps/api/js?key=AIzaSyA-ZWuoso96viUofF_enb79SiN7QwYIh1o&sensor=false">
    </script>
    <script type="text/javascript">

        //Variables used in maps
        var geocoder;
        var map;
        var markersArray = [];
        var infos = [];

        geocoder = new google.maps.Geocoder();



    function initialize() {
        var mapOptions = {
          center: new google.maps.LatLng(47.6, -122.3),
          zoom: 8,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById("content_map"),
            mapOptions);

        var infoWindow = new google.maps.InfoWindow;

        google.maps.event.addListener(map, \'click\', function(event) {
            placeMarker(event.latLng, map);
        });

      // loads the xml data from marker database
          downloadUrl("http://wp-content/themes/oxygen-child/phpsqlajax_genxml3.php", function(data) {
            var xml = data.responseXML;
            var markers = xml.documentElement.getElementsByTagName("marker");
            for (var i = 0; i < markers.length; i++) {
              var name = markers[i].getAttribute("name");
              var type = markers[i].getAttribute("type");
              var description = markers[i].getAttribute("description");
              var point = new google.maps.LatLng(
                  parseFloat(markers[i].getAttribute("lat")),
                  parseFloat(markers[i].getAttribute("lng")));
              var rank = markers[i].getAttribute("rank");
              var html = "<b>" + name + "</b> <br/>" + description;
              var icon = customIcons[type] || {};
              var marker = new google.maps.Marker({
                map: map,
                position: point,
                icon: icon.icon,
                shadow: icon.shadow
              });
              bindInfoWindow(marker, map, infoWindow, html);
            }
          });



      }

        function downloadUrl(url, callback) {
          var request = window.ActiveXObject ?
              new ActiveXObject(\'Microsoft.XMLHTTP\') :
              new XMLHttpRequest;

          request.onreadystatechange = function() {
            if (request.readyState == 4) {
              request.onreadystatechange = doNothing;
              callback(request, request.status);
            }
          };

          request.open(\'GET\', url, true);
          request.send(null);
        }   

    function doNothing() {}

    //]]>

      function placeMarker(location, map) {
        var marker = new google.maps.Marker({
            position: location, 
            map: map
            });
            map.panTo(position);
      } 




    </script>`
(phpsqlajax\\u genxml3.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("\'",\'&apos;\',$xmlStr); 
$xmlStr=str_replace("&",\'&amp;\',$xmlStr); 
return $xmlStr; 
} 

// Opens a connection to a mySQL server
$connection=mysql_connect (localhost, $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 WHERE 1";
$result = mysql_query($query);
if (!$result) {
  die(\'Invalid query: \' . mysql_error());
}

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 \'type="\' . $row[\'type\'] . \'" \';
  echo \'description="\' . parseToXML($row[\'description\']) . \'" \';
  echo \'lat="\' . $row[\'lat\'] . \'" \';
  echo \'lng="\' . $row[\'lng\'] . \'" \';
  echo \'rank="\' . $row[\'rank\'] . \'" \';

  echo \'/>\';
}

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

?>
`

2 个回复
SO网友:Kelly Mann

所以我找到了问题所在。我对XMLHttpRequest中如何处理URL不够熟悉。我需要改变

downloadUrl("http://wp-content/themes/oxygen-child/phpsqlajax_genxml3.php", function(data) {

downloadUrl("http://www.theworldsflgiht.com/wp-content/themes/oxygen-child/phpsqlajax_genxml3.php", function(data) {
谢谢大家的帮助!

SO网友:Ritesh

WordPress的最佳实践,在您更改主机时也可以使用。

downloadUrl("<?php echo get_template_directory_uri(); ?>/phpsqlajax_genxml3.php", function(data) {

结束

相关推荐

清理/过滤通过XML RPC插入到发布内容的HTML

一个使用XML RPC在我的网站上发布一些帖子的外部服务将一些不需要的CSS样式和HTML添加到帖子内容中。我想使用基于DOMDocument和DOMXPath构建的HTML清理器来清理它。我唯一的问题是,如何在post\\u内容实际保存到数据库之前访问它,以便在其上运行我的清理程序?也许我可以使用一个过滤器/操作?