locked
Getting Longitude and Latitude In My Lightswitch Desktop Client Applicatiom RRS feed

  • Question

  • I have created a Lightswitch Desktop Client Application where the screen looks like the following.

    I want users to type in an address and click Get Location to populate longitude, latitude, and coordinates.

    I've looked at tons of examples and have tried them all and am not sure where I am going wrong?

    I want to use as ingle method to make a geocoding request to Google maps and I have a key.

    I tried following the the examplehttp://www.c-sharpcorner.com/uploadfile/scottlysle/geocode-an-address-using-google-maps-in-vb-net/ but to no avail.

    Can someone help me please or point me in the right direction.

    Namespace LightSwitchApplication

        Public Class CreateNewRightOfEntry

            Private Sub CreateNewRightOfEntry_InitializeDataWorkspace(ByVal saveChangesTo As Global.System.Collections.Generic.List(Of Global.Microsoft.LightSwitch.IDataService))
                ' Write your code here.
                Me.RightOfEntryProperty = New RightOfEntry()
                Me.RightOfEntryProperty.status = 0
            End Sub

            Private Sub CreateNewRightOfEntry_Saved()
                ' Write your code here.
                Me.Close(False)
                Application.Current.ShowDefaultScreen(Me.RightOfEntryProperty)
            End Sub

            Public Class LatLon
                Public Property Latitude As Double
                Public Property Longitude As Double
                Public Sub New()
                End Sub
                Public Sub New(ByVal lat As Double, ByVal lon As Double)
                    Me.Latitude = lat
                    Me.Longitude = lon
                End Sub
            End Class

            Public Function GetLatLon(ByVal addr As String) As LatLon
                Dim url As String = "http://maps.google.com/maps/geo?output=csv&key=AIzaSyCtmVN61Pflg_hCV-xOuzbgGj9fM2m2lkQ&q=" & addr
                Dim request As System.Net.WebRequest = WebRequest.Create(url)

                Dim response As HttpWebResponse = request.GetResponse()

                If response.StatusCode = HttpStatusCode.OK Then
                    Dim ms As New System.IO.MemoryStream()
                    Dim responseStream As System.IO.Stream = response.GetResponseStream()
                    Dim buffer(2048) As Byte
                    Dim count As Integer = responseStream.Read(buffer, 0, buffer.Length)
                    While count > 0
                        ms.Write(buffer, 0, count)
                        count = responseStream.Read(buffer, 0, buffer.Length)
                    End While
                    responseStream.Close()
                    ms.Close()
                    Dim responseBytes() As Byte = ms.ToArray()
                    Dim encoding As New System.Text.UTF8Encoding
                    Dim coords As String = encoding.GetString(responseBytes, 0, count)
                    Dim parts() As String = coords.Split(",")
                    Return New LatLon(Convert.ToDouble(parts(2)), Convert.ToDouble(parts(3)))
                End If
                Return Nothing
            End Function

            Private Sub GetLocation_Execute()
                ' Write your code here.
                Try
                    Dim ll As New LatLon()
                    ll = GetLatLon(RightOfEntryProperty.address_number & RightOfEntryProperty.address_street & "," & RightOfEntryProperty.city & "," & RightOfEntryProperty.state)
                    Me.RightOfEntryProperty.Latitude = ll.Latitude.ToString()
                    Me.RightOfEntryProperty.Longitude = ll.Longitude.ToString()
                Catch ex As Exception
                    MessageBox.Show("An error has occurred")
                End Try
            End Sub
        End Class
    End Namespace

    I keep getting 'GetResponse' is not a member of 'Webrequest'.   I've tried adding Imports System.Net.WebRequest but it has no impact.

    Please help.  What am I doing wrong

    I looked at:  https://msdn.microsoft.com/en-us/library/system.net.webresponse%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    Not sure where to put the mentioned:

    <SerializableAttribute>
    Public MustInherit Class WebResponse
    	Inherits MarshalByRefObject
    	Implements ISerializable, IDisposable

    • Edited by JAMRE01 Wednesday, September 28, 2016 10:13 PM
    Tuesday, September 27, 2016 8:27 AM

