php - 谷歌地图不显示标记

标签 php mysql google-maps

我直接使用来自 Google 的代码。 https://developers.google.com/maps/articles/phpsqlajax_v3?csw=1

我稍微修改了一下以使用我的数据库。 map 显示在页面上,如果我运行 XML 生成器 - 它会正确显示 XML。

我在这里进行了广泛的搜索,并尝试进行建议的更改 - 但是,我无法让标记显示在 Google map 上?

 <?php

    $state= "CA";
    $country= "US";

    require("phpsqlajax_dbinfo.php");

    // 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());
    }

    $sql1 = "SELECT avg(lat), avg(lng) FROM marker WHERE country='$country' && state='$state'";
    $res1 = mysql_query($sql1); 
    $row1 = mysql_fetch_array($res1);
    $alat = $row1[0];
    $alng = $row1[1];
    ?>
    <!DOCTYPE html >
    <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>PHP/MySQL & Google Maps Example</title>
    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js"></script>
    <script type="text/javascript">
    //<![CDATA[

    var customIcons = {
      free: {
        icon: '/images/blue.png'
      },
      paid: {
        icon: '/images/red.png'
      }
    };

    function load() {
      var map = new google.maps.Map(document.getElementById("map"), {
        center: new google.maps.LatLng(<? echo $alat; ?>, <? echo $alng; ?>),
        zoom: 6,
        mapTypeId: 'roadmap'
      });
      var infoWindow = new google.maps.InfoWindow(); 

      // Change this depending on the name of your PHP file
      downloadUrl("phpsqlajax_genxml.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 address = markers[i].getAttribute("address");
          var type = markers[i].getAttribute("type");
          var point = new google.maps.LatLng(
              parseFloat(markers[i].getAttribute("lat")),
              parseFloat(markers[i].getAttribute("lng")));
          var html = "<b>" + name + "</b> <br/>" + address;
          var icon = eval('customIcons'+'.'+type) || {};
          var marker = new google.maps.Marker({
            map: map,
            position: point,
            icon: icon.icon
          });
          bindInfoWindow(marker, map, infoWindow, html);
        }
      });
    }

    function bindInfoWindow(marker, map, infoWindow, html) {
      google.maps.event.addListener(marker, 'click', function() {
        infoWindow.setContent(html);
        infoWindow.open(map, marker);
      });
    }

    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() {}

    //]]>

    </script>
  </head>
  <body onload="load()">
   <div align="center">
    <div id="map" style="width: 1024px; height: 600px"></div>
  </div>
 </body>
</html>

用于生成 XML 的文件:

<?php

require("phpsqlajax_dbinfo.php");

// Start XML file, create parent node

$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);

// 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 marker WHERE country='$country' && state='$state'";
$result = mysql_query($query);
if (!$result) {
  die('Invalid query: ' . mysql_error());
}

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

// Iterate through the rows, adding XML nodes for each

while ($row = @mysql_fetch_assoc($result)){
  // ADD TO XML DOCUMENT NODE
  $node = $dom->createElement("marker");
  $newnode = $parnode->appendChild($node);
  $newnode->setAttribute("name",$row['name']);
  $newnode->setAttribute("address", $row['address']);
  $newnode->setAttribute("lat", $row['lat']);
  $newnode->setAttribute("lng", $row['lng']);
  $newnode->setAttribute("type", $row['type']);
}

echo $dom->saveXML();

?>

我确实看到了一个解释,但我不明白它的意思。 “文件必须托管在 Web 服务器上,而不是文件系统上。”

我正在尝试在网站上运行它;这些文件不存储在我的计算机上,而是存储在服务器上。

任何额外的帮助将不胜感激。

最佳答案

将您的脚本替换为

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?v=3&sensor=false"></script>

关于php - 谷歌地图不显示标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24816091/

相关文章:

php - 在 Visual Studio Code 中为 PHP 重命名重构

mysql - rails : how to find all users older then n minutes ago where n is stored in joined group

mysql 存储过程正则表达式

javascript - 实现协作实时 map 应用程序

php - stmt->get_result() 未完成

php - 按时间顺序对 MySQL 记录进行排序?

javascript - 如何不让用户直接从 AJAX 使用的浏览器访问 php 文件?

mysql - 查询在终端中运行但不在 PHPMyadmin 中运行

google-maps - 可以在Google静态 map 上画一个圆吗?

angularjs - 如何使用 Angular Google map 在标记列表中的另一个标记上设置特定标记显示