4

I'm developing a web page with a Google Maps API v3. I currently have a functional map and search bar. I need to be able to display the distance from a searched address to the nearest placemark on one of the KML files on the map. How can I do this?

Here is the code for the page:

<script type="text/javascript">
    var geocoder;
    var map; 
    var marker;
    var layers = [];
  function initialize() {
    geocoder = new google.maps.Geocoder ();
    var latlng = new google.maps.LatLng (41, -73.4);
    var myOptions = {
      zoom: 7,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
        }
      map = new google.maps.Map(document.getElementById("map_canvas"),
        myOptions);
      marker = new google.maps.Marker({map:map});


        layers[0] = new google.maps.KmlLayer('http://dl.dropbox.com/u/80233620/South-and-North-County-Trailway.kml',
            {preserveViewport: true});
        layers[1] = new google.maps.KmlLayer('http://www.nyc.gov/html/dot/downloads/misc/cityracks.kml', 
            {preserveViewport: true});
        layers[2] = new google.maps.KmlLayer('http://dl.dropbox.com/u/80233620/NWS%20Radar%20Images.kmz', 
            {preserveViewport: true});
    for (var i = 0; i < layers.length; i++) {
                layers[i].setMap(map);
              }
        }

    function codeAddress () {
        var address = document.getElementById ("address").value;
        geocoder.geocode ( { 'address': address}, function(results, status)  {
        if (status == google.maps.GeocoderStatus.OK)  {
            map.setCenter(results [0].geometry.location);
            marker.setPosition(results [0].geometry.location);
            map.setZoom(14);
            } 
        else {
            alert("Geocode was not successful for the following reason: " + status);
                }
        }); 
                            }
    function toggleLayer(i) {
      if(layers[i].getMap() === null) {
        layers[i].setMap(map);
      }
      else {
        layers[i].setMap(null);}
    }

</script>
Galen
  • 29,976
  • 9
  • 71
  • 89
Gavin
  • 141
  • 5
  • 12

1 Answers1

1

You cannot access the data in KML layers like that

https://developers.google.com/maps/documentation/javascript/layers#KMLLayers

Because KML may include a large number of features, you may not access feature data from the KmlLayer object directly. Instead, as features are displayed, they are rendered to look like clickable Maps API overlays.

Instead you can process the XML and add markers manually, then use the geometry library and computeDistanceBetween() to get the distance. I usually multiply the distance by some number to account for turns (The distance formula gets a straight line distance). I believe around 1.2 was the most accurate.

Galen
  • 29,976
  • 9
  • 71
  • 89
  • What do you mean by processing the XML and adding markers manually? – Gavin May 30 '12 at 21:28
  • you have to open the xml file with php and extract the points out and add the markers. Google maps doesn't allow you to access that data via a kml layer. – Galen May 30 '12 at 22:14
  • I'm not familiar with php, how do I open the file with it? – Gavin May 30 '12 at 22:18
  • Galen, I found another question related to this, and your answer to it seemed to work for other users: http://stackoverflow.com/questions/4057665/google-maps-api-v3-find-nearest-markers/4060721#4060721. Can I use this same code in mine? if so, how do you display the calculated distance? – Gavin May 30 '12 at 22:23
  • 1
    That wont work because you dont have markers on your map you have a kml layer. Extracting the markers out of the kml layer is out of the scope of these comments. – Galen May 30 '12 at 22:47
  • do you recommend somewhere or someone that can help me extract the markers? – Gavin May 30 '12 at 23:02
  • Look up how to parse xml with php – Galen May 31 '12 at 00:28