Answers

  • Hi Jamre,

    This is what I did. I making my request through the table validate function (Serverside).

    See below code.

    Remember to do .toLower and maybe Removewhitespace in your client fields before you make the request to the server.

       Dim errorDesc = ""
                Dim isValid = False
    
    
    
                Dim url3 = String.Format("https://dev.virtualearth.net/REST/v1/Locations/{0},{1},{2},{3},?o=xml&key={4}",
                UrlEncode(entity.Housenumber),
                UrlEncode(entity.validationadress),
                UrlEncode(entity.validationcity),
                UrlEncode(entity.country),
                "Apikey Here")
    
    
     Try
                        'Call the service and load the XML result 
                        If entity.Housenumber IsNot Nothing AndAlso entity.adress IsNot Nothing Then
                            Dim addressData = XElement.Load(url3)
                            
    
    
    
                            'Check for errors first
                            Dim err = addressData.<ErrorDetails>
                            If err.Any > "0" Then
                                errorDesc = err.<string>.Value
    
                            Else
    
                                entity.Latitude = addressData.<ResourceSets>.<ResourceSet>.<Resources>.<Location>.<BoundingBox>.<SouthLatitude>.Value.Remove(6)
                                entity.Longitude = addressData.<ResourceSets>.<ResourceSet>.<Resources>.<Location>.<BoundingBox>.<WestLongitude>.Value.Remove(6)
                                entity.Latitudewest = addressData.<ResourceSets>.<ResourceSet>.<Resources>.<Location>.<BoundingBox>.<NorthLatitude>.Value.Remove(6)
                                entity.Longitudewest = addressData.<ResourceSets>.<ResourceSet>.<Resources>.<Location>.<BoundingBox>.<EastLongitude>.Value.Remove(6)
                                entity.Fulladress = addressData.<ResourceSets>.<ResourceSet>.<Resources>.<Location>.<Address>.<FormattedAddress>.Value
                                entity.FullCord = addressData.<ResourceSets>.<ResourceSet>.<Resources>.<Location>.<Point>.<Latitude>.Value
                                entity.FullCordLng = addressData.<ResourceSets>.<ResourceSet>.<Resources>.<Location>.<Point>.<Longitude>.Value
    
                                
                                isValid = True
                            End If
                        Else
                            Dim addressData2 = XElement.Load(url3)
    
                            Dim err = addressData2.<ErrorDetails>
                            If err.Any > "0" Then
                                errorDesc = err.<string>.Value
    
                            Else
                                entity.Latitude = addressData2.<ResourceSets>.<ResourceSet>.<Resources>.<Location>.<BoundingBox>.<SouthLatitude>.Value.Remove(6)
                                entity.Longitude = addressData2.<ResourceSets>.<ResourceSet>.<Resources>.<Location>.<BoundingBox>.<WestLongitude>.Value.Remove(6)
                                entity.Latitudewest = addressData2.<ResourceSets>.<ResourceSet>.<Resources>.<Location>.<BoundingBox>.<NorthLatitude>.Value.Remove(6)
                                entity.Longitudewest = addressData2.<ResourceSets>.<ResourceSet>.<Resources>.<Location>.<BoundingBox>.<EastLongitude>.Value.Remove(6)
                                entity.Fulladress = addressData2.<ResourceSets>.<ResourceSet>.<Resources>.<Location>.<Address>.<FormattedAddress>.Value
                                entity.FullCord = addressData2.<ResourceSets>.<ResourceSet>.<Resources>.<Location>.<Point>.<Latitude>.Value
                                entity.FullCordLng = addressData2.<ResourceSets>.<ResourceSet>.<Resources>.<Location>.<Point>.<Longitude>.Value
                                'entity.Map = addressDataMap.Value.OfType(Of Byte)()
                                isValid = True
    
                            End If
    
    
    
    
    
                        End If
    
    
    
    
                    Catch ex As Exception
                        Trace.TraceError(ex)
                    End Try


    Sven Elm

    • Proposed as answer by Angie Xu Monday, October 10, 2016 3:11 AM
    • Marked as answer by Angie Xu Monday, October 10, 2016 3:11 AM
    Wednesday, October 5, 2016 12:49 PM

