谷歌地图 API V3 搜索 KML 文件

Google Maps API V3 Search KML File

本文关键字:KML 文件 搜索 V3 API 谷歌地图      更新时间:2023-09-26

我想搜索 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:betsypalk@cspc.net'>" +
            "betsypalk@cspc.net</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:betsypalk@cspc.net'>" +
            "betsypalk@cspc.net</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);
                }
            }
        );
    };

如果我理解你的问题,我相信你希望公式来确定一个点(google.maps.LatLng)是否属于你的KML Placemark定义之一(你给出的名称如下:Neighborhood Group 1)。在每个Placemark中,你定义一个Polygon,在每个Polygon中,你定义一组coordinates,它们表示Polygon的顶点。

使用Polygon中的coordinates以及通过地理编码检索到的LatLng,您可以从以下公式开始,然后选择最适合您的公式:

  • 快速绕组数在多边形中包含点
  • 多边形中点算法

在SO上也提出了一个非常相似的问题:确定多边形内标记的纬度LNG。