Bing 地图包含定位功能

Bing Maps containsLocation function

我有一个同时使用 Bing 和 Google 地图的网站。每个函数都有一个 Bing 和 Google 版本。我在 Bing 映射中复制 google.maps.geometry.poly.containsLocation 函数时遇到问题。有这种东西吗?

基本上我构建了一个多边形,并希望确定图钉是否位于地图上的多边形内部。

您可以使用 Bing 地图 AJAX 控件的可扩展性来添加自己的方法。您可以将该扩展方法放在 Microsoft.Maps.Location class.

Microsoft.Maps.Location.prototype.IsInPolygon=function(polygon)
                {           
                    var isInside = false;
                    var j = 0;
                    var x = this.longitude;
                    var y = this.latitude;
                    var paths = polygon.getLocations();

                    for (var i = 0; i < paths.length ; i++) {
                        j++;
                        if (j == paths.length) { j = 0; }
                        if (((paths[i].latitude < y) && (paths[j].latitude >= y)) || ((paths[j].latitude < y) && (paths[i].latitude >= y))) {
                            if (paths[i].longitude + (y - paths[i].latitude) / (paths[j].latitude - paths[i].latitude) * (paths[j].longitude - paths[i].longitude) < x) {
                                isInside = !isInside
                            }
                        }
                    }           

                    return isInside;
                };

这是一个使用 Bing Maps V8 的工作示例:

<!DOCTYPE html>
<html>
    <head>
        <title>Bing Maps - V8 - Polygon test</title>
        <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
    </head>
    <body>
        <div id='myMap' style='width: 100vw; height: 100vh;'></div>
        <script type='text/javascript'>
            function load() {
                Microsoft.Maps.Location.prototype.IsInPolygon=function(polygon)
                {           
                    var isInside = false;
                    var j = 0;
                    var x = this.longitude;
                    var y = this.latitude;
                    var paths = polygon.getLocations();

                    for (var i = 0; i < paths.length ; i++) {
                        j++;
                        if (j == paths.length) { j = 0; }
                        if (((paths[i].latitude < y) && (paths[j].latitude >= y)) || ((paths[j].latitude < y) && (paths[i].latitude >= y))) {
                            if (paths[i].longitude + (y - paths[i].latitude) / (paths[j].latitude - paths[i].latitude) * (paths[j].longitude - paths[i].longitude) < x) {
                                isInside = !isInside
                            }
                        }
                    }           

                    return isInside;
                };

                var map = new Microsoft.Maps.Map(document.getElementById('myMap'), {
                    credentials: 'YOUR KEY'
                });
                var center = map.getCenter();
                var polygon = new Microsoft.Maps.Polygon([new Microsoft.Maps.Location(center.latitude - 0.05, center.longitude - 0.05),
                    new Microsoft.Maps.Location(center.latitude + 0.01, center.longitude - 0.05),
                    new Microsoft.Maps.Location(center.latitude + 0.01, center.longitude + 0.05)], { fillColor: 'yellow', strokeColor: 'orange',
                    strokeThickness: 5, strokeDashArray: [1, 2, 5, 10] });
                map.entities.push(polygon);

                var location = new Microsoft.Maps.Location(center.latitude, center.longitude);
                alert("The location is inside in the polygon : " + location.IsInPolygon(polygon));                
            }



        </script>
        <script type='text/javascript' src='https://www.bing.com/api/maps/mapcontrol?callback=load' async defer></script>
    </body>
</html>

Bing Maps V8 有一个空间数学模块,可以使用相交函数轻松地为您进行计算:

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
    </head>
    <body>
        <div id='myMap' style='width: 100vw; height: 100vh;'></div>
        <script type='text/javascript'>
            function load() {
                var map = new Microsoft.Maps.Map(document.getElementById('myMap'), {
                    credentials: 'YOUR BING MAPS KEY'
                });

                //Create a polygon and location for testing.    
                var center = map.getCenter();
                var polygon = new Microsoft.Maps.Polygon([new Microsoft.Maps.Location(center.latitude - 0.05, center.longitude - 0.05),
                    new Microsoft.Maps.Location(center.latitude + 0.01, center.longitude - 0.05),
                    new Microsoft.Maps.Location(center.latitude + 0.01, center.longitude + 0.05)], { fillColor: 'yellow', strokeColor: 'orange',
                    strokeThickness: 5, strokeDashArray: [1, 2, 5, 10] });
                map.entities.push(polygon);

                var location = new Microsoft.Maps.Location(center.latitude, center.longitude);

                //Load the Spatial Math module
                Microsoft.Maps.loadModule('Microsoft.Maps.SpatialMath', function () {
                    //Check to see if the shapes intersect.
                    var intersects = Microsoft.Maps.SpatialMath.Geometry.intersects(location, polygon);

                    if(intersects){
                        alert("The location is inside in the polygon");        
                    } else {
                    alert("The location is NOT inside in the polygon");        
                                    }
                });     
            }
        </script>
        <script type='text/javascript' src='https://www.bing.com/api/maps/mapcontrol?callback=load' async defer></script>
    </body>
</html>