1

Before I begin my question: Last time I asked this question a couple of you posted helpful links to parsing the XML. But my question is asking how to get the XML information into a string variable in the first place. It is NOT a duplicate of those links.

Hopefully my question makes sense. But I am trying to store the XML I get from the Google geocode API (https://developers.google.com/maps/documentation/geocoding/intro) so I can autoformat addresses that people type into some fields.

Essentially I am looking for a Javascript equivalent of =WEBSERVICE([URL]) in MS Excel.

The idea is to retrieve the address fields that people type, put it into the geocode URL, store the XML as a string (this is the step I am having trouble with), and then set the fields to the results gotten from the API.

Mike
  • 961
  • 6
  • 19
  • 43
  • Store it where? You know that javascript has limited capabilities for data persistence, don't you? – lucas Mar 30 '16 at 19:30
  • In a string variable. And I did not know that. I do not have much experience with Javascript. – Mike Mar 30 '16 at 20:19
  • Yes, to persist data locally across sessions you would need to store it in cookies or localstorage. To persist data and make it accessible between users you would need a database so in the end you will most likely end up recreating what google provides out of the box. From what I understand, anyway. Maybe I am confused about your goals. Either way, I would suggest requesting json output (see docs) as that will at least save you the runaround of parsing xml – lucas Mar 31 '16 at 00:02
  • Oh, I don't need to store the data between sessions. I just need it stored as a text variable so I can use find/substring functions to retrieve the address data and basically print it to the screen. – Mike Mar 31 '16 at 23:05

2 Answers2

1

A simple example using jQuery:

var response = $.get('https://maps.googleapis.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=YOUR_API_KEY');
var text = response.responseText;

$.get will return a jqXHR object, which exposes responseText. You can check out https://api.jquery.com/jquery.ajax/ for more details.

Note: The above example WILL return an error (since we are not providing an API key), but the response format is the same.

To actually convert an XMLDocument to a string, you would have to use a serializer (see Convert xml to string with jQuery).

Community
  • 1
  • 1
zabrador
  • 11
  • 2
  • I used your code and used `sensor=false` instead of the API key (I do have an API key though). When I followed it with `alert(typeof text);` the alert text said `undefined`. Is this supposed to happen? – Mike Mar 31 '16 at 22:59
  • Here is my code in case you are wondering (the URL works fine when typed into a URL): `var response = $.get('https://maps.googleapis.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false');` `var text = response.responseText;` `alert(text);` – Mike Mar 31 '16 at 23:33
0

The other guy's answer didn't work for me for some reason. But using the code structure here did: How do I get the entire XML string from a XMLDocument returned by jQuery (cross browser)?

I simply replaced the url with the Google API's (and replaced the dataType to XML as the answer suggested).

Community
  • 1
  • 1
Mike
  • 961
  • 6
  • 19
  • 43