Skip to main content
1-Visitor
April 3, 2017
Solved

Can I use Google Maps API to reverse geocode a lat/long value in a Service?

  • April 3, 2017
  • 2 replies
  • 8924 views

I am using the Google Maps widget in a Mashup and I can set a location property for my thing that way: latitude and longitude.

Using the Google Maps API (API key required from Google) you can convert a lat/long to an address string. (reverse geocode) Google has examples where you make an HTTPS request as a URL in the browser and then get back JSON that includes a value for the common address string.

I have used the PostText and GetJSON using the ContentLoaderFunctions mechanism to interact with an endpoint that understands REST.

But, and I may be missing something, I don't see how to get the JSON  content that comes back from the https request in the browser via REST. There are no CURL examples that I could find to REST-ify the Maps API usage.

Can anyone offer a suggestion? I would like a service to pass my location property's lat and long to Google at an external URL and then get the location string that I can save as a property.

If this is built into ThingWorx, a pointer to an explanation would be appreciated.

Thanks,

Jim

Best answer by jamesso

I figured this out. You do use the GetJSON function via ContentLoaderFunctions reference . This gets usable address text corresponding to the lat and long that are derived from the location property.

Here is the service logic that works for me (where you use the Maps API key you get from Google):

var params = {

    ignoreSSLErrors: true /* BOOLEAN */,

    useNTLM: false /* BOOLEAN */,

    timeout: 60 /* NUMBER */,

  url: "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + me.tagLocation.latitude +

    "," + me.tagLocation.longitude + "&key=YOUR_API_KEY" /* STRING */,

};

// result: JSON

var result = Resources["ContentLoaderFunctions"].GetJSON(params);

me.rawJSON = result;

me.tagLocationText = result.results[0].formatted_address;

2 replies

20-Turquoise
April 4, 2017

Let me get back to you tomorrow during the US office hours, I may have an old working example for you.

jamesso1-VisitorAuthor
1-Visitor
April 4, 2017

Thanks Polina. I will look for a posting tomorrow.

jamesso1-VisitorAuthorAnswer
1-Visitor
April 4, 2017

I figured this out. You do use the GetJSON function via ContentLoaderFunctions reference . This gets usable address text corresponding to the lat and long that are derived from the location property.

Here is the service logic that works for me (where you use the Maps API key you get from Google):

var params = {

    ignoreSSLErrors: true /* BOOLEAN */,

    useNTLM: false /* BOOLEAN */,

    timeout: 60 /* NUMBER */,

  url: "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + me.tagLocation.latitude +

    "," + me.tagLocation.longitude + "&key=YOUR_API_KEY" /* STRING */,

};

// result: JSON

var result = Resources["ContentLoaderFunctions"].GetJSON(params);

me.rawJSON = result;

me.tagLocationText = result.results[0].formatted_address;

7-Bedrock
April 4, 2017

JAMES SOLDERITSCH wrote:

I figured this out. You do use the GetJSON function via ContentLoaderFunctions reference . This gets usable address text corresponding to the lat and long that are derived from the location property.

Here is the service logic that works for me (where you use the Maps API key you get from Google):

var params = {

    ignoreSSLErrors: true /* BOOLEAN */,

    useNTLM: false /* BOOLEAN */,

    timeout: 60 /* NUMBER */,

  url: "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + me.tagLocation.latitude +

    "," + me.tagLocation.longitude + "&key=YOUR_API_KEY" /* STRING */,

};

// result: JSON

var result = Resources["ContentLoaderFunctions"].GetJSON(params);

me.rawJSON = result;

me.tagLocationText = result.results[0].formatted_address;

Hi,

Glad you got it working. I have an example that uses Google's reverse geocoding api, and plugs it into a mashup -- happy to share if you need any further assistance with this. As you mentioned, it utilizes the GetJSON function of "ContentLoaderFunctions".

Stephen

jamesso1-VisitorAuthor
1-Visitor
April 4, 2017

Stephen,

Always looking for good example code. Would be happy to learn what you have done.

Jim