如何在WPF中围绕一个纬度/长点在Bing地图上放置圆圈?

时间:2014-07-31 15:23:38

标签: html api maps bing-maps bing

我希望能够为半径输入数字。然后它将围绕指定的纬度/经度坐标放置一个圆。然后,在该图钉周围将有一个圆圈,其中包含用户指定的任何尺寸半径。这是我的代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;

namespace BingMapsPushpins
{
public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string Locations = GetLocations();
        Literal1.Text= @"
            <script type='text/javascript'            src='http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0'>
            </script>

            <script type='text/javascript'>

                var  map = new Microsoft.Maps.Map(document.getElementById('myMap'), {
                        credentials: 'My bing maps key'
                    });

                function GetMap() {

                    map.entities.clear();
                    " + Locations+ @"

                    function ZoomIn(e){
                        if (e.targetType == 'pushpin'){
                            var location = e.target.getLocation();                        

                            map.setView({
                                zoom:5,
                                center: location
                            });
                        }
                    }
                }


function AddCircle(latin, lonin, radius) 
{ 
var locs = new Array(); 
var lat1 = latin * Math.PI/180.0; 
var lon1 = lonin * Math.PI/180.0; 
var d = radius/3956; 
var x; 
for (x = 0; x <= 360; x++) 
{ 
    var tc = (x / 90)* Math.PI / 2; 
    var lat = Math.asin(Math.sin(lat1)*Math.cos(d)+Math.cos(lat1)*Math.sin(d)*Math.cos(tc)); 
    lat = 180.0 * lat / Math.PI; 
    var lon; 
     if (Math.cos(lat1)==0) 
    { 
        lon=lonin; // endpoint a pole 
    } 
    else 
    { 
        lon = ((lon1 - Math.asin(Math.sin(tc) * Math.sin(d)/Math.cos(lat1)) + Math.PI) % (2 * Math.PI)) - Math.PI; 
    } 
    lon = 180.0 * lon / Math.PI; 
    var loc = new VELatLong(lat,lon); 
    locs.push(loc); 
} 
var poly = new VEPolyline(999, locs, new VEColor(0,255,0,.5) , 4); 
return poly; 
}
            </script>";
    }

    private string GetLocations()
    {
        string Locations = "";
        using (SqlConnection con = 
            new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=DBO;Data Source=DATABASE\\SQL2012")) // Get connection string defined in web.config file
        {
            SqlCommand cmd = new SqlCommand("SELECT Latitude, Longitude FROM GeocodingTest", con);
            con.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                Locations += "var pushpin = new Microsoft.Maps.Pushpin(new Microsoft.Maps.Location("+
                    reader["Latitude"].ToString()+", "+reader["Longitude"].ToString()+
                    "), null);Microsoft.Maps.Events.addHandler(pushpin, 'click', ZoomIn);map.entities.push(pushpin);";
            }
        }
        return Locations;
    }        
}

}

1 个答案:

答案 0 :(得分:1)

为此,您需要计算圆的点数。以下是使用Bing Maps v6.3的示例:http://pietschsoft.com/post/2008/02/09/Virtual-Earth-Draw-a-Circle-Radius-Around-a-LatLong-Point

那就是说,你不应该在Bing Maps v6.3上做任何新的开发。它已经很老了,4年前被V7取代了。 V7更快,更小,功能更多。您可以在此处找到适用于V7的模块,以便在此处添加对圈子的支持:https://bingmapsv7modules.codeplex.com/wikipage?title=Circle%20Module