2012-02-16 26 views
3

我已經看到了在這裏做: http://www.somebody4u.com/Profile/Live如何向Bing地圖添加半徑圓?

,但我不能找到一種方法,使用API​​(AJAX)來做到這一點。

+0

4人:冰MSDN論壇答案】(https://social.msdn.microsoft.com/Forums/en-US/bd041bbd-:Bing地圖V8圈的答案可以從這裏得到的b4be-40b6-ae68-8b48aba83b64/how-to-create-accuracy-circle-in-bing-map-v8?forum = bingmapsajax) – RyBolt 2016-08-08 14:25:33

回答

12

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); 
+0

太棒了!非常感謝。 – 2013-02-21 00:19:17