douchixu3686 2015-02-19 05:54
浏览 30

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

I am trying to plot addresses from my db via xml format. I have got a PHP file that plot these addresses in xml format, and I would like to display them into google map as markers.

generatexml.php working fine, it display all addresses as xml format but I can't figure out how to plot these addresses into map as marker.

Result of generatexml.php

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

Here's my php file that generate xml file.

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

Any idea or examples ?

展开全部

  • 写回答

0条回答 默认 最新

      编辑
      预览

      报告相同问题?

      手机看
      程序员都在用的中文IT技术交流社区

      程序员都在用的中文IT技术交流社区

      专业的中文 IT 技术社区,与千万技术人共成长

      专业的中文 IT 技术社区,与千万技术人共成长

      关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

      关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

      客服 返回
      顶部