All replies

  • Hi, Done that a few years ago. I think I used the validate method. I will look if I have an example.

    Sven Elm

    Thursday, September 29, 2016 6:33 AM
  • Hi JAMRE01,

    You could have a try put breakpoint in your application, it will be helpful for you to troubleshoot.

    In addition, in the Options window, find Projects and Solutions >> Build and Run. Find Diagnostic in the drop-down for MSBuild project build output verbosity.

    Regards,

    Angie


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, September 29, 2016 10:14 AM
  • Sven,

    were you able to find the example you referred?

    - Jamre01


    jamre01

    Wednesday, October 5, 2016 12:02 PM
  • Hi Jamre,

    This is what I did. I making my request through the table validate function (Serverside).

    See below code.

    Remember to do .toLower and maybe Removewhitespace in your client fields before you make the request to the server.

       Dim errorDesc = ""
                Dim isValid = False
    
    
    
                Dim url3 = String.Format("https://dev.virtualearth.net/REST/v1/Locations/{0},{1},{2},{3},?o=xml&key={4}",
                UrlEncode(entity.Housenumber),
                UrlEncode(entity.validationadress),
                UrlEncode(entity.validationcity),
                UrlEncode(entity.country),
                "Apikey Here")
    
    
     Try
                        'Call the service and load the XML result 
                        If entity.Housenumber IsNot Nothing AndAlso entity.adress IsNot Nothing Then
                            Dim addressData = XElement.Load(url3)
                            
    
    
    
                            'Check for errors first
                            Dim err = addressData.<ErrorDetails>
                            If err.Any > "0" Then
                                errorDesc = err.<string>.Value
    
                            Else
    
                                entity.Latitude = addressData.<ResourceSets>.<ResourceSet>.<Resources>.<Location>.<BoundingBox>.<SouthLatitude>.Value.Remove(6)
                                entity.Longitude = addressData.<ResourceSets>.<ResourceSet>.<Resources>.<Location>.<BoundingBox>.<WestLongitude>.Value.Remove(6)
                                entity.Latitudewest = addressData.<ResourceSets>.<ResourceSet>.<Resources>.<Location>.<BoundingBox>.<NorthLatitude>.Value.Remove(6)
                                entity.Longitudewest = addressData.<ResourceSets>.<ResourceSet>.<Resources>.<Location>.<BoundingBox>.<EastLongitude>.Value.Remove(6)
                                entity.Fulladress = addressData.<ResourceSets>.<ResourceSet>.<Resources>.<Location>.<Address>.<FormattedAddress>.Value
                                entity.FullCord = addressData.<ResourceSets>.<ResourceSet>.<Resources>.<Location>.<Point>.<Latitude>.Value
                                entity.FullCordLng = addressData.<ResourceSets>.<ResourceSet>.<Resources>.<Location>.<Point>.<Longitude>.Value
    
                                
                                isValid = True
                            End If
                        Else
                            Dim addressData2 = XElement.Load(url3)
    
                            Dim err = addressData2.<ErrorDetails>
                            If err.Any > "0" Then
                                errorDesc = err.<string>.Value
    
                            Else
                                entity.Latitude = addressData2.<ResourceSets>.<ResourceSet>.<Resources>.<Location>.<BoundingBox>.<SouthLatitude>.Value.Remove(6)
                                entity.Longitude = addressData2.<ResourceSets>.<ResourceSet>.<Resources>.<Location>.<BoundingBox>.<WestLongitude>.Value.Remove(6)
                                entity.Latitudewest = addressData2.<ResourceSets>.<ResourceSet>.<Resources>.<Location>.<BoundingBox>.<NorthLatitude>.Value.Remove(6)
                                entity.Longitudewest = addressData2.<ResourceSets>.<ResourceSet>.<Resources>.<Location>.<BoundingBox>.<EastLongitude>.Value.Remove(6)
                                entity.Fulladress = addressData2.<ResourceSets>.<ResourceSet>.<Resources>.<Location>.<Address>.<FormattedAddress>.Value
                                entity.FullCord = addressData2.<ResourceSets>.<ResourceSet>.<Resources>.<Location>.<Point>.<Latitude>.Value
                                entity.FullCordLng = addressData2.<ResourceSets>.<ResourceSet>.<Resources>.<Location>.<Point>.<Longitude>.Value
                                'entity.Map = addressDataMap.Value.OfType(Of Byte)()
                                isValid = True
    
                            End If
    
    
    
    
    
                        End If
    
    
    
    
                    Catch ex As Exception
                        Trace.TraceError(ex)
                    End Try


    Sven Elm

    • Proposed as answer by Angie Xu Monday, October 10, 2016 3:11 AM
    • Marked as answer by Angie Xu Monday, October 10, 2016 3:11 AM
    Wednesday, October 5, 2016 12:49 PM