【问题标题】:How do I add a radius circle to Bing Maps?如何将半径圆添加到 Bing 地图?
【发布时间】:2012-02-16 17:10:45
【问题描述】:

我在这里看到过它: http://www.somebody4u.com/Profile/Live

但我找不到使用 api (ajax) 的方法。

【问题讨论】:

标签: jquery ajax bing-maps


【解决方案1】:

Bing Maps V7 不包含画圈方法。

必须通过绘制几个小段来“近似”圆。

var key = .....; // API access key

var MM = Microsoft.Maps;
var R = 6371; // earth's mean radius in km

var map = new Microsoft.Maps.Map(this.element[0], {credentials: key, disableKeyboardInput: true});

var radius = ...;      //radius of the circle
var latitude = ...;    //latitude of the circle center
var longitude = ...;   //longitude of the circle center

var backgroundColor = new Microsoft.Maps.Color(10, 100, 0, 0);
var borderColor = new Microsoft.Maps.Color(150, 200, 0, 0);

var lat = (latitude * Math.PI) / 180;     
var lon = (longitude * Math.PI) / 180;
var d = parseFloat(radius) / R;
var circlePoints = new Array();

for (x = 0; x <= 360; x += 5) {
    var p2 = new MM.Location(0, 0);
    brng = x * Math.PI / 180;
    p2.latitude = Math.asin(Math.sin(lat) * Math.cos(d) + Math.cos(lat) * Math.sin(d) * Math.cos(brng));

    p2.longitude = ((lon + Math.atan2(Math.sin(brng) * Math.sin(d) * Math.cos(lat), 
                     Math.cos(d) - Math.sin(lat) * Math.sin(p2.latitude))) * 180) / Math.PI;
            p2.latitude = (p2.latitude * 180) / Math.PI;
            circlePoints.push(p2);
}

var polygon = new MM.Polygon(circlePoints, { fillColor: backgroundColor, strokeColor: borderColor, strokeThickness: 1 });

map.entities.push(polygon);

【讨论】:

    【解决方案2】:

    更好的是,现在 V8 文档中有一个示例。 Geolocation Accuracy Circle Example

    编辑:好的,上面的例子看起来很像这里其他答案的代码——而且,代码不起作用。我在文档页面上报告了它。我在 Bing 地图开发中心获得了一个工作示例。

    Generate a regular polygon

    var map = new Microsoft.Maps.Map(document.getElementById('myMap'), {});
    Microsoft.Maps.loadModule('Microsoft.Maps.SpatialMath', function () {
        // Get locations of a regular hexagon, 5 miles from each vertex the map center
        var locations = Microsoft.Maps.SpatialMath.getRegularPolygon(map.getCenter(), 5, 6, Microsoft.Maps.SpatialMath.DistanceUnits.Miles);
        var polygon = new Microsoft.Maps.Polygon(locations, null);
        map.entities.push(polygon);
    });
    

    如果你把6改成36,你会得到一个圆圈。

    【讨论】:

    • 我能够在他们的代码中找到错误(看起来他们只是在您报告它时忽略了您)。第 19 行必须如下所示 var path = Microsoft.Maps.SpatialMath.getRegularPolygon(loc, position.coords.accuracy, 36, Microsoft.Maps.SpatialMath.DistanceUnits.Meters); 请注意我是如何引用枚举“DistanceUnits”的。
    猜你喜欢
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多