2013-05-03 73 views
1

我很難讓我的標記與Google Maps V3 API一起顯示。Google Maps API V3標記不能從XML工作

我得到下面的錯誤,當我加載頁面 -

類型錯誤:XML是空 VAR的標記= xml.documentElement.getElementsByTagName( 「標記」);

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

phpsqlajax_genxml.php

<?php 
$username="root"; 
$password=""; 
$database="cake"; 
    // Start XML file, create parent node 
    $doc = domxml_new_doc("1.0"); 
    $node = $doc->create_element("markers"); 
    $parnode = $doc->append_child($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 markers"; 
    $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 = $doc->create_element("marker"); 
    $newnode = $parnode->append_child($node); 

    $newnode->set_attribute("name", $row['name']); 
    $newnode->set_attribute("address", $row['address']); 
    $newnode->set_attribute("lat", $row['lat']); 
    $newnode->set_attribute("lng", $row['lng']); 
    $newnode->set_attribute("type", $row['type']); 
    } 

    $xmlfile = $doc->dump_mem(); 
    echo $xmlfile; 

    ?> 
+0

alert(data.responseXML)給了什麼? – karthikr 2013-05-03 21:32:13

+0

對不起karthikr請參閱我的錯誤。我一再編輯 – 2013-05-03 21:34:28

+0

它給空 – 2013-05-03 21:36:00

回答

0

phpsqlajax_genxml.php正在產生無效的XML(不能告訴從PHP爲什麼)。將瀏覽器指向它,它會通知您錯誤或無效的XML。