geocode - google maps geocoder to return state -
i using google maps geocoder geocode zipcode , want return state zipcode located in , store in variable "local". getting error indicating local undefined. why?
see code below:
var address=document.getelementbyid("address").value; var radius=document.getelementbyid("radius").value; var latitude=40; var longitude=0; var local; geocoder.geocode( { 'address': address}, function(results, status){ if (status==google.maps.geocoderstatus.ok){ latlng=(results[0].geometry.location); latitude=latlng.lat(); longitude=latlng.lng(); //store state abbreviation in variable local local=results[0].address_components.types.adminstrative_area_level_1; } else{ alert("geocode not successful following reason: " + status); } });
i think issue address_components have more 1 component, , order isn't same zip codes. have iterate through results find correct one.
<html xmlns="http://www.w3.org/1999/xhtml"> <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> <script type="text/javascript"> var geocoder = new google.maps.geocoder(); function test() { var address=document.getelementbyid("address").value; var local = document.getelementbyid("local"); var latitude=40; var longitude=0; geocoder.geocode( { 'address': address}, function(results, status) { if (status==google.maps.geocoderstatus.ok) { latlng=(results[0].geometry.location); latitude=latlng.lat(); longitude=latlng.lng(); //store state abbreviation in variable local for(var ix=0; ix< results[0].address_components.length; ix++) { if (results[0].address_components[ix].types[0] == "administrative_area_level_1") { local.value=results[0].address_components[ix].short_name; } } } else { alert("geocode not successful following reason: " + status); } }); } </script> </head> <body> <input type='text' id='address' value='84102' /> <input type='text' id='local' value='' /> <a href='#' onclick="test();" >try</a> </body> </html>
Comments
Post a Comment