3

OK, I am taking from an example I found, but my brain is fried from the heat, and google api's always do something to me, anyway

function codeLatLng(lat, lng)
{
    var latlng = new google.maps.LatLng(lat, lng);
    geocoder.geocode({'latLng': latlng}, function(results, status)
    {
        if (status == google.maps.GeocoderStatus.OK)
        {
            //console.log(results)
            if(results[1])
            {
                //formatted address
                //alert(results[0].formatted_address)
                //find country name
                $('#geolocation_latlng').html(latlng);
                for(var i=0; i<results[0].address_components.length; i++)
                {
                    for(var b=0;b<results[0].address_components[i].types.length;b++)
                    {

                        //there are different types that might hold a city admin_area_lvl_1 usually does in come cases looking for sublocality type will be more appropriate
                        if(results[0].address_components[i].types[b] == "administrative_area_level_1")
                        {
                            //this is the object you are looking for
                            city=results[0].address_components[i];
                            break;
                        }
                    }
                }
            //city data
            //alert(city.short_name + " " + city.long_name)
            }
            else
            {
                alert("Could not Determin Location");
            }
      }
      else
      {
          alert("Location dection failed: " + status);
      }
    });
}

Its safe to assume the lat and lon are already being passed and I am getting a result, I commented out one alert for formated_address, What I am trying to do is figure out how I can take the zipcode, state, city from that formatted address and show it on screen. But like I said Im fried right now, and Im crunching a clock so I need a bit of help to kick start me.

4

1 回答 1

2

My demo is comical collection of braces, but the idea is to look inside the results.types for:

 "locality"                     // CITY
 "administrative_area_level_1"  // STATE
 "postal_code"                  // ZIP

And as iterating, when you match these types, fill in the values. I asked for the country because this approach is explained in the docs to correspond to the US, but I have no idea, say if it's valid in Singapore. As they say, it's not an exact science.

So if you expand, you might have to check for country first, and match to another wantedTypes object with different correspondences to Google Maps' types (in another country the state might be Admin. area level 2).

I use the short name because most likely you want AZ and not Arizona. I didn't go as far to check if cities differ, e.g. St. Paul (short) vs. Saint Paul.

    if (status == google.maps.GeocoderStatus.OK) {
        if (results[0]) {
            var typeCorrespondence = {
                "city": "locality",
                "state": "administrative_area_level_1",
                "zipcode": "postal_code"
            };

            var geocoderResults = {};

            var components = results[0].address_components;

            for (var i = 0; i < components.length; i++) {
                for (var j = 0; j < components[i].types.length; j++) {

                    for (myType in typeCorrespondence) {
                        if (typeCorrespondence[myType] == components[i].types[j]) {
                            geocoderResults[myType] = components[i].short_name;
                        }
                    }
                }
            }
            $("#result").val(JSON.stringify(geocoderResults));
        }
    }
于 2012-07-09T01:24:58.090 回答