Bing Maps包含位置功能

时间:2017-10-25 20:05:20

标签: bing-maps

我有一个使用Bing和Google地图的网站。每个功能都有Bing和Google版本。我在Bing地图中复制google.maps.geometry.poly.containsLocation函数时遇到了麻烦。有这样的事吗?

基本上我构建了一个多边形,我正在寻找一个图钉是否位于地图上多边形内部。

2 个答案:

答案 0 :(得分:3)

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>

答案 1 :(得分:0)

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

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>