通过XML格式的PHP文件加载Google Map Markers

时间:2015-02-19 13:54:20

标签: javascript php xml google-maps google-maps-api-3

我试图通过xml格式从我的数据库中绘制地址。我有一个PHP文件,以xml格式绘制这些地址,我想将它们显示为谷歌地图作为标记。

generatexml.php工作正常,它将所有地址显示为xml格式,但我无法弄清楚如何将这些地址作为标记绘制到地图中。

generatexml.php的结果

<markers>
<marker address="Lichfield Road, Burton on Trent, United Kingdom"/>
<marker address="Anchor BuildingsWestgate, Morecambe, United Kingdom"/>
</markers>

这是我生成xml文件的php文件。

 // file name  generatexml.php
    require ('connection.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=mysqli_connect($host, $username, $password);
    if (!$connection) {  die('Not connected : ' . mysqli_error());}
    // Set the active MySQL database
    $db_selected = mysqli_select_db($connection ,$database );
   if (!$db_selected) {
     die("Database selection failed: " . mysqli_error());
     }
  // Select all the rows in the markers table

   $query = "SELECT * FROM mapping WHERE 1";
   $result = mysqli_query($connection , $query);
   if (!$result) {
       die('Invalid query: ' . mysqli_error());
    }
   header('Content-type: text/xml');
    // Iterate through the rows, adding XML nodes for each
    while ($row = @mysqli_fetch_assoc($result)){
    // ADD TO XML DOCUMENT NODE
        $node = $dom->createElement("marker");
    $newnode = $parnode->appendChild($node);
     $newnode->setAttribute("address", $row['address']);

       }

       echo $dom->saveXML();

       ?>
  <script>
        src="https://maps.googleapis.com/maps/api/jskey=mykey&sensor=false">
  </script>

 function initialise() {
myLatlng = new google.maps.LatLng(54.559323,-3.321304);
mapOptions = {
    zoom: 5,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
  }
geocoder = new google.maps.Geocoder();
infoWindow = new google.maps.InfoWindow;
map = new google.maps.Map(document.getElementById('map-canvas'),         
  mapOptions);

xmlUrl = "inc/generatexml.php";

loadMarkers();

     }
  google.maps.event.addDomListener(window, 'load', initialise);

任何想法或例子?

0 个答案:

没有答案