2015-09-15 46 views
1
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/> 
    <title>Google Maps AJAX + mySQL/PHP</title> 
    <script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> 
    <script type="text/javascript"> 

    var customIcons = { 
     restaurant: { 
     icon: 'http://labs.google.com/ridefinder/images/mm_20_blue.png', 
     shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png' 
     }, 
     bar: { 
     icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png', 
     shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png' 
     } 
    }; 
    function load() { 
     var map = new google.maps.Map(document.getElementById("map"), { 
     center: new google.maps.LatLng(47.6145, -122.3418), 
     zoom: 13, 
     mapTypeId: 'roadmap' 
     }); 
     var infoWindow = new google.maps.InfoWindow; 
     // Change this depending on the name of your PHP file 
     downloadUrl("maps_xml.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 = customIcons[type] || {}; 
      var marker = new google.maps.Marker({ 
      map: map, 
      position: point, 
      icon: icon.icon, 
      shadow: icon.shadow 
      }); 
      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 id="map" style="width: 100%; height: 100%"></div> 
    </body> 
</html>1 

maps_xml.php 上創建的GoogleMap標記這是我phpfile其從數據庫中獲得的數據和動態創建標記。 但是這樣做我是geeting地圖,但我能夠在地圖上得到任何標記,即使插入了7個數據也是如此。 如此善舉幫助我解決這個問題。如何使用數據庫和PHP

<?php 
require("config.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('&','&amp;', $row['name']) . '" '; 
    echo 'address="' . parseToXML($row['address']) . '" '; 
    echo 'lat="' . $row['lat'] . '" '; 
    echo 'lng="' . $row['lng'] . '" '; 
    echo 'type="' . $row['type'] . '" '; 
    echo '/>'; 
} 
// End XML file 
echo '</markers>'; 
?> 
+0

我會首先驗證'downloadUrl()'的'data'。你是否獲得了預期的結果? – Twisty

+0

另外,請考慮使用[MySQL PDO](http://php.net/manual/en/ref.pdo-mysql.php)或[MySQLi](http://php.net/manual/en/book.mysqli .php)連接並從數據庫中檢索。暫且不推薦使用,現在在PHP 7中刪除了mysql_ *擴展。並考慮使用[htmlentities()](http://www.w3schools.com/php/func_string_htmlentities.asp)而不是自己的用戶定義函數'parseToXML ()'。 – Parfait

回答

0

嘗試,增加了小的變化如下:

// Select all the rows in the markers table 
$query = "SELECT * FROM markers"; 
$results = mysql_query($query); 
if (!$results) { 
    die('Invalid query: ' . mysql_error()); 
} 
header("Content-type: text/xml"); 
// Start XML file, echo parent node 
echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"; 
echo '<markers>' . "\r\n"; 
// Iterate through the rows, printing XML nodes for each 
while ($row = mysql_fetch_assoc($results)){ 
    // ADD TO XML DOCUMENT NODE 
    echo "\t<marker "; 
    echo 'name="' . parseToXML('&','&amp;', $row['name']) . '" '; 
    echo 'address="' . parseToXML($row['address']) . '" '; 
    echo 'lat="' . $row['lat'] . '" '; 
    echo 'lng="' . $row['lng'] . '" '; 
    echo 'type="' . $row['type'] . '" '; 
    echo "/>\r\n"; 
} 
// End XML file 
echo "</markers>\r\n"; 

這將增加在線路的正常結束和縮進是正確的XML格式。 JS不會閱讀它們,但這是很好的做法。然後,您也可以直接在瀏覽器中調用頁面來測試結果,如果XML是正確的,那麼您需要排除JS代碼的故障並瞭解它爲什麼不生成標記。檢查您的瀏覽器控制檯(或使用FireBug之類的工具)來查找錯誤。

EDIT

研究發現,<div id="map" style="width: 100%; height: 100%"></div>似乎是問題的一部分。建議更改爲像素值:<div id="map" style="width: 1024px; height: 680px;"></div>

您還沒有設置或更新您的標誌器陣列,在load():在downloadUrl()再次

map.markers = map.markers || [];

和:

map.markers.push(marker);

工作例如:http://jsfiddle.net/Twisty/k5nnsqcL/

+0

我還是得到了同樣的東西 – user3419125

+0

你能發表你的XML數據的例子嗎? – Twisty

+0

泛非市場,「華盛頓州西雅圖1521第一大街1521號」,47.608941,-122.340145,餐廳 「泰國佛羅里達酒吧」,西華盛頓2222第二大道2222號,47.613591,-122.344394,酒吧 The Melting Pot,「14 Mercer St,Seattle,WA「,47.624562,-122.356442,餐廳 Ipanema Grill,」1225 1st Ave,Seattle,WA「,47.606366,-122.337656,餐廳 – user3419125