【问题标题】:Google map is not displayed using php使用php不显示谷歌地图
【发布时间】:2012-12-05 09:52:41
【问题描述】:

我有一个 php 代码,它从数据库(测试)和表名 manu 获取位置信息,它是使用 wamp 中的 phpmyadmin 创建的,并使用标记在地图上显示这些位置。感知纬度和经度值中的位置详细信息。

更新

<?
$dbname            ='test'; //Name of the database
$dbuser            =''; //Username for the db
$dbpass            =''; //Password for the db
$dbserver          ='localhost'; //Name of the mysql server

$dbcnx = mysql_connect ("$dbserver", "$dbuser", "$dbpass");
mysql_select_db("$dbname") or die(mysql_error());
 ?>


 <html>
 <head>
 <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
 <style type="text/css">
  body { font: normal 10pt Helvetica, Arial; }
 #map { width: 350px; height: 300px; border: 0px; padding: 0px; }
 </style>
 <script src="http://maps.google.com/maps/api/js?v=3&sensor=false"        type="text/javascript" ></script>
 <script type="text/javascript" src="js/jquery.js"></script>
 <script type="text/javascript">

  var icon = new google.maps.MarkerImage("http://maps.google.com/mapfiles/ms/micons/blue.png",
  new google.maps.Size(32, 32), new google.maps.Point(0, 0),
  new google.maps.Point(16, 32));
  var center = null;
  var map = null;
  var currentPopup;
  var bounds = new google.maps.LatLngBounds();
  function addMarker(lat, lng, info) {
  var pt = new google.maps.LatLng(lat, lng);
  bounds.extend(pt);
  var marker = new google.maps.Marker({
  position: pt,
  icon: icon,
  map: map
  });
  var popup = new google.maps.InfoWindow({
  content: info,
  maxWidth: 300
  });

   google.maps.event.addListener(marker, "click", function() {
 if (currentPopup != null) {
   currentPopup.close();
  currentPopup = null;
  }
   popup.open(map, marker);
  currentPopup = popup;
 });
 google.maps.event.addListener(popup, "closeclick", function() {
 map.panTo(center);
  currentPopup = null;
 });
 }
 function initMap() {
 map = new google.maps.Map(document.getElementById("map"), {
 center: new google.maps.LatLng(0, 0),
  zoom: 14,
  mapTypeId: google.maps.MapTypeId.ROADMAP,
  mapTypeControl: false,
  mapTypeControlOptions: {
  style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR
  },
  navigationControl: true,
  navigationControlOptions: {
  style: google.maps.NavigationControlStyle.SMALL
  }
   });


 $.getJSON('googlescript.php', function(items)
 {
 for (var i = 0; i < items.length; i++) 
 {
 (function(item) 
 {
 addMarker(item.lat, item.long, item.name + ' ' + item.desc);
 })(items[i]);
 }

 });


 center = bounds.getCenter();
  map.fitBounds(bounds);
 }

  </script>
  </head>
  <body onload="initMap()" style="margin:0px; border:0px; padding:0px;">
   <div id="map"></div>
  </body>
   </html>

googlescript.php 文件代码:

   <?php

   $dbname            ='test'; //Name of the database
   $dbuser            =''; //Username for the db
   $dbpass            =''; //Password for the db
   $dbserver          ='localhost'; //Name of the mysql server
   $tbl_name          ='manu';

   $dbcnx = mysql_connect ("$dbserver", "$dbuser", "$dbpass");
   mysql_select_db("$dbname") or die(mysql_error());

   $query = mysql_query("SELECT * FROM manu")or die(mysql_error());

   header('Content-Type: application/json');

   $rows = array();
   while ($row = mysql_fetch_array($query))
   {
    $rows[] = $row;
   }
  echo json_encode($rows);
  exit;
   ?>

