简体   繁体   English

使用PHP / MySQL为Google Maps上的标记形成XML文件

[英]Using PHP/MySQL to form a XML file for markers on Google Maps

I'm trying to put markers on my google map canvas by fetching the info out of the database, I followed every step from https://developers.google.com/maps/articles/phpsqlajax_v3 though somehow it doesnt work (probably because I changed a few things, though I cant find what brings the troubles up.) 我试图通过从数据库中获取信息来在我的Google Map画布上放置标记,我遵循了https://developers.google.com/maps/articles/phpsqlajax_v3的每个步骤,尽管有时不起作用(可能是因为我改变了几件事,尽管我找不到造成麻烦的原因。)

I already managed to fetch the data by using a phptoxml.php file the dummy output proves that It's correct I guess: 我已经设法通过使用phptoxml.php文件来获取数据,虚拟输出证明这是正确的,我猜:

<users>
<user id="1" name="John Tucker" age="19" lat="47.608940" lng="-122.340141" type="0"/>
<user id="2" name="Dean Jester" age="21" lat="51.219040" lng="4.326590" type="0"/>
<user id="3" name="Joris Nisteven" age="26" lat="51.203671" lng="4.341480" type="0"/>
<user id="4" name="Joske Vermeulen" age="20" lat="51.204155" lng="4.327018" type="0"/>
<user id="5" name="Timmy den Beir" age="21" lat="51.209263" lng="4.339720" type="0"/>
<user id="6" name="Ben von Duppen" age="23" lat="51.168308" lng="4.394287" type="0"/>
</users>

now the code that I use to fetch the data and put it in variables: 现在,我用来获取数据并将其放入变量的代码:

downloadUrl("phptoxml.php", function(data) {
        var xml = data.responseXML;
        var users = xml.documentElement.getElementsByTagName("user");
        for (var i = 0; i < users.length; i++) {
            var id = parseInt(users[i].getAttribute("id"));
            var name = users[i].getAttribute("name");
            var age = parseInt(users[i].getAttribute("age"));
            var lat = parseFloat(users[i].getAttribute("lat"));
            var lng = parseFloat(users[i].getAttribute("lng"));
            var marker = add_marker(lat,lng,id,name,age); // pass in as Latitude, then Longitude
            fluster.addMarker(marker);
        }
    });

where fluster is used for cluster management and the function add_marker worked well before, so I doubt that that could be the problem. 在使用fluster进行集群管理的地方,函数add_marker之前运行良好,因此我怀疑这可能是问题所在。

the function downloadUrl is defined as followed (though this part of the code is pretty vague for me) 函数downloadUrl的定义如下(尽管这段代码对我来说很模糊)

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

I noticed that if I put an alert behind request.send(null); 我注意到,如果我在request.send(null)后面放置了警报; with debug info, it doesnt pop up. 与调试信息,它不会弹出。 same for any alert that comes after the downloadUrl call.. 对于downloadUrl调用后出现的任何警报也是如此。

I have no clue, hope someone might help :) 我不知道,希望有人可以帮忙:)

Ty in advance 提前输入

for more info i include the entire code: 有关更多信息,我包括整个代码:

 <script type="text/javascript">



var map; var fluster; var infoBubble;
  function initialize() {
    var latlng = new google.maps.LatLng(27.059125784374068,-37.6171875);
    var myOptions = {
    zoom: 3,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    mapTypeControl: false,
    streetViewControl: false
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
fluster = new Fluster2(map);


// Change this depending on the name of your PHP file
downloadUrl("phptoxml.php", function(data) {
    var xml = data.responseXML;
    var users = xml.documentElement.getElementsByTagName("user");
    for (var i = 0; i < users.length; i++) {
        var id = parseInt(users[i].getAttribute("id"));
        var name = users[i].getAttribute("name");
        var age = parseInt(users[i].getAttribute("age"));
        var lat = parseFloat(users[i].getAttribute("lat"));
        var lng = parseFloat(users[i].getAttribute("lng"));
        var marker = add_marker(lat,lng,id,name,age); // pass in as Latitude, then Longitude
        fluster.addMarker(marker);
    }
});

var input = document.getElementById('searchTextField');
var autocomplete = new google.maps.places.Autocomplete(input);
autocomplete.bindTo('bounds', map);
google.maps.event.addListener(autocomplete, 'place_changed', function() {
    var place = autocomplete.getPlace();
    if (place.geometry.viewport) {
        map.fitBounds(place.geometry.viewport);
    } else {
        map.setCenter(place.geometry.location);
        map.setZoom(17);  // Why 17? Because it looks good.
    }

    var address = '';
    if (place.address_components) {
        address = [(place.address_components[0] &&
                    place.address_components[0].short_name || ''),
                   (place.address_components[1] &&
                    place.address_components[1].short_name || ''),
                   (place.address_components[2] &&
                    place.address_components[2].short_name || '')
                  ].join(' ');
    }
});

// Initialize Fluster
// This will set event handlers on the map and calculate clusters the first time.
fluster.initialize();   



 }




 function add_marker(lat,lng,id,name,age) {

var marker = new google.maps.Marker({
      position: new google.maps.LatLng(lat,lng),
      map: map,
      icon: 'suiticon.png',
      title: name
});
    var infoBubble = new InfoBubble({
      maxWidth: 300,
      backgroundColor: '#dedddd',
      borderWidth: 2,
      borderColor: 'rgb(68, 68, 68)'
    });

    var contentString = '<div id="content">'+
        /*'<h2>'+name+'</h2>'+
        '<p><span class="myLabel" style="margin-right:10px">'+age+'</span>21</p>'+
        '<p><center><img src="'+id+'.jpg" class="bro_image"></center> </p>'+
        '<p><button class="button_inverse" style="padding-left: 23px; padding-right:23px; margin-left:31px;" href="#">Rate this Bro!</button></p>'+
        */'</div>';

    var div = document.createElement('DIV');
    div.innerHTML = 'No pictures uploaded by this user.';

    infoBubble.addTab('Personal', contentString);
    infoBubble.addTab('Pictures', div);

    google.maps.event.addListener(marker, 'click', function() {
      if (!infoBubble.isOpen()) {
        infoBubble.open(map, marker);
      }
    }); 

return marker;


}


function addTab() {
    var title = document.getElementById('tab-title').value;
    var content = document.getElementById('tab-content').value;

    if (title != '' && content != '') {
      infoBubble.addTab(title, content);
    }
}

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

}
</script>

