« grep in find command: how to display file names | Home | Usage of the temp tablespace in Oracle »

Yahoo! Maps API in VB.Net

Imports System

Imports System.Collections
Imports System.ComponentModel

Imports System.Data

Imports System.Drawing
Imports System.Globalization
Imports System.Xml
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls

Imports WCPierce.Web.UI.WebControls

Partial Class _Default
   Inherits System.Web.UI.Page

   Public YAHOOAPI_code = "YahooDemo"

   Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) _
   Handles MyBase.Load

       Dim gSmallMapControl As New GSmallMapControl()
       Dim gMapTypeControl As New GMapTypeControl()

       If IsPostBack() = False Then
           gMap.AddControl(gSmallMapControl)
           gMap.AddControl(gMapTypeControl)

           'initially centered on Atlanta
           Dim gpOrigin As New GPoint(-84.0F, 34.0F)

           gMap.CenterAndZoom(gpOrigin, 10)
           getLocations()

       End If

       displayDirections()

   End Sub
   Private Sub LatLon_change()
       Dim gpFrom As New GPoint
       Dim gpTo As New GPoint
       Dim gpOldFrom As New GPoint
       Dim gpOldTo As New GPoint
       Dim ioverlay As Integer

       For ioverlay = 0 To gMap.Overlays.Count - 1
           gMap.Overlays.RemoveAt(0)
       Next

       gpFrom = Point_plot("From Address", fromLat.Text, fromLon.Text)
       gpTo = Point_plot("To Address", toLat.Text, toLon.Text)

       oldFromLat.Value = fromLat.Text
       oldFromLon.Value = fromLon.Text
       oldToLat.Value = toLat.Text
       oldToLon.Value = toLon.Text

       zoomAndCenter(gpFrom, gpTo)

       Dim distanceBetweenPoints As Double
       distanceBetweenPoints = calcDistance(gpFrom, gpTo)
       distance.Text = distanceBetweenPoints.ToString()

   End Sub
   Function calcDistance(ByVal pStart As GPoint, ByVal pEnd As GPoint) As
Double
       Dim fRadiusOfEarth As New Double()
       Dim fLonA As New Double()
       Dim fLonB As New Double()
       Dim fLatA As New Double()
       Dim fLatB As New Double()
       Dim a As New Double()
       Dim c As New Double()
       Dim d As New Double()
       Dim fDistanceLon As New Double()
       Dim fDistanceLat As New Double()

       fRadiusOfEarth = 3959.871 ' radius of earth in miles
       'fRadiusOfEarth = (double)6372.795477598; ' radius of earth in km

       fLonA = pStart.X * Math.PI / 180
       fLonB = pEnd.X * Math.PI / 180
       fLatA = pStart.Y * Math.PI / 180
       fLatB = pEnd.Y * Math.PI / 180

       Dim dblFloat, dblFloat2 As Double

       fDistanceLon = fLonB - fLonA
       fDistanceLat = fLatB - fLatA

       a = Math.Pow(Math.Sin(fDistanceLat / 2), 2)
       dblFloat2 = Math.Pow(Math.Sin(fDistanceLon / 2), 2)

       a = a + (Math.Cos(fLatA) * Math.Cos(fLatB) * dblFloat2)

       dblFloat = Math.Sqrt(a)
       dblFloat2 = Math.Sqrt(1 - a)
       c = 2 * Math.Atan2(dblFloat, dblFloat2)

       d = fRadiusOfEarth * c

       calcDistance = d

   End Function
   Private Sub zoomAndCenter(ByVal gpFrom As GPoint, ByVal gpTo As GPoint)

       Dim spanLat As Double
       Dim spanLon As Double
       Dim latMid As Double
       Dim lonMid As Double
       Dim gpMidpoint As New GPoint()

       spanLat = Math.Abs(gpFrom.Y - gpTo.Y)
       spanLon = Math.Abs(gpFrom.X - gpTo.X)
       latMid = (gpFrom.Y + gpTo.Y) / 2
       lonMid = (gpFrom.X + gpTo.X) / 2
       gpMidpoint = New GPoint()
       gpMidpoint.X = lonMid
       gpMidpoint.Y = latMid

       Dim mapWidth As Double = 250
       Dim mapHeight As Double = 250

       Dim zoomX As Double
       zoomX = Math.Log(spanLon * 100000 * Math.Cos(latMid * Math.PI / 180)
/ mapWidth) / Math.Log(2)
       Dim zoomY As Double
       zoomY = Math.Log(spanLat * 100000 / mapHeight) / Math.Log(2)
       Dim iZoom As Integer
       iZoom = Math.Ceiling(Math.Max(zoomX, zoomY))
       gMap.CenterAndZoom(gpMidpoint, iZoom + 1)

   End Sub
   Private Function Point_plot(ByVal strTag As String, ByVal strLat As
String, ByVal strLon As String) As GPoint
       Dim gpPoint As New GPoint()

       Dim fLat As New Double
       Dim fLon As New Double

       If strLat <> "" And strLon <> "" Then

           fLat = Convert.ToDouble(strLat)

           fLon = Convert.ToDouble(strLon)

           gpPoint.Y = fLat
           gpPoint.X = fLon

           'gMap.CenterAndZoom(gpPoint, 5);

           Dim gm As New GMarker(gpPoint, strTag)

           gMap.Overlays.Add(gm)

       End If

       Point_plot = gpPoint

   End Function

   Private Sub Address_getLatLon()

       Dim strGeneratedURL As New
