Reimers.dk

.NET, AJAX and Google APIs brought together
Welcome to Reimers.dk Sign in | Join | Help
in Search

My VB.NET Working Examples - Load Markers From an SQL Server

Last post 01-11-2010, 22:47 by jjrdk. 11 replies.
Sort Posts: Previous Next
  •  12-24-2006, 13:30 169

    My VB.NET Working Examples - Load Markers From an SQL Server

    I just wanted to share my VB.NET example to help others out. - Please do the same and feel free to comment on better ways of doing things.

    Imports System.Web.Configuration
    Imports System.Data.SqlClient
    Imports System.Data
    Imports Reimers.WebControls
    Imports Reimers.Map

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Map.GoogleKey = WebConfigurationManager.AppSettings("MapKey").ToString

    If Not Page.IsPostBack Then
     
      Map.Center =
    New GoogleLatLng(42.562539, -83.148805)
      Map.Zoom = 15
      Map.Controls.Add(
    New Reimers.Map.Controls.GoogleSmallMapTypeControl("smc"))
      Map.Controls.Add(
    New Reimers.Map.Controls.GoogleSmallMapControl("sc"))

    End If

    'Handelers ********************

    AddHandler Sidebar.Click, AddressOf Sidebar_Click

    AddHandler Sidebar.Click, AddressOf Sidebar_Click

    AddHandler Map.OverlayClick, AddressOf Me.Map_MarkerClick

    'Handelers ********************

    Sidebar.AlternatingItemStyle.BackColor = System.Drawing.Color.Blue

    Sidebar.Overlays = Map.Overlays

    End Sub


    Protected Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

    Dim UserName = HiddenField1.Value

    Dim TourString As String = "SELECT dbo.tblDestinations.*, tblAddress.* FROM dbo.tblDestinations INNER JOIN tblAddress ON dbo.tblDestinations.DestinationID = tblAddress.DestinationID Where TourID= '" & ListBox1.SelectedValue & "'"

    Dim con1 As String = WebConfigurationManager.ConnectionStrings("ConnectionStringTour").ConnectionString()

    Dim Tours As New _

    System.Data.SqlClient.SqlDataAdapter(TourString, con1)

    Dim dst1 As New System.Data.DataSet()

    Tours.Fill(dst1, "tblDestinations")

    Dim menuFile As String

    menuFile = ""

    Dim lat As String

    Dim lng As String

    Dim address As String

    Dim city As String

    Dim state As String

    Dim zip As String

    Dim tele As String

    Dim name As String

    Dim website As String

    Dim logo As String

    Dim MyOption As New Reimers.Map.GoogleMarkerOptions

    Dim Baloon As String

    Dim destinationID As String

    Dim ic1 As New GoogleIcon("custom", "http://www.virtualeyes.net/royaloakchamber/icons/ATM.png", New GoogleSize(25, 30), New GooglePoint(16, 16), New GooglePoint(16, 16))

    Map.Icons.Add(ic1)

     

    For Each masterRow As System.Data.DataRow In dst1.Tables("tblDestinations").Rows()

    destinationID = masterRow("DestinationID").ToString()

    lat = masterRow("Latitude").ToString()

    lng = masterRow("Longitude").ToString()

    address = masterRow("Address1").ToString()

    city = masterRow("City").ToString()

    state = masterRow("State").ToString()

    zip = masterRow("Zip").ToString()

    tele = masterRow("Phone").ToString()

    name = masterRow("DestinationName").ToString()

    website = masterRow("website").ToString()

    logo = masterRow("Logo").ToString()

    Baloon = "<table cellpadding=5 style=width: 356px; position: static; height: 68px><tr><td align=left rowspan=2 valign=middle><img src=" & logo & " style=width: 88px; position: static; height: 90px /></td><td align=left rowspan=2 style=width: 353px valign=middle><strong>" & name & "<br /></strong>" & address & "<br />" & tele & "<br /><a href=http://" & website & " target=_blank >More Info </a><br /></td></tr><tr></tr></table>"

    Dim DestEvent As New Reimers.Map.GoogleMarkerOptions

    DestEvent.Clickable = True

    DestEvent.Title = masterRow("DestinationName").ToString()

    Dim MyMarker = New Reimers.Map.GoogleMarker(name, lat, lng, New GoogleMarkerOptions(ic1))

    MyMarker.MarkerText = Baloon

    Map.Overlays.Add(MyMarker)

    Next

    Map.PostRenderScript = Map.ZoomToBounds(Map.Overlays.Bounds)

    End Sub


    Protected Sub Map_MarkerClick(ByVal Map As Reimers.Map.GoogleMap, ByVal Marker As GoogleOverlay, ByRef MapCommand As String)

    If TypeOf Marker Is GoogleMarker Then

    Dim m As GoogleMarker = CType(Marker, GoogleMarker)

    MapCommand = m.OpenInfoWindowHTML(Map, "<div>" + m.MarkerText + "</div>")

    Else

    End If

    End Sub


    Protected Sub Map_Click(ByVal Map As Reimers.Map.GoogleMap, ByVal Point As GoogleLatLng, ByRef MapCommand As String)

    MapCommand = Map.OpenInfoWindowHTML(Point, "Lat: " + Point.Latitude.ToString + "<br />Lng: " + Point.Longitude.ToString)

    End Sub


    Function ClientGeocoder1_ClientGeocoded(ByVal Position As Reimers.Map.GoogleLatLng, ByVal IP As String) As String

    Dim l As Reimers.Map.Geocoding.Location = Reimers.Map.Geocoding.GoogleGeocoder.ReverseGeocode(Position)

    Dim gm As GoogleMarker = New GoogleMarker(DateTime.Now.Millisecond.ToString, l.Point)

    gm.MarkerText = String.Format("<p><strong>Nearest Location</strong></p><p>{0}, {1}</p>", l.Address.City, l.Address.Country)

    gm.ClientSideHandlers.OnClick = gm.OpenInfoWindowHTML(Map, gm.MarkerText)

    Return Map.AddOverlay(gm) + Map.CenterAndZoom(Position, 8) + Map.OpenInfoWindowHTML(Position, String.Format("Your IP address ({0}) is registered here.", IP))

    End Function


    Function Sidebar_MouseOut(ByVal Item As GoogleOverlay) As String

    Return Map.CloseInfoWindow

    End Function


    Function Sidebar_Click(ByVal Item As GoogleOverlay) As String

    If TypeOf Item Is GoogleMarker Then

    Dim m As GoogleMarker = CType(Item, GoogleMarker)

    Return m.OpenInfoWindowHTML(Map, m.MarkerText)

    Else

    Return Nothing

    End If

    End Function

  •  12-25-2006, 4:13 173 in reply to 169

    Re: My VB.NET Working Examples - Load Markers From an SQL Server

    Thanks for putting up an example that people can learn from (and writing it in VB.NET is nice since I do all my examples in C# which may be difficult for some).

    I have a few comments on your code, which are not serious but just to make your code more clean.

    Firstly you seem to create the Sidebar click handler twice - oversight?

    The second comment is about your indexchanged handler. I would suggest that you clear the Overlays collection before feeding new user values into it. The contents of the Overlays collection are persisted through postbacks and so if the user makes several choices his map will fill up quickly with old data.

    Furthermore I would suggest that you use SqlCommands and parameters in your search query. In theory someone could inject an item in your dropdown list with some nasty SQL code as the value thereby ruining your database.

    Again on the data retrieval I would suggest you use either an SqlDataReader to get your data. There is no need to use the SqlDataAdapter since you are not going to update the data in the database later. If you do find it easier to use the SqlDataAdapter then I would suggest you restrict yourself to filling only a DataTable. The DataSet is in fact only a superfluous shell.

     Since you are using the SqlDataAdapter you should have a strongly typed DataTable, which means you can cast your values directly into the proper type. I'm thinking about the Latitude and Longitude values which I'm supposing are stored as numeric values in the database. It's one of the things that is easily overlooked when not using Option Strict, but can improve performance.

    Again, thanks for posting the example and I encourage everyone to share more of their map ideas. Also feel free to post a link where people can see your page.

  •  05-07-2008, 4:24 1579 in reply to 169

    Re: My VB.NET Working Examples - Load Markers From an SQL Server

    Will it be possible for you to post an sample application??

    Filed under:
  •  05-13-2008, 5:52 1590 in reply to 169

    Re: My VB.NET Working Examples - Load Markers From an SQL Server

    Dim MyMarker = New Reimers.Map.GoogleMarker(name, lat, lng, New GoogleMarkerOptions(ic1))

     

    Surely everytime a loop occurs, mymarker would need to be called something else. You'd have to specify an array of markers possibly?

  •  05-13-2008, 7:55 1593 in reply to 1590

    Re: My VB.NET Working Examples - Load Markers From an SQL Server

    Ignore that last comment. The column did not have the identity property on.
  •  09-13-2009, 4:32 2746 in reply to 1593

    Re: My VB.NET Working Examples - Load Markers From an SQL Server

    Hey yall, I was curious about something. I am working with the OnZoomChanged and OnMoveEnd events. I am attempting to requery the DB and pull new results based on the bounds of the viewport. I am successful at rebinding the data and setting up the new markers. However the map & markers stay the same until I refresh the page or it posts back from another function on the page. Am I missing a property or method to refresh the markers on the page after I 'rebind' the control? Any help would be appreciated.

     

    Thanks

    Mike

  •  09-13-2009, 15:13 2748 in reply to 2746

    Re: My VB.NET Working Examples - Load Markers From an SQL Server

    It sounds like you are not passing any commands back to the map. What you need to do is pass back the commands you want to execute to the MapCommand property of the event args. In your case you would probably want to clear the map and then redraw the new markers:

    StringBuilder addAllOverlays = new StringBuilder();

    foreach(GoogleMarker mrk in markersToAdd){ addAllOverlays.Append(e.Map.AddOverlay(mrk)); } 

    e.MapCommand = e.Map.ClearOverlays() + addAllOverlays.ToString();

  •  09-13-2009, 18:28 2752 in reply to 2748

    Re: My VB.NET Working Examples - Load Markers From an SQL Server

    That did the trick, I am thankful for your help as I get to know the control!
  •  09-13-2009, 19:24 2753 in reply to 2752

    Re: My VB.NET Working Examples - Load Markers From an SQL Server

    Another quick question, what kind of object is "markersToAdd" - I created a function that would return a  GoogleOverlayCollection and I am looping over it and adding them like your previous post. I have lost the ability to click and show the info window however. Seems like that error is based on the overlay being "Undefined' - did I do something wrong? My thanks in advance

  •  10-05-2009, 16:02 2797 in reply to 169

    Re: My VB.NET Working Examples - Load Markers From an SQL Server

    Hi,

    Do you have this in a downloadable format?  When I copy and paste, I get a lot of errors.

    Jim

  •  01-11-2010, 7:36 2941 in reply to 169

    How to Laod or overlay Postgis tables or Layers

    Dear All,

     

     

           I want to overlay existing polygon layers(tables) on gmap.I have about 100 tables or layers which are stored in PostgreSQL/PostGIS .I want to populates those layers on gmap.is it possible.If it is please guide me how to overlay..

     

     I am waiting for your great response.

     

    Thanks and Regards,

     

    Venkat

  •  01-11-2010, 22:47 2946 in reply to 2941

    Re: How to Laod or overlay Postgis tables or Layers

    You will need to write some code that loops through your dataset and transfers the points to a GooglePolygon. After that it's just a question of adding the polygon as an overlay.
View as RSS news feed in XML