0

我试图通过从数据库中获取信息来在我的谷歌地图画布上放置标记,我遵循了https://developers.google.com/maps/articles/phpsqlajax_v3的每一步, 尽管它不工作(可能是因为我改变了一些东西,虽然我找不到带来麻烦的原因。)

我已经设法通过使用 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>

现在我用来获取数据并将其放入变量的代码:

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

其中fluster用于集群管理并且函数add_marker之前运行良好,所以我怀疑这可能是问题所在。

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

我注意到,如果我在 request.send(null); 后面放一个警报;使用调试信息,它不会弹出。对于 downloadUrl 调用之后出现的任何警报都是一样的。

我不知道,希望有人可以帮助:)

提前TY

有关更多信息,我包括整个代码:

 <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>
4

1 回答 1

1

您将需要 ProjectionOverlay,因此请使用 Fluster2.packed.js 而不是 Fluster2.js。这就是您没有收到警报消息的原因!

'map' 没有在 add_marker 函数中定义,所以你也应该导入它!

以下代码应该可以工作!

<!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>
于 2012-07-22T23:34:49.037 回答