3

我有一个 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;
   ?>
4

1 回答 1

4

网页

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

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 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);

} /script>

$.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 路径。

于 2012-12-05T10:52:36.073 回答