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

Javascript 使用地理编码器查找位置,但仅使用城市,不使用街道,javascript,maps,geocode,Javascript,Maps,Geocode,例如: var city, country; geocoder.geocode( {'address': city + ', ' + country}, function(results, status) {} ); 问题在于varcity错误并且是随机街道名称(在随机城市中) 他在结果中列出了这个位置,但我只想要城市 有没有办法只使用geocode()搜索城市?//从lat和long获取城市和州等地址信息 //get address info such as city an

例如:

var city, country;

geocoder.geocode(
     {'address': city + ', ' + country}, function(results, status) {}
);
问题在于var
city
错误并且是随机街道名称(在随机城市中) 他在结果中列出了这个位置,但我只想要城市

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

//从lat和long获取城市和州等地址信息
   //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;
                }
              }
            }
          }
        }
geocoder.geocode({'latLng':latLng},函数(结果,状态) { if(status==google.maps.GeocoderStatus.OK) { //将三维数组分解为更简单的数组 对于(i=0;i
您从何处获得var city?似乎您应该解决输入错误的问题。代码示例没有显示值的定义位置。我有一个输入字符串(=var city)恩,我必须找到那个城市。但是如果有人把一条街道放在输入字段中,那应该不会给出结果,但是我不能去检查字符串是一条街道还是一个城市……但是如果我检查这个:if(city==super_var1[j].long_name){do stuff;}应该做检查,不是吗?