87,904
社区成员
发帖
与我相关
我的任务
分享
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Data.Sql;
public partial class TEST1 : System.Web.UI.Page
{
public static string sCon = ConfigurationManager.AppSettings["connect"];
public static SqlConnection con = new SqlConnection(sCon);
public string longitude=string.Empty;//经度字符串
public string latitude = string.Empty; //纬度字符串
public static int SumCount;
protected void Page_Load(object sender, EventArgs e)
{//2、连接数据库
if (string.IsNullOrEmpty(sCon))
{
Response.Write("连接字符串为空!");
}
//3、打开数据库
if (con.State == ConnectionState.Closed)
{
con = new SqlConnection(sCon);
con.Open();
}
SqlCommand cmd = new SqlCommand("SELECT Longitude,Latitude FROM Goods", con);
if (!Page.IsPostBack)
{
GetLonLat(cmd, longitude, latitude);
}
}
public void GetLonLat(SqlCommand cmd, string longitude, string latitude)
{
double[] Longitudes = new double[1024];
double[] Latitudes = new double[1024];
int count = 0;
cmd.CommandTimeout = 600;
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
DataTable dt = new DataTable();//实例化一个datatable
dt.Load(reader);//将取到得数据填充到这个datatable中
reader.Dispose();//释放reader对象
con.Dispose();//释放conn对象
//判断dt中这有没有数据
for (int i = 0; i < dt.Rows.Count; i++)
{
//获取经度
string strlon = dt.Rows[i]["Longitude"].ToString();
Longitudes[count] = System.Double.Parse(strlon);
longitude += Longitudes[count] + "|";//将读取的传感器经纬度存储为字符串形式</span>
//获取纬度
string strlat = dt.Rows[i]["Latitude"].ToString();
Latitudes[count] = System.Double.Parse(strlat);
latitude += Latitudes[count] + "|";
count = count + 1;
}
SumCount = count;
Response.Write(longitude + count + latitude);
}
<span style="color: rgb(204, 0, 0);"> var array1 = '<%=longitude%>';//后台生成的经纬度字符串
var array2 = '<%=latitude %>';//后台生成的经纬度字符串
var pointX = array1.split('|');//解析字符串,生成相应的数组
var pointY = array2.split('|');//解析字符串,生成相应的数组</span>
<span style="color: rgb(204, 0, 0);"> var count = pointX.length;//数组长度</span>
for (var i = 0; i < count; i++) {
if (pointX[i] > 0 && pointY[i] > 0) {
addMarker(new BMap.Point(pointX[i], pointY[i]), i + 3); //向百度地图添加标记
}
}
s = 'y|y|y|y|x|x|x|x|4';
a = s.split('|'); //切割成数组
count = a.pop(); //得到项数
y = a.slice(0, count);
x = a.slice(count);