0

for example:

var city, country;

geocoder.geocode(
     {'address': city + ', ' + country}, function(results, status) {}
);

the problem is when the var city is wrong and is a random streetname (in a random city) he puts this location in results, but I only want cities...

Is there a way to use geocode() only for searching a city?

Anto Jurković
  • 11,188
  • 2
  • 29
  • 42

1 Answers1

0
   //get address info such as city and state from lat and long
      geocoder.geocode({'latLng': latlng}, function(results, status) 
      {
        if (status == google.maps.GeocoderStatus.OK) 
        {
          //break down the three dimensional array into simpler arrays
          for (i = 0 ; i < results.length ; ++i)
          {
            var super_var1 = results[i].address_components;
            for (j = 0 ; j < super_var1.length ; ++j)
            {
              var super_var2 = super_var1[j].types;
              for (k = 0 ; k < super_var2.length ; ++k)
              {
                //find city
                if (super_var2[k] == "locality")
                {
                  //put the city name in the form
                  main_form.city.value = super_var1[j].long_name;
                }
                //find county
                if (super_var2[k] == "administrative_area_level_2")
                {
                  //put the county name in the form
                  main_form.county.value = super_var1[j].long_name;
                }
                //find State
                if (super_var2[k] == "administrative_area_level_1")
                {
                  //put the state abbreviation in the form
                  main_form.state.value = super_var1[j].short_name;
                }
              }
            }
          }
        }
  • I have an input String (=var city) en I have to find that city. but if someone puts a street in the input field, that should give no results but I can't go check if the String is a street or a city... but if I check this: if(city == super_var1[j].long_name) { do stuff; } that should do the check, no? – user3745218 Jun 16 '14 at 16:41