简体   繁体   中英

Google Maps API V3 Search KML File

I am wanting to search a KML file to see if a particular address falls within an overlay. Presently, I have the address converting to a geocode. However, I'm not sure what code is needed to add this functionality.

Here's the present code:

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

If I understand your question, I believe you want the formula to determine if a point ( google.maps.LatLng ) falls within one of your KML Placemark definitions (that you give names such as: Neighborhood Group 1 ). Within each Placemark , you define a Polygon and within each Polygon , you define a set of coordinates , which represent the vertices of the Polygon .

Using the coordinates within the Polygon and the LatLng you retrieve via geocoding, you could start with these formulas and select that one that is the best fit for you:

A very similar question has also been asked here on SO: Determine the lat lngs of markers within a polygon .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM