使用地理编码器查找位置但仅使用城市,而不是使用街道

时间:2014-06-16 14:25:47

标签: javascript maps geocode

例如:

var city, country;

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

问题是当var city错误并且是随机的街道名称(在随机城市中) 他把这个位置放在结果中,但我只想要城市......

有没有办法只使用geocode()搜索城市?

1 个答案:

答案 0 :(得分: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;
                }
              }
            }
          }
        }