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.

4
  • Store it where? You know that javascript has limited capabilities for data persistence, don't you? Commented Mar 30, 2016 at 19:30
  • In a string variable. And I did not know that. I do not have much experience with Javascript. Commented Mar 30, 2016 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 Commented Mar 31, 2016 at 0: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. Commented Mar 31, 2016 at 23:05

2 Answers 2

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).

Sign up to request clarification or add additional context in comments.

2 Comments

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?
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);
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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.