2013-07-01 87 views
2

我正在使用Google地图控件DLL文件来创建Google地图应用程序,该应用程序使用C#ASP.NET显示来自我的SQL数据库的所有坐标点。我的问题是它显示标记,但不显示多段线以加入我的点。多段线不显示(谷歌地图)

这里是C#中的一段代码,获取的纬度,经度和标记的每个:

Here is the code: 

     double lat = 0; 
     double log = 0; 

     SqlConnection conn = new SqlConnection(sqlConStr); 
     SqlCommand cmd = new SqlCommand("SELECT Lat,Long FROM Tracks WHERE TrackID= @TrackID", conn); 
     cmd.Parameters.AddWithValue("@TrackID", addressType.SelectedValue); 

     SqlDataReader reader; 
     // Try to open database and read information. 
     try 
     { 
      conn.Open(); 
      reader = cmd.ExecuteReader(); 

      while (reader.Read()) 
      { 

       lat = Convert.ToDouble(reader[0].ToString()); 
       log = Convert.ToDouble(reader[1].ToString()); 


       GMap.Center = new LatLng(lat, log); 
       GMap.ZoomLevel= 12; 
       GMap.Add(new ImageMarker(GMap.Center, " ", new InfoWindow(""), "http://localhost/red.jpg")); 

       List<LatLng> Coord= new List<LatLng>(); 

       Coord.Add(new LatLng(lat, log)); 
       GMap.Add(new PolyLine(Coord, Color.Red, 100f, 2, new InfoWindow(""))); 


      } 
      reader.Close(); 
     } 
     catch (Exception err) 
     { 
      lblResults.Text = "Error Fetching "; 
      //err.Message; 
     } 
     finally 
     { 
      conn.Close(); 
     } 



     } 

由于提前。

+1

你不如果只包含单个点,则绘制多段线。 – user7116

+0

谢谢。我添加了一些固定坐标,它的工作原理,但我想从我的sql数据库中读取所有的纬度和经度,并将其添加到列表中,但这似乎并没有工作。任何想法请 – Fred

+0

你应该包括导致问题的实际代码。 – user7116

回答

1

你的问题是你不断向地图添加单点多义线,而不是包含所有点的多段线。

// define your list of coordinates OUTSIDE the loop, 
// otherwise you are just resetting it 
List<LatLng> Coords = new List<LatLng>(); 
while (reader.Read()) 
{ 
    // perhaps even: lat = reader.GetDouble(0); 
    lat = Convert.ToDouble(reader[0].ToString()); 
    log = Convert.ToDouble(reader[1].ToString()); 

    // add the next point in the polyline 
    Coords.Add(new LatLng(lat, log)); 
} 

现在你可以采取Coords(我改名为Coord为清楚起见),并把它添加到地图,再加上找到它的中心,并用它来标记在地图上:

// Once we have read in ALL of the points in the track, add the polyline 
// to the map. 
GMap.Add(new PolyLine(Coords, Color.Red, 100f, 2, new InfoWindow(""))); 

// Lastly, identify the center of the polyline and add that point: 
GMap.Center = Coords[Coords.Count/2]; 
GMap.ZoomLevel = 12; 
GMap.Add(new ImageMarker(GMap.Center, " ", new InfoWindow(""), 
+0

谢谢你的作品。欣赏它.. – Fred

+0

对不起仍然有问题的代码是好的,但我有大约1500个坐标点和我的浏览器冻结只是读出谷歌地图上的这些记录从SQL数据库。当它试图读取所有这些点时,我认为它会耗尽内存。请问你有什么建议?我必须仍然使用列表吗? – Fred

+0

@Fred:我建议你研究开始另一个问题,因为这是一个单独的问题(折线太多点,即你需要一个“折线简化”算法)。我还建议查看我们的[about]和[help]页面,它们可以帮助您最好地使用Stack Overflow。 – user7116