With Google Maps API V3, determine if a marker is inside a KML Layer boundary

后端 未结 4 979
情话喂你
情话喂你 2021-02-02 04:10

Is there a way to determine if a marker has entered an area covered by a KmlLayer? My .kml is mostly made up of a with a bunch of coordinates that

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-02 04:32

    I had a request to provide more detail on how I implemented this. You can see an example here: http://www.usinternet.com/fiber-info/map.php

    I have a couple markers on the map, and I want to test if they are inside or outside a boundary. I set them up like this:

            var userLocation = new google.maps.LatLng(44.928633,-93.298919);    //Lyndale Park
    
            var wendysLocation = new google.maps.LatLng(44.948056,-93.282222);  //Wendy's on Lake St
    
            var userMarker = new google.maps.Marker({
                position: userLocation,
                map: map,
                title:"Your Location",
                draggable: true
            });
    
            var wendysMarker = new google.maps.Marker({
                position: wendysLocation,
                map: map,
                title:"Wendy's on Lake St",
                draggable: true
            });
    

    To create the polygon you need something to read the KML file and build a set of coordinates, which can then be added to the polygon's paths: property. Unfortunately it doesn't seem like you can go from KML to Polygon just using the Google Maps API, but somebody correct me if I'm wrong!

    var sector3PillburyPleasant = new google.maps.KmlLayer('http://usinternet.com/fiber-info/kml/Sector3Pillbury-Pleasant.kml', kmlOptions);
                sector3PillburyPleasant.setMap(map);
    
                var coordsSector3PillburyPleasant = [
                    new google.maps.LatLng(44.91615269014508, -93.28382953316716),
                    new google.maps.LatLng(44.91619137463104, -93.28120338511586), 
                    new google.maps.LatLng(44.93046751403393, -93.28114057929436),
                    new google.maps.LatLng(44.93046991974436, -93.28055243604703),
                    new google.maps.LatLng(44.94815274527994, -93.28053962401711),
                    new google.maps.LatLng(44.94815856171297, -93.28364017122617),
                ];
    
                var polySector3PillburyPleasant = new google.maps.Polygon({
                    paths: coordsSector3PillburyPleasant,
                    strokeColor: "#FFFFFF",
                    strokeOpacity: 0.0,
                    fillColor: "#FFFFFF",
                    fillOpacity: 0.0
                });
    

    I parsed the polygon's path down from the KML, the coordinates that make up the path were nested like this: (the last coordinate pair isn't needed if you are closing the path.)

    
    
        
            
                1
                
                    
                        
                            -93.28382953316716,44.91615269014508,0 -93.28120338511586,44.91619137463104,0 -93.28114057929436,44.93046751403393,0 -93.28055243604703,44.93046991974436,0 -93.28053962401711,44.94815274527994,0 -93.28364017122617,44.94815856171297,0 -93.28382953316716,44.91615269014508,0 
                        
                    
                
            
        
    
    
    

    Then I test whether the marker's I've got are inside or outside of the polygon:

            var markerIn = google.maps.geometry.poly.containsLocation(wendysMarker.getPosition(), polySector3PillburyPleasant);
    
            var markerOut = google.maps.geometry.poly.containsLocation(userMarker.getPosition(), polySector3PillburyPleasant);
    
            console.log(markerIn);
    
            console.log(markerOut);
    

    Then you've got yourself a true or false test if you are inside or outside the KML layer, though there was a bit of a detour to turn the KML layer into a Polygon.

提交回复
热议问题