使用PHP创建Google地图标记

All, I've got some locations stored in my database with long and lat coordinates. I'm debating between creating a map for each one of the locations or adding markers for all of them. Any advice would be appreciated on that idea but my issue is that I'm getting these values from my mySQL database. It looks like the only way you can actually add these is through javascript. Do I need to pass in the coordinates to a javascript function to create the markers? How would that look? Any suggestions are appreciated!

You're correct. You have to fetch the data from the database and use something along the line of:

  function initialize() {
    var myLatlng = new google.maps.LatLng(<?php echo $lat; ?>,<?php echo $long; ?>);
    var myOptions = {
      zoom: 4,
      center: myLatlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

    var marker = new google.maps.Marker({
        position: myLatlng, 
        map: map,
        title:"Hello World!"
    });   
  }

In a < script type="text/javascript">-tag on the page. The above is taken from http://code.google.com/apis/maps/documentation/javascript/examples/marker-simple.html, so you'll have to adapt it to your needs.

Yes, you need to pass the long and lat coordinates from your database into Javascript code on the web page in order to create markers for them on the map. If you are using PHP code to generate the page, that means writing PHP code to query the database and then echo() the HTML page with included Javascript code.

zrvan's reply above uses new google.maps.LatLng() to create a single coordinates object and draw a single marker. If you decide to place multiple markers on a single map, I recommend putting the data for the markers into a Javascript array data structure. Then write a Javascript loop to call new google.maps.Marker() for each entry in the array.

Google Maps documentation recommends using an XML document and AJAX calls to deliver the marker data from the database to the web page. See http://code.google.com/apis/maps/articles/phpsqlajax.html .

If you get more than a few hundred markers on a single map, you might want to read the paper, "Too Many Markers!", in the Google Maps API documentation: http://code.google.com/apis/maps/articles/toomanymarkers.html .