0

Check this code

//using the url: which fetches the xml data find the link here

<GeocodeResponse>
<status>OK</status>
−
<result>
<type>locality</type>
<type>political</type>
<formatted_address>Chennai, Tamil Nadu, India</formatted_address>
−
<address_component>
<long_name>Chennai</long_name>
<short_name>Chennai</short_name>
<type>locality</type>
<type>political</type>
</address_component>
−
<address_component>
<long_name>Chennai</long_name>
<short_name>Chennai</short_name>
<type>administrative_area_level_2</type>
<type>political</type>
</address_component>
−
<address_component>
<long_name>Tamil Nadu</long_name>
<short_name>TN</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
−
<address_component>
<long_name>India</long_name>
<short_name>IN</short_name>
<type>country</type>
<type>political</type>
</address_component>
−
<geometry>
−
<location>
<lat>13.0604220</lat>
<lng>80.2495830</lng>
</location>
<location_type>APPROXIMATE</location_type>
−
<viewport>
−
<southwest>
<lat>12.9734519</lat>
<lng>80.1215236</lng>
</southwest>
−
<northeast>
<lat>13.1473615</lat>
<lng>80.3776424</lng>
</northeast>
</viewport>
−
<bounds>
−
<southwest>
<lat>12.9459267</lat>
<lng>80.1487827</lng>
</southwest>
−
<northeast>
<lat>13.2340851</lat>
<lng>80.3322913</lng>
</northeast>
</bounds>
</geometry>
</result>
</GeocodeResponse> 

*In this following code ,unable to get the total no of nodes from google map, Can you please fetch the results *

$.ajax({
            type: "post",
            url: "http://maps.googleapis.com/maps/api/geocode/xml?address=chennai&sensor=false",
            dataType: "xml",
            success  : function(xmlData){
            var totalNodes = $('*',xmlData).length; // count XML nodes
            alert("This XML file has " + totalNodes);
            },
            error    : function(){
                 alert("Could not retrieve XML file.");
            }

          });

//the output will be This XML file has 20 or more nodes

Sam Arul Raj T
  • 1,752
  • 17
  • 23

2 Answers2

1

Your problem is this: you are trying to "POST" an ajax request to a domain that you don't own, which is impossible. You can only submit JSONP requests to external domains. The only solution you have if you want to use that endpoint is to create a proxy page on your server that will pass your request through to Google and will return the response back to your browser.

There is a native JS solution using methods provided via the Javascript API, which is detailed here: Google maps geocode API V3 not returning result in javascript function

Community
  • 1
  • 1
streetlogics
  • 4,640
  • 33
  • 33
-1

You aren't passing any API keys to Google Maps which I think you need to do for Google Maps API.

twilson
  • 2,062
  • 14
  • 19