2012-05-09 4 views
0

Je souhaite rechercher un fichier KML pour voir si une adresse particulière se trouve dans une superposition. Actuellement, j'ai l'adresse de conversion en un géocodage. Cependant, je ne suis pas sûr du code nécessaire pour ajouter cette fonctionnalité.API Google Maps V3 Recherche Fichier KML

Voici le code actuel:

function initialize() { 
    var infowindow = new google.maps.InfoWindow({ 
     content: '', 
     suppressMapPan:true 
    }); 

    var myLatlng = new google.maps.LatLng(35.910200,-84.085100); 
     var myOptions = { 
      zoom: 12, 
      center: myLatlng, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }; 

    var map = new google.maps.Map(
     document.getElementById("map_canvas"), myOptions); 

    var d = new Date();  
    var n = d.getMilliseconds(); 
    var nyLayer = new google.maps.KmlLayer(
     'http://www.cspc.net/neighborhoods/groups.kml?rand=' + n, 
     { suppressInfoWindows: true, map: map}); 

    google.maps.event.addListener(nyLayer, 'click', function(kmlEvent) {  
     var url = kmlEvent.featureData.snippet; 
     var groupName = kmlEvent.featureData.name; 
     var insideContent = "<div style='width:250px;'><h2>" + groupName + 
      "</h1><p>We have a neighborhood contact in your area! </p>" + 
      "<p><a href='" + url + "' target='_blank'>Get connected!</a>" + 
      " They look forward to hearing from you.</p><p>If you have " + 
      "any additional questions, please contact our " + 
      "<a href='http://www.cspc.net/communitylife' target='_blank'>" + 
      "Community Life</a> staff for more information. Betsy Palk, " + 
      "the Administrative Assistant, may be reached at:<br/><br/>" + 
      "<b>Email:</b> <a href='mailto:[email protected]'>" + 
      "[email protected]</a><br/><b>Phone:</b> 865-291-5268<p></div>"; 

     var clickPos = kmlEvent.latLng; 
     var posX = new google.maps.LatLng(clickPos.lat(), clickPos.lng()); 
     infowindow.close(); 
     infowindow.setPosition(posX); 
     infowindow.setContent(insideContent); 
     infowindow.open(map); 
    }); 

    eventMapClick = google.maps.event.addListener(map, 'click', 
     function(event) { 
      var marker = new google.maps.Marker({ position: event.latLng }); 
      var outsideContent = "<div style='width:250px;'><h2>Oops!</h1>" + 
      "<p> It seems we don't have a neighborhood contact in your " + 
      "area.</p><p>Please contact our <a " + 
      "href='http://www.cspc.net/communitylife' target= '_blank'>" + 
      "Community Life</a> staff for more information. " + 
      "Betsy Palk, the Administrative Assistant, may be reached at:" + 
      "<br/><br/><b>Email: </b> <a href='mailto:[email protected]'>" + 
      "[email protected]</a><br/><b>Phone:</b> 865-291-5268<p></div>"; 

      infowindow.setContent(outsideContent); 
      infowindow.open(map, marker); 
     }); 
    } 

    var geocoder = new google.maps.Geocoder(); 

    function searchAddress(address) { 
     geocoder.geocode(
      {'address': address}, 
      function(results, status) { 
       if (status == google.maps.GeocoderStatus.OK) { 
        var loc = results[0].geometry.location; 
        // use loc.lat(), loc.lng() 
        window.alert(loc); 
       } 
       else { 
        window.alert("Not found: " + status); 
       } 
      } 
     ); 
    }; 

Répondre

0

Si je comprends bien votre question, je crois que vous voulez la formule pour déterminer si un point (google.maps.LatLng) se situe dans l'un de vos définitions KML Placemark (que vous donnez des noms tels comme: Neighborhood Group 1). Dans chaque Placemark, vous définissez un Polygon et dans chaque Polygon, vous définissez un ensemble de coordinates, représentant les sommets du Polygon.

Utilisation du coordinates dans le Polygon et la LatLng vous récupérez via géocodage, vous pourriez commencer par ces formules et sélectionnez ce qui est la meilleure solution pour vous:

Une question très similaire a également été posée sur SO: Determine the lat lngs of markers within a polygon.