You will need ProjectionOverlay so use Fluster2.packed.js instead of Fluster2.js. 您将需要ProjectionOverlay,因此请使用Fluster2.packed.js而不是Fluster2.js。 This was the reason you didn't get the alert message! 这就是您没有收到警报消息的原因!

'map' isn't defined inside add_marker function so you should you should import that too! 在add_marker函数中未定义“地图”,因此您也应该将其导入!

The following code should work! 下面的代码应该工作!

<!DOCTYPE html >
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>PHP/MySQL Example</title>
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?
libraries=places&sensor=false"></script>
    <script type="text/javascript" src="Fluster2.packed.js"></script>
<script type="text/javascript" src="http://google-maps-utility-library-v3.googlecode.com/svn/trunk/infobubble/src/infobubble.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'
      }
    };

var infoBubble;
  function initialize() {
    var latlng = new google.maps.LatLng(27.059125784374068,-37.6171875);
    var myOptions = {
    zoom: 3,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    mapTypeControl: false,
    streetViewControl: false
};
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var fluster = new Fluster2(map);

      // Change this depending on the name of your PHP file
      downloadUrl("phpsqlajax_genxml3.php", function(data) {
        var xml = data.responseXML;
        var users = xml.documentElement.getElementsByTagName("user");
        for (var i = 0; i < users.length; i++) {
          var id = parseInt(users[i].getAttribute("id"));
          var name = users[i].getAttribute("name");
          var age = users[i].getAttribute("age");
          var type = users[i].getAttribute("type");
          var lat = parseFloat(users[i].getAttribute("lat"));
          var lng = parseFloat(users[i].getAttribute("lng"));
          var content = add_content(lat,lng,id,name,age,type,map); // pass in as Latitude, then Longitude
          fluster.addMarker(content);
        }
      });

var input = document.getElementById('searchTextField');
var autocomplete = new google.maps.places.Autocomplete(input);
autocomplete.bindTo('bounds', map);
google.maps.event.addListener(autocomplete, 'place_changed', function() {
    var place = autocomplete.getPlace();
    if (place.geometry.viewport) {
        map.fitBounds(place.geometry.viewport);
    } else {
        map.setCenter(place.geometry.location);
        map.setZoom(17);  // Why 17? Because it looks good.
    }

    var address = '';
    if (place.address_components) {
        address = [(place.address_components[0] &&
                    place.address_components[0].short_name || ''),
                   (place.address_components[1] &&
                    place.address_components[1].short_name || ''),
                   (place.address_components[2] &&
                    place.address_components[2].short_name || '')
                  ].join(' ');
    }
});

// Initialize Fluster
// This will set event handlers on the map and calculate clusters the first time.
    fluster.initialize();   
    }

 function add_content(lat,lng,id,name,age,type,map) {
 var icon = customIcons[type] || {};
 var marker = new google.maps.Marker({
                    position: new google.maps.LatLng(lat,lng),
                    map: map,
                    icon: icon.icon,
                    title: name
                    });

    var contentString = '<div id="content">'+
        /*'<h2>'+name+'</h2>'+
        '<p><span class="myLabel" style="margin-right:10px">'+age+'</span>21</p>'+
        '<p><center><img src="'+id+'.jpg" class="bro_image"></center> </p>'+
        '<p><button class="button_inverse" style="padding-left: 23px; padding-right:23px; margin-left:31px;" href="#">Rate this Bro!</button></p>'+
        */'</div>';

   var infoBubble = new InfoBubble({
      maxWidth: 300,
      backgroundColor: '#dedddd',
      borderWidth: 2,
      borderColor: 'rgb(68, 68, 68)'
    });

    infoBubble.open(map, marker);

    var div = document.createElement('DIV');
    div.innerHTML = 'No pictures uploaded by this user.';

    infoBubble.addTab('Pictures', div);
    infoBubble.addTab('Personal', contentString);

    google.maps.event.addListener(marker, 'click', function() {
      if (!infoBubble.isOpen()) {
        infoBubble.open(map, marker);
      }
    });

return 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);
      alert(url);
      request.send(null);
    }
function doNothing() {}

  </script>

  </head>
  <body onload="initialize()">
    <div id="map_canvas" style="width: 800px; height: 550px"></div>
  </body>

</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM