Consulting

Results 1 to 4 of 4

Thread: Pull Latitude from XML and keep Double

  1. #1
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,211
    Location

    Pull Latitude from XML and keep Double

    Hi all,

    Not been on here for a while (sorry), I am having a mind blank with the below code. I am trying to geocode a postcode with Here Maps API, I have managed to pull the value i need from the XML but it does not keep it's decimal places.

    Obviously the decimal place of a Lat or Long is pretty important so i need to keep it.

    Here is the piece of VBA i suspect is causing my issue:
        Set regex = CreateObject("VBScript.RegExp"): regex.Pattern = """Latitude"".?([0-9]+)": regex.Global = False    
        Set matches = regex.Execute(objHTTP.responseText)
        tmpval = Replace(matches(0).SubMatches(0), ".", Application.International(xlListSeparator))
        MsgBox tmpval
    tmpval is defined as double by the way, i think it has something to do with the replacement part but i can't be sure.

    Any help will be appreciated

    Thanks in advance
    George
    Last edited by georgiboy; 01-30-2020 at 09:10 AM.
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2404, Build 17531.20128

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,734
    Location
    What EXACTLY is matches(0)?
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Expert Leith Ross's Avatar
    Joined
    Oct 2012
    Location
    San Francisco, California
    Posts
    552
    Location
    Hello georgiboy,

    Does the word latitude appear in quotes in the text? Do you need to preserve these quotes?

    @Paul - A Match occurs when the text returns a successful pattern match. Only one will be returned per each execution of the RegEx as the Global property is set to FalseA SubMatch is usually indicated in the RegEx Pattern by a pair of parentheses. The indices are zero based making SubMatch(0) the first one in the pattern. In the pattern above, (0-9)+, indicates 1 or more numbers that follow the word Latitude with or with a decimal point.
    Sincerely,
    Leith Ross

    "1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG"

  4. #4
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,211
    Location
    Thank you both for taking the time to have a look for me.

    I have only just come back to this to have a play and i have found the following:

    I believe the below part was only allowing the numbers and symbols within it (square brackets), anything after a symbol was removed thus leaving me with a round number.
    ([0-9]+)
    I thought i had a rounding issue with double but instead it was more of a string issue.

    I have changed it to the below and it now pulls through every bit of the latitude.
    ([0-9.-]+)
    I had to add the - also as it was not pulling the latitude if it was negative, this was due to the - symbol being first in the string so it was removing everything after the -

    I had also converted this from json to xml and used the below, but i am happy now i know a little more about VBScript.RegExp.
        Set oSeqNodes = oXml.SelectNodes("//NavigationPosition")    For Each oSeqNode In oSeqNodes
            Latitude = oSeqNode.SelectSingleNode("Latitude").Text ' lat
            Longitude = oSeqNode.SelectSingleNode("Longitude").Text ' long
        Next
    Thanks again

    George
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2404, Build 17531.20128

Posting Permissions

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