Fred
Fred

Reputation: 25

Polylines not displaying (Google Maps)

I am using the Map controll DLL file for Google Maps to create a Google map application that displays all coordinate points from my SQL database using C# ASP.NET. My problem is that it displays the markers, but does not display the polylines to join my points.

Here is the piece of code in C# that gets the latitude, longitude and marks for each:

  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();
        }



        }

Thanks in Advance.

Upvotes: 2

Views: 1358

Answers (1)

user7116
user7116

Reputation: 64118

Your problem is you're constantly adding single point polylines to the map, rather than a polyline containing all of the points.

// 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));
}

Now you can take Coords (I renamed Coord for clarity) and add it to the map, plus find its center and use that to mark it on the 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(""), 

Upvotes: 1

Related Questions