0

我想搜索 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);
                }
            }
        );
    };
4

1 回答 1

0

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

使用您通过地理编码检索coordinates的insidePolygon和 the LatLng,您可以从这些公式开始并选择最适合您的公式:

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

于 2012-05-09T15:28:44.783 回答