Results 1 to 8 of 8

Thread: [RESOLVED] Help converting this code and obtaining results

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Location
    United Kingdom
    Posts
    115

    Resolved [RESOLVED] Help converting this code and obtaining results

    Hi all

    I am using VB Express 2008.

    Sorry for the rather long post but I felt it would explain my problem better.

    I have used the code convertor at developerfusion to convert the following two pieces of code (originaly from http://www.justagile.com/blogs/posts...ogle-maps.aspx.

    Can anyone tell me how to get the returned Latitude and Longitude values to display in my textboxes named tab5txtLatitude and tab5txtLongitude?

    Original code part 1
    Code:
    public static GeocoderLocation Locate(string query)
    {
        WebRequest request = WebRequest.Create("http://maps.google.com/maps?output=kml&q="
            + HttpUtility.UrlEncode(query));
     
        using (WebResponse response = request.GetResponse())
        {
            using (Stream stream = response.GetResponseStream())
            {
                XDocument document = XDocument.Load(new StreamReader(stream));
     
                XNamespace ns = "http://earth.google.com/kml/2.0";
     
                XElement longitudeElement = document.Descendants(ns + "longitude").FirstOrDefault();
                XElement latitudeElement = document.Descendants(ns + "latitude").FirstOrDefault();
     
                if (longitudeElement != null && latitudeElement != null)
                {
                    return new GeocoderLocation
                    {
                        Longitude = Double.Parse(longitudeElement.Value, CultureInfo.InvariantCulture),
                        Latitude = Double.Parse(latitudeElement.Value, CultureInfo.InvariantCulture)
                    };
                }
            }
        }
     
        return null;
    }
    Original code part 2
    Code:
    [Serializable]
    public class GeocoderLocation
    {
        public double Longitude { get; set; }
        public double Latitude { get; set; }
     
        public override string ToString()
        {
            return String.Format("{0}, {1}", Latitude, Longitude);
        }
    }
    Converted code part 1
    Code:
    Public Shared Function Locate(ByVal query As String) As GeocoderLocation
            Dim request As WebRequest = WebRequest.Create("http://maps.google.com/maps?output=kml&q=" & HttpUtility.UrlEncode(query))
    
            Using response As WebResponse = request.GetResponse()
                Using stream As Stream = response.GetResponseStream()
                    Dim document As XDocument = XDocument.Load(New StreamReader(stream))
    
                    Dim ns As XNamespace = "http://earth.google.com/kml/2.0"
    
                    Dim longitudeElement As XElement = document.Descendants(ns + "longitude").FirstOrDefault()
                    Dim latitudeElement As XElement = document.Descendants(ns + "latitude").FirstOrDefault()
    
                    If longitudeElement IsNot Nothing AndAlso latitudeElement IsNot Nothing Then
                        Return New GeocoderLocation()
    
                    End If
                End Using
            End Using
    
            Return Nothing
        End Function
    Converted code part 2
    Code:
    <Serializable()> _
        Public Class GeocoderLocation
        Private _Longitude As Double
        Public Property Longitude() As Double
            Get
                Return _Longitude
            End Get
            Set(ByVal value As Double)
                _Longitude = value
            End Set
        End Property
        Private _Latitude As Double
        Public Property Latitude() As Double
            Get
                Return _Latitude
            End Get
            Set(ByVal value As Double)
                _Latitude = value
            End Set
        End Property
    
        Public Overloads Overrides Function ToString() As String
            Return [String].Format("{0}, {1}", Latitude, Longitude)
        End Function
    End Class
    Conversion seems to have been done ok apart from the two lines (in red text) which appear to be missing from the converted function.

    I call the locate function using
    Code:
    Locate(tab5txtPostCode.text)
    Thanks in advance for any help.
    Regards
    Colin

    If my comments helped please remember to add to my reputation using Rate This Post.

    If your problem has been resolved please mark the thread resolved using the "Mark Thread Resolved" link in thread tools.

    CodeBank Submissions: 1) WebBrowser - Delete cookies etc 2) Import My.Settings from previous build version

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Help converting this code and obtaining results

    The VB equivalent of this:
    Code:
                   return new GeocoderLocation
                    {
                        Longitude = Double.Parse(longitudeElement.Value, CultureInfo.InvariantCulture),
                        Latitude = Double.Parse(latitudeElement.Value, CultureInfo.InvariantCulture)
                    };
    is this:
    Code:
                   Return New GeocoderLocation With _
                    { _
                        Longitude = Double.Parse(longitudeElement.Value, CultureInfo.InvariantCulture), _
                        Latitude = Double.Parse(latitudeElement.Value, CultureInfo.InvariantCulture) _
                    }
    Obviously you can remove some of the line breaks if you want. I'd normally format that like this:
    Code:
    Return New GeocoderLocation With {Longitude = Double.Parse(longitudeElement.Value, CultureInfo.InvariantCulture), _
                                      Latitude = Double.Parse(latitudeElement.Value, CultureInfo.InvariantCulture)}
    You may want to look at Instant VB from Tangible Software for code conversions in future. It's not free, although there is a trial version, but it's the only product I've seen that is keeping pace with the changes in the VB and C# languages. It's results are close to perfect no matter how complex the code. I'm a user myself, so I speak from experience. I guess it just comes down to how much code you need to convert as to whether it's worth the cost.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Location
    United Kingdom
    Posts
    115

    Re: Help converting this code and obtaining results

    Thanks for that.

    May look at that software in the future, at the moment I don't really convert a lot of code.

    One final question.

    How would I get the return values from Latitude and Longitude in the function code into my textboxes "tab5txtLatitude" and "tab5txtLongitude"? The call to the function, as shown in my first post, is being made from a button click event.
    Regards
    Colin

    If my comments helped please remember to add to my reputation using Rate This Post.

    If your problem has been resolved please mark the thread resolved using the "Mark Thread Resolved" link in thread tools.

    CodeBank Submissions: 1) WebBrowser - Delete cookies etc 2) Import My.Settings from previous build version

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Help converting this code and obtaining results

    The method returns an onject and that object has Latitude and Longitude properties. I'm guessing that you know how to get a property value and display it in a TextBox.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Location
    United Kingdom
    Posts
    115

    Re: Help converting this code and obtaining results

    Sorry, Haven't done a lot of coding like this and therefore I'm still learning. I had originally coded this project using a webbrowser control, placing the postcode into the required field on a webpage and reading the coordinates that were returned by the web page. For this new version of the project I decided to go with the above code as the author reckons it is more accurate than some of the other geocoder sites etc, for uk post codes anyway.

    If you could give me an example of getting the property value into the textbox I'd be very grateful.
    Regards
    Colin

    If my comments helped please remember to add to my reputation using Rate This Post.

    If your problem has been resolved please mark the thread resolved using the "Mark Thread Resolved" link in thread tools.

    CodeBank Submissions: 1) WebBrowser - Delete cookies etc 2) Import My.Settings from previous build version

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Location
    United Kingdom
    Posts
    115

    Re: Help converting this code and obtaining results

    Silly me. Was calling the function then trying to get the results afterwards.

    Managed to populate the textboxes as follows. Not sure if it's right, but it works.
    Code:
    tab5txtLatitude.Text = Locate(mypcode).Latitude
                    tab5txtLongitude.Text = Locate(mypcode).Longitude
    Thanks for all the help. Really appreciate it.
    Regards
    Colin

    If my comments helped please remember to add to my reputation using Rate This Post.

    If your problem has been resolved please mark the thread resolved using the "Mark Thread Resolved" link in thread tools.

    CodeBank Submissions: 1) WebBrowser - Delete cookies etc 2) Import My.Settings from previous build version

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Help converting this code and obtaining results

    There's no point calling the method twice. Call it once and assign the result to a variable. You can then access that one object as many times as you like.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Location
    United Kingdom
    Posts
    115

    Re: [RESOLVED] Help converting this code and obtaining results

    Thanks for the tip. Didn't even think of that, makes much more sense though.
    Regards
    Colin

    If my comments helped please remember to add to my reputation using Rate This Post.

    If your problem has been resolved please mark the thread resolved using the "Mark Thread Resolved" link in thread tools.

    CodeBank Submissions: 1) WebBrowser - Delete cookies etc 2) Import My.Settings from previous build version

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width