2012-05-09 102 views
0

我想要搜索KML文件以查看某個特定地址是否屬於疊加層。目前,我已將地址轉換爲地理編碼。但是,我不確定添加此功能需要什麼代碼。Google Maps API V3搜索KML文件

下面是本次代碼:

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

回答

0

如果我明白你的問題,我相信你想要的公式來確定一個點(google.maps.LatLng)落在你的KML Placemark定義一個內(即你給這樣的名字如:Neighborhood Group 1)。在每個Placemark內,您定義了一個Polygon,並在每個Polygon內定義了一組代表Polygon的頂點的一組coordinates

使用Polygon和您通過地理編碼檢索LatLngcoordinates,您可以用這些公式開始,並選擇一個是你最合適的:

在SO:Determine the lat lngs of markers within a polygon上也有一個非常類似的問題。