2

我正在使用 Google Maps 的 Map controll 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();
        }



        }

提前致谢。

4

1 回答 1

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(""), 
于 2013-07-01T19:03:42.433 回答