Trying To Get GPS Lat/Long From Previously Posted Project

  1. 3 months ago

    James R

    May 11 Pre-Release Testers, Xojo Pro Connecticut

    Hi,

    I am trying to replicate a project posted by Will Shank back in January 2014.

    link text

    I am using Xojo 2019 R1. This is what I have done as per Will's Code:

    1. Added TextField and called it addressFld
    2. Added TextField and called it latLonResultsFld
    3. Added a Button and called it downloadLatLonButton
    4. Added a URL connection. I can't find the HTTPSocket. ( I have no idea where that is.) Called it latLonSocket

    5) On the button I added this code:

    Dim head, query, tail As String   //build url
    head = "http://maps.googleapis.com/maps/api/geocode/xml?address="
    query = addressFld.Text.Trim.ReplaceAll(" ", "+")
    tail = "&sensor=false"
    
    latLonSocket.Get(head + query + tail)   //request data

    6) On the latLonSocket ContentReceived event (There is no PageReceived event) I added this code.

    Dim doc As New XmlDocument(content)  //parse to tree
    
    Dim lat, lon As String
    lat = xqlFirstInnerText(doc, "//geometry/location/lat")   //extract values
    lon = xqlFirstInnerText(doc, "//geometry/location/lng")
    
    latLonResultsFld.Text = "latitude: " + lat + ", longitude: " + lon   //show

    7) I created a Method and called it xqlFirstInnerText

    Function xqlFirstInnerText(doc As XmlDocument, query As String) As String
      dim nl As XmlNodeList = doc.Xql(query)
      if nl.Length = 0 then return ""
      dim n As XmlNode = nl.Item(0).FirstChild
      if n = nil or not n IsA XmlTextNode then return ""
      return n.Value
    End Function

    When I run it I get the error:

    Window1.downloadLatLonButton.Action, line 6
    Type "URLConnection" has no member named "Get"
    latLonSocket.Get(head + query + tail) //request data

    I'm not sure hat I am doing wrong here. The project is attached here.

    link text

    If anyone can help, I would appreciate it.

  2. Dave S

    May 11 San Diego, California USA

    I think it has to be HTTPS not HTTP

    and don't you need a code from GOOGLE to use that API???

  3. James R

    May 11 Pre-Release Testers, Xojo Pro Connecticut

    Hi Dave,

    Changed the http to https and no difference.

    Will did not state anything about a Key or Code in his post, so I'm not sure.

  4. Jean-Yves P

    May 11 Pre-Release Testers, Xojo Pro Europe (France, Besançon)

    google went from free to paid api for this maps API. it was last year IIRC.
    so yes you need an API key today with google maps.
    I changed my method to use openstreetmaps free api right away.
    it was quite easy.

  5. James R

    May 11 Pre-Release Testers, Xojo Pro Connecticut

    I was just trying to find out if there was a way to get the Lat/Lon for a location inside a Xojo Desktop app by typing in the address. I wanted this to be in my app, but I guess it is a little more complicated than I thought.

  6. Jean-Yves P

    May 11 Pre-Release Testers, Xojo Pro Europe (France, Besançon)

    here is your lucky day ...

    Public Sub UpdateMap(anAddress as String, zoomLevel as Integer)
      Dim oRequest As New HTTPSecureSocket
      oRequest.SetRequestHeader("User-Agent", "Xojo App")
      Dim SearchRequest As String = ReplaceAll( anAddress, " ", "+")
      
      Dim sResult As String = oRequest.Get("https://nominatim.openstreetmap.org/search?q="+SearchRequest+"&format=json&polygon=1&addressdetails=1", 10)
      
      Dim oJSONResult As JSONItem
      
      If (sResult <> "") Then
        Try
          oJSONResult = New JSONItem(sResult)
        Catch err As JSONException
          'could not parse
        End Try
      End If
      
      If (oJSONResult = Nil) Then Return
      
      'do something with oJSONResult
      Dim lat As Variant = oJSONResult.LookupFull( "lat","0")
      Dim lon As Variant = oJSONResult.LookupFull( "lon","0")
      
      centerLatitude = lat.DoubleValue
      centerLongitude = lon.DoubleValue
      
      UpdateMap( centerLatitude, centerLongitude, zoomLevel)
      
    End Sub
  7. James R

    May 11 Pre-Release Testers, Xojo Pro Connecticut

    Hi Jean,

    Thank you for spending the time to try to help me.
    I am kind of confused as to what to do with this.

    I created a Method Called UpdateMap with the parameters "anAddress as String, zoomLevel as Integer"

    I'm not what I should do from here. Do I create two TextFields called centerLogitude and the other centerLatitude? How do I get the Address string in?

    Do I make a TetxFiled called txtAddress and then write:

    anAddress=txtAddress.text

    If I just run the code as is, I get the errors:

    Window1.UpdateMap, line 20
    Type "JSONItem" has no member named "LookupFull"
    Dim lat As Variant = oJSONResult.LookupFull( "lat","0")

    Window1.UpdateMap, line 21
    Type "JSONItem" has no member named "LookupFull"
    Dim lon As Variant = oJSONResult.LookupFull( "lon","0")

    Window1.UpdateMap, line 23
    This method cannot accept an assigned value (it lacks an Assigns parameter).
    centerLatitude = lat.DoubleValue

    Window1.UpdateMap, line 24
    This method cannot accept an assigned value (it lacks an Assigns parameter).
    centerLongitude = lon.DoubleValue

    Window1.UpdateMap, line 26
    Too many arguments: got 3, expected only 2.
    UpdateMap( centerLatitude, centerLongitude, zoomLevel)

    I'm sorry, I just don't know what to do with this.

  8. Jean-Yves P

    May 11 Pre-Release Testers, Xojo Pro Europe (France, Besançon)

    sorry you need also this method :

    Public Function LookupFull(extends aJSON as JSONItem, aName as String, aDefault as Variant) as Variant
      If Not aJSON.IsArray Then Return aJSON.Lookup( aName, aDefault)
      
      For i As Integer = 0 To aJSON.Count-1
        Dim v As Variant = aJSON.Child( i).Lookup( aName, aDefault)
        If v<>aDefault Then
          Return v
        End If
      Next
      
      Return aDefault
      
    End Function

    and forget the last line "updatemap" it is to update the htmlviewer where the map is displayed.
    you enter the address above, and you get the centerlatitude and centerlongitude for that address at the bottom
    adapt to your method and parameters.

  9. James R

    May 11 Pre-Release Testers, Xojo Pro Connecticut

    Hi Jean,

    I added the method. I still get all these errors and a few new ones

    Window1.UpdateMap, line 20
    Type "JSONItem" has no member named "LookupFull"
    Dim lat As Variant = oJSONResult.LookupFull( "lat","0")

    Window1.UpdateMap, line 21
    Type "JSONItem" has no member named "LookupFull"
    Dim lon As Variant = oJSONResult.LookupFull( "lon","0")

    Window1.UpdateMap, line 23
    This method cannot accept an assigned value (it lacks an Assigns parameter).
    centerLatitude = lat.DoubleValue

    Window1.UpdateMap, line 24
    This method cannot accept an assigned value (it lacks an Assigns parameter).
    centerLongitude = lon.DoubleValue

    Window1.UpdateMap, line 26
    Too many arguments: got 3, expected only 2.
    UpdateMap( centerLatitude, centerLongitude, zoomLevel)

    Window1.LookupFull, line 10
    You cannot return a value because this method has not defined a return type

    Window1.LookupFull Declaration
    The extends modifier cannot be used on a class method
    Sub LookupFull(extends aJSON as JSONItem, aName as String, aDefault as Variant)

    I just don't see how this all works or what control are suppose to be on the screen or where you enter the address, and where you get the results.

  10. Andre K

    May 11 Pre-Release Testers

    @James R : This is what i did understand from @Jean-YvesPochez :
    link text

    In the textfield below the HTMLviewer insert the address you want in this form: City[space]Streetname[space]number and push then the button. In the labels you get the values you want.

    HTH,
    Andre

  11. Jean-Yves P

    May 11 Pre-Release Testers, Xojo Pro Europe (France, Besançon)

    Window1.LookupFull Declaration
    The extends modifier cannot be used on a class method

    you must locate the LookupFull method inside a module, and scope public.
    the extends methods cannot be located inside a class ( or a window)

  12. James R

    May 12 Pre-Release Testers, Xojo Pro Connecticut

    Thank you, Andre and Jean, for taking the time to help me! After posting my last message yesterday, I met with my son and he messed around with Jean's code for a while and showed me what I was doing wrong.

    I posted that version here.
    link text

    This morning I logged in to the forum and saw Andre's project. I downloaded it and it's very similar to what my son came up with. Thank you for doing that Andre. I wish I saw that yesterday.

    I very much appreciate the help from both of you. The GPS lookup is being used for a fire fighting application that requires the location of a fire station so it can obtain weather information from the Dark Sky Weather API which is replacing the Weather Underground API that no longer works in our application.

    Thank you all once again.

or Sign Up to reply!