views:

72

answers:

0

Hi Guys - I am developing a map for a client which uses AJAX and PHP/MySQL to plot markers on a Google Map.

I need to be able to add an input field of some sort above the map - the user searches there postcode and the map automatically zooms in to the nearest points depending on the postcode. I have searched and crawled the web for many hours looking for a viable solution.

(I assume that I just need a function to parse the values)

Here is my code that I am using:

       <script type="text/javascript">
    //<![CDATA[

    var iconBlue = new GIcon(); 
    iconBlue.image = 'http://labs.google.com/ridefinder/images/mm_20_blue.png';
    iconBlue.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
    iconBlue.iconSize = new GSize(12, 20);
    iconBlue.shadowSize = new GSize(22, 20);
    iconBlue.iconAnchor = new GPoint(6, 20);
    iconBlue.infoWindowAnchor = new GPoint(5, 1);

    var iconRed = new GIcon(); 
    iconRed.image = 'http://labs.google.com/ridefinder/images/mm_20_red.png';
    iconRed.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
    iconRed.iconSize = new GSize(12, 20);
    iconRed.shadowSize = new GSize(22, 20);
    iconRed.iconAnchor = new GPoint(6, 20);
    iconRed.infoWindowAnchor = new GPoint(5, 1);

    var customIcons = [];
    customIcons["restaurant"] = iconBlue;
    customIcons["bar"] = iconRed;

    function load() {
      if (GBrowserIsCompatible()) {

        var map = new GMap2(document.getElementById("map"));
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        map.setCenter(new GLatLng(55.378051, -3.435973), 6);

        GDownloadUrl("sources/ajax/ajax-maps-xml.php", function(data) {
          var xml = GXml.parse(data);
          var markers = xml.documentElement.getElementsByTagName("marker");
          for (var i = 0; i < markers.length; i++) {
            var id = markers[i].getAttribute("id");
            var name = markers[i].getAttribute("name");
            var address = markers[i].getAttribute("address");
            var type = markers[i].getAttribute("type");
            var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
                                    parseFloat(markers[i].getAttribute("lng")));
            var marker = createMarker(point, name, address, type, id);
            map.addOverlay(marker);
          }
        });
      }
    }

    function createMarker(point, name, address, type, id) {
      var marker = new GMarker(point, customIcons[type]);
      var stripName = name.toLowerCase().replace(/ /gi, '-');
      var html = "<div class='gmap-details'><b>" + name + "</b> <br /><a href='venues/"+id+"/"+stripName+"'>View Full Details &rarr;</a></div>";
      GEvent.addListener(marker, 'click', function() {
        marker.openInfoWindowHtml(html);
      });
      return marker;
    }



    //]]>
  </script>