Here is the formatted function. You can see a working example at
http://neogismap.appspot.com/samples/geocode/geocode.html
Bye
function codeLatLng() {
var input = document.getElementById("latlng").value;
var latlngStr = input.split(",",2);
var lat = parseFloat(latlngStr[0]);
var lng = parseFloat(latlngStr[1]);
var latlng = new google.maps.LatLng(lat, lng);
if (geocoder) {
geocoder.geocode({'latLng': latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results.length>0) {
var country = "";
for (i=0; i<results.length; i++) {
var res = results[i];
for (j=0; j<res.types.length; j++) {
if (res.types[j]=="country") {
country =
res.address_components[0].long_name;
};
}
}
alert(country);
} else {
alert("No results found");
}
} else {
alert("Geocoder failed due to: " + status);
}
});
}
}
On Jun 7, 3:05 pm, FLX <[email protected]> wrote:
> Hi David,
>
> Could you please pastebin it so it won't mess up formatting?
>
> Thanks,
>
> Dennis
>
> On Jun 7, 11:41 am, Davide Cremonesi <[email protected]> wrote:
>
>
>
> > Hi there, here is a modified version of the codeLatLng function which does
> > the recursive parsing.
> > bye,
> > Davide
> > *************************************
> > *function* codeLatLng() {
>
> > *var* input = document.getElementById("latlng").value;
>
> > *var* latlngStr = input.split(",",2);
>
> > *var* lat = parseFloat(latlngStr[0]);
>
> > *var* lng = parseFloat(latlngStr[1]);
>
> > *var* latlng = *new* google.maps.LatLng(lat, lng);
>
> > *if* (geocoder) {
>
> > geocoder.geocode({'latLng': latlng}, *function*(results, status) {
>
> > *if* (status == google.maps.GeocoderStatus.OK) {
>
> > *if* (results.length>0) {
>
> > *var*country= "";
>
> > *for* (i=0; i<results.length; i++) {
>
> > *var* res = results[i];
>
> > *for* (j=0; j<res.types.length; j++) {
>
> > *if* (res.types[j]=="country") {
>
> >country= res.address_components[0].long_name;
>
> > };
> > }
> > }
>
> > alert(country);
>
> > } *else* {
>
> > alert("No results found");
>
> > }
> > } *else* {
>
> > alert("Geocoder failed due to: " + status);
>
> > }
> > });
> > }
> > }
>
> > *****************************************************************
>
> > 2010/6/7 Davide Cremonesi <[email protected]>
>
> > > Hi there,
> > > unfortunately you cannot access thecountrydirectly as you would like.
> > > You have to parse the address_component array recursively, looking for an
> > > entry whose types array contains "country" and take its long_name or
> > > short_name as you prefer.
> > > If you inspect the results object in Firebug you will understand better
> > > what I mean.
> > > Hope it helps,
> > > Davide
>
> > > 2010/6/6 hero <[email protected]>
>
> > > hi
> > >> iam trying to mainpulate this example to return thecountryname where
> > >> my marker is set :
>
> > >>http://code.google.com/apis/maps/documentation/javascript/examples/ge...
> > >> iam trying the following:
>
> > >> alert(results[1].address_component[country]); but it is not
> > >> working
>
> > >> and here`s the code :
>
> > >> function codeLatLng() {
> > >> var input = document.getElementById("latlng").value;
> > >> var latlngStr = input.split(",",2);
> > >> var lat = parseFloat(latlngStr[0]);
> > >> var lng = parseFloat(latlngStr[1]);
> > >> var latlng = new google.maps.LatLng(lat, lng);
> > >> if (geocoder) {
> > >> geocoder.geocode({'latLng': latlng}, function(results, status)
> > >> {
> > >> if (status == google.maps.GeocoderStatus.OK) {
> > >> if (results[1]) {
> > >> alert(results[1].address_component[country]);
> > >> } else {
> > >> alert("No results found");
> > >> }
> > >> } else {
> > >> alert("Geocoder failed due to: " + status);
> > >> }
> > >> });
> > >> }
> > >> }
>
> > >> any help is really appreciated, thanks a lot
>
> > >> --
> > >> You received this message because you are subscribed to the Google Groups
> > >> "Google Maps JavaScript API v3" group.
> > >> To post to this group, send email to
> > >> [email protected].
> > >> To unsubscribe from this group, send email to
> > >> [email protected]<google-maps-js-api-v3%2b[email protected]>
> > >> .
> > >> For more options, visit this group at
> > >>http://groups.google.com/group/google-maps-js-api-v3?hl=en.
>
> > > Hi- Hide quoted text -
>
> - Show quoted text -
--
You received this message because you are subscribed to the Google Groups
"Google Maps JavaScript API v3" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-maps-js-api-v3?hl=en.