【问题讨论】:

  • 你检查是否有任何错误?使用 mozila addon firebug 进行检查。
  • [2012 年 12 月 5 日星期三 15:14:39] [错误] [客户端 127.0.0.1] 文件不存在:C:/wamp/www/favicon.ico [12 月 5 日星期三 15:29 :03 2012] [错误] [客户端 127.0.0.1] 文件不存在:C:/wamp/www/favicon.ico [2012 年 12 月 5 日星期三 15:29:03] [错误] [客户端 127.0.0.1] 文件不存在不存在:C:/wamp/www/favicon.ico 这是我在 apache 错误日志中得到的内容
  • 尝试签入firebug..我之前也遇到过同样的问题....即使在其他代码中缺少任何文件或错误,谷歌地图也不会被加载,所以如果没有,请尝试查找错误这里有错误..
  • 好的,你一定有firefox mozila吧。?好的,现在搜索firebug并将其安装在firefox mozila中..然后检查控制台中是否显示错误...别担心你会看到bug图标,只需点击它就会出现firebug窗口,只需点击控制台并启用它..do然后它告诉我们萤火虫显示什么错误..
  • 解决你的问题,一旦完成,我就会把它贴在这里,你需要添加一些小代码......等一会儿兄弟。

标签: php javascript google-maps wamp


【解决方案1】:

HTML页面

body onload="initMap()" //you forget to close it.
div id="map"

在 html 文件中进行了以下更改。

script src="http://maps.google.com/maps/api/js?v=3&sensor=false"    type="text/javascript"
script type="text/javascript" src="js/jquery.js"
script type="text/javascript">
    var icon = new google.maps.MarkerImage("http://maps.google.com/mapfiles/ms/micons/blue.png",
    new google.maps.Size(32, 32), new google.maps.Point(0, 0),
    new google.maps.Point(16, 32));
    var center = null;
    var map = null;
    var currentPopup;
    var bounds = new google.maps.LatLngBounds();
    function addMarker(lat, lng, info)
    {
        var pt = new google.maps.LatLng(lat, lng);
        bounds.extend(pt);
        var marker = new google.maps.Marker(
        {
            position: pt,
            icon: icon,
            map: map
        });
        var popup = new google.maps.InfoWindow(
        {
            content: info,
            maxWidth: 300
        });

    google.maps.event.addListener(marker, "click", function()
    {
        if (currentPopup != null)
        {
            currentPopup.close();
            currentPopup = null;
        }
        popup.open(map, marker);
        currentPopup = popup;
    });
    google.maps.event.addListener(popup, "closeclick", function()
    {
        map.panTo(center);
        currentPopup = null;
    });
}

函数 initMap() { 地图 = 新 google.maps.Map(document.getElementById("map"), { 中心:新的 google.maps.LatLng(0, 0), 缩放:14, mapTypeId:google.maps.MapTypeId.ROADMAP, 地图类型控制:假, 地图类型控制选项: { 风格:google.maps.MapTypeControlStyle.HORIZONTAL_BAR }, 导航控制:真, 导航控制选项: { 风格:google.maps.NavigationControlStyle.SMALL } });

// getjson code goes here which is as below because i was unable to format it here so i wrote it downward.

center = bounds.getCenter();
map.fitBounds(bounds);

} /脚本>

$.getJSON('googlescript.php', function(items)
{
for (var i = 0; i < items.length; i++)
{
(function(item)
{
addMarker(item.lat, item.long, item.name + ' ' + item.desc);
})(items[i]);
}

});

我使用了$.getJSON('googlescript.php',,因此您需要创建一个googlescript.php 文件并将以下代码粘贴到其中。

$query = mysql_query("SELECT * FROM manu")or die(mysql_error());

header('Content-Type: application/json');

$rows = array();
while ($row = mysql_fetch_array($query))
{
    $rows[] = $row;
}
echo json_encode($rows);
exit;

?>

现在检查一下,我相信你会得到你想要的结果。 不要忘记更改jquery路径和更改数据库连接设置,请正确放置jquery路径。

【讨论】:

  • 谢谢让我试试,如果可行,我会接受你的回答。再次感谢
  • 嘿,请记住变量你使用了 lon,因为我使用 long 作为经度,所以在 getJson 中相应地更改它.. 嘿,你总是受欢迎的。
  • 抱歉给您带来不便,我无法格式化 GetJson 代码,所以我通过替换 initMap 函数中的注释来编写 js 的缺点 put getjson 代码...你试过了吗..它会起作用,因为我明白了使用信息窗口内容运行。
  • “更改 jquery 路径并更改数据库连接”。这是什么意思?
  • 它的意思是我已经使用 jquery javascript lib 来完成这个任务......并且数据库连接更改意味着更改 localhost 和 root 以及数据库名称等......你以前使用过 jquery 吗?你知道jquery吗?如果没有,那么现在就试试它非常强大的 js 库。
猜你喜欢
  • 1970-01-01
  • 2010-12-28
  • 1970-01-01
相关资源
最近更新 更多