String("http://api.local.yahoo.com/MapsService/V1/geocode?")

       'start address
       strGeneratedURL = strGeneratedURL + "appid=" + YAHOOAPI_code
       strGeneratedURL = strGeneratedURL + "&street=" + fromAddr.Text + "+"
       strGeneratedURL = strGeneratedURL + "&city=" + fromCity.Text + "+"
       strGeneratedURL = strGeneratedURL + "&state=" + fromState.Text + "+"
       strGeneratedURL = strGeneratedURL + "&zip=" + fromZip.Text

       HiddenFieldLatLonFrom.Value = strGeneratedURL
       fromLat.Text = String_getFromXML(strGeneratedURL, "Latitude")
       fromLon.Text = String_getFromXML(strGeneratedURL, "Longitude")

       strGeneratedURL = "http://api.local.yahoo.com/MapsService/V1/geocode?"

       'destination address
       strGeneratedURL = strGeneratedURL + "appid=" + YAHOOAPI_code
       strGeneratedURL = strGeneratedURL + "&street=" + toAddr.Text + "+"
       strGeneratedURL = strGeneratedURL + "&city=" + toCity.Text + "+"
       strGeneratedURL = strGeneratedURL + "&state=" + toState.Text + "+"
       strGeneratedURL = strGeneratedURL + "&zip=" + toZip.Text

       HiddenFieldLatLonTo.Value = strGeneratedURL
       toLat.Text = String_getFromXML(strGeneratedURL, "Latitude")
       toLon.Text = String_getFromXML(strGeneratedURL, "Longitude")

   End Sub
   Private Function String_getFromXML(ByVal strGeneratedURL As String,
ByVal strTag As String) As String

       Dim XMLDoc As New System.Xml.XmlDocument()

       'XMLDoc.async = false

       XMLDoc.Load(strGeneratedURL)

       Dim nodeArr As XmlNodeList

       nodeArr = XMLDoc.GetElementsByTagName(strTag)

       String_getFromXML = nodeArr.Item(0).InnerText

   End Function
   Private Sub Directions_displayYahoo()
       'Aiming to generate a url like the following one:

'http://maps.yahoo.com/dd_result?newaddr=1199+Main+St&taddr=518+Smithstone+Trace+SE&csz=Atlanta%2C+GA+30349-5839&country=us&tcsz=Marietta+GA+30067&tcountry=us

       Dim strGeneratedURL As New
String("http://maps.yahoo.com/dd_result?newaddr=")

       'start address
       strGeneratedURL = strGeneratedURL + fromAddr.Text + "+"
       strGeneratedURL = strGeneratedURL + "&csz=" + fromCity.Text + "+"
       strGeneratedURL = strGeneratedURL + fromState.Text + "+"
       strGeneratedURL = strGeneratedURL + fromZip.Text
       strGeneratedURL = strGeneratedURL + "&country=us"

       'destination address
       strGeneratedURL = strGeneratedURL + "&taddr=" + toAddr.Text + "+"
       strGeneratedURL = strGeneratedURL + "&tcsz=" + toCity.Text + "+"
       strGeneratedURL = strGeneratedURL + toState.Text + "+"
       strGeneratedURL = strGeneratedURL + toZip.Text
       strGeneratedURL = strGeneratedURL + "&tcountry=us"

       HiddenField1.Value = strGeneratedURL

   End Sub

   Private Sub getDirections_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles getDirections.Click
       displayDirections()

   End Sub
   Sub putLocationsInFields(ByVal dsLocations As DataSet)
       Dim drStart As System.Data.DataRow
       Dim drEnd As System.Data.DataRow

       drStart = dsLocations.Tables.Item(0).Rows.Item(0)

       fromAddr.Text = drStart.Item("Address").ToString()
       fromCity.Text = drStart.Item("City").ToString()
       fromState.Text = drStart.Item("State").ToString()
       fromZip.Text = drStart.Item("Zip").ToString()

       drEnd = dsLocations.Tables.Item(0).Rows.Item(1)

       toAddr.Text = drEnd.Item("Address").ToString()
       toCity.Text = drEnd.Item("City").ToString()
       toState.Text = drEnd.Item("State").ToString()
       toZip.Text = drEnd.Item("Zip").ToString()

   End Sub
   Private Sub getLocations()
       Dim dsLocations As New DataSet
       Dim drStart As DataRow
       Dim drEnd As DataRow

       dsLocations.Tables.Add("Location")
       dsLocations.Tables(0).Columns.Add("Address", GetType(System.String))
       dsLocations.Tables(0).Columns.Add("City", GetType(System.String))
       dsLocations.Tables(0).Columns.Add("State", GetType(System.String))
       dsLocations.Tables(0).Columns.Add("Zip", GetType(System.String))
       dsLocations.Tables(0).Columns.Add("DistanceTo",
GetType(System.Decimal))

       drStart = dsLocations.Tables(0).NewRow

       drStart.Item("Address") = "1199 Main St"
       drStart.Item("City") = "Atlanta"
       drStart.Item("State") = "GA"
       drStart.Item("Zip") = "30349"
       drStart.Item("DistanceTo") = 0.0

       dsLocations.Tables(0).Rows.Add(drStart)

       drEnd = dsLocations.Tables(0).NewRow

       drEnd.Item("Address") = "2035 Main St"
       drEnd.Item("City") = "Atlanta"
       drEnd.Item("State") = "GA"
       drEnd.Item("Zip") = "30349"
       drEnd.Item("DistanceTo") = 0.0

       dsLocations.Tables(0).Rows.Add(drEnd)

       putLocationsInFields(dsLocations)

   End Sub
   Private Sub displayDirections()

       Directions_displayYahoo()
       Address_getLatLon()
       LatLon_change()

   End Sub
End Class

Topics: VB, VB.NET | Submitter: admin

Comments

You must be logged in to post a comment.

Keep on coding