c# - 折线不显示(谷歌地图)

标签 c# asp.net google-maps

我正在使用 Google map 的 map 控件 DLL 文件来创建一个 Google map 应用程序,该应用程序使用 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();
        }



        }

提前致谢。

最佳答案

您的问题是您不断向 map 添加单点折线,而不是包含所有点的折线。

// 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)并将其添加到 map 中,然后找到它的中心并使用它在 map 上标记它:

// 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(""), 

关于c# - 折线不显示(谷歌地图),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17410823/

相关文章:

asp.net - ASPXGridView ClientSideEvents 如何获取选定行的 KeyField 值

javascript - 对象不支持属性或方法 'dataTable' ;即使包含 jQuery

c# - 有什么办法可以简化这种双重条件从句结构吗?

c# - 是否存在限制解决方案的访问修饰符?

c# - 具有不同签名的函数,但编译器调用了错误的函数

c# - 使用 open id connect server 在 asp.net core 中检索声明

android - 如何更改谷歌地图 v2 android 中的信息窗口(自定义)位置?

javascript - 未知 "BESbswy"<跨度>

javascript - 如何在 Googlemaps api v3 中添加 Spiderfier?

c# - 所有 LINQ 查询表达式关键字的列表?