Example Code

Get Google Maps API XML Data using LabVIEW

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Software

  • LabVIEW
  • LabVIEW Communications

Code and Documents

Attachment

Description

1. Overview
This example VI creates a Google Maps API URL for a specified address, and returns its XML content using an HTTP method.

 

2. Description
Using the Google Maps API Subset shown here to build up the URL string, you can easily get the Latitude and Longitude and view other data by entering an address into this VI. This could be further expanded to read back this XML data via DataSockets for parsing, and even then using those parameters to automatically show the location via Google Earth.
This example also uses the HTTP method GET in LabVIEW to get the URL's content. Use this example to understand how to fetch the content of simple URL's.

 

3. Requirements
NI LabVIEW Base Development System 2012 (or compatible).

 

4. Steps to Implement or Execute Code

  1. Download and open the VI.
  2. Fill the address in the First Address Line, Town/City, and Country/State string controls.
  3. Run the VI.

5. Additional Information or References

 

Get Google Maps API XML Data Screenshot.PNG

 

 

Searching a few parameters for National Instruments UK Office returns the following XML:

 

{
  "status": "OK",
  "results": [ {
    "types": [ "locality", "political" ],
    "formatted_address": "Newbury, Berkshire, UK",
    "address_components": [ {
      "long_name": "Newbury",
      "short_name": "Newbury",
      "types": [ "locality", "political" ]
    }, {
      "long_name": "West Berkshire",
      "short_name": "West Berkshire",
      "types": [ "administrative_area_level_3", "political" ]
    }, {
      "long_name": "West Berkshire",
      "short_name": "West Berkshire",
      "types": [ "administrative_area_level_2", "political" ]
    }, {
      "long_name": "England",
      "short_name": "England",
      "types": [ "administrative_area_level_1", "political" ]
    }, {
      "long_name": "United Kingdom",
      "short_name": "GB",
      "types": [ "country", "political" ]
    } ],
    "geometry": {
      "location": {
        "lat": 51.4003873,
        "lng": -1.3218591
      },
      "location_type": "APPROXIMATE",
      "viewport": {
        "southwest": {
          "lat": 51.3864628,
          "lng": -1.3538739
        },
        "northeast": {
          "lat": 51.4143075,
          "lng": -1.2898443
        }
      },
      "bounds": {
        "southwest": {
          "lat": 51.3671040,
          "lng": -1.3532450
        },
        "northeast": {
          "lat": 51.4145500,
          "lng": -1.2881960
        }
      }
    },
    "partial_match": true
  } ]
}
Applications Engineer

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
wirebirdlabs
Member
Member
on

A few areas of improvement:

  1. That's JSON data transfer protocol, not XML. The lack of <>'s gives it away, and the "json" in the URL
  2. The "Search and Replace String" needs TRUE wired into the "replace all?" terminal
  3. County/State does not need a comma after
  4. You can reduce the amoung of G by using a "Format into String"
  5. As shown in the snippet below, "Search and Replace String" does not even need to be called (it's disabled), probably because the DS Read is sanitizing the URL

GoogleMapsSnippet.png

peter_smith
Member
Member
on

Hi!

I just wanted to let people reading this know, that JSON to XML conversion can be achieved fairly simpy. I wrote a document, where I use JSON-XML conversion. It probably worth to read and watch the code.

Peter