cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Need to share some code when posting a question or reply? Make sure to use the "Insert code sample" menu option. Learn more! X

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

jamesso
10-Marble

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

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

1 ACCEPTED SOLUTION

Accepted Solutions
jamesso
10-Marble
(To: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;

View solution in original post

12 REPLIES 12
posipova
20-Turquoise
(To:jamesso)

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

Thanks Polina. I will look for a posting tomorrow.

jamesso
10-Marble
(To: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;

stephenf
5-Regular Member
(To:jamesso)

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

Stephen,

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

Jim

stephenf
5-Regular Member
(To:jamesso)

Sure thing. Take a look at these example entities (Attachment). Includes a very simple mashup for entering an address and getting a lat/long back from the Google Geocoding API -- most likely in the exact same way that yours works.

Note - you need enter your Geocoding API key in the service "ConvertAddress" on the GoogleGeocodeCallTemplate Thing Template before the service will work!

Best of luck, let me know if you have any questions.

I have been getting by with the academic instance of ThingWorx supporting the udemy IoT course. I can't import a twx file into that platform since I do not have admin rights.

I could stand up a local developer instance I suppose and import that into it. Are there other alternatives to "read" the service logic?

Thanks

Jim

stephenf
5-Regular Member
(To:jamesso)

James,

No problem. I converted it to an xml file. Give this a try.

Stephen

Thanks Stephen,

I can read this and plan to adapt it for use in my mashup.

Jim

stephenf
5-Regular Member
(To:jamesso)

Great to hear. Hope you find it useful!

i try your code..

but come up error..

do you know why this error come up?

stephenf
5-Regular Member
(To:CHASEONHO)

Never seen that error before, although I was not using this with SSL enabled.  It looks like it is related to that. Check your SSL setup, and reference some online articles: java - Error - trustAnchors parameter must be non-empty - Stack Overflow

Top Tags