【问题标题】:Find a location with geocoder but only using city, not using street使用地理编码器查找位置,但仅使用城市,而不使用街道
【发布时间】:2014-06-16 14:25:47
【问题描述】:

例如:

var city, country;

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

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

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

【问题讨论】:

  • 你从哪里得到 var city?
  • 看来您应该解决输入错误的问题。代码示例没有向我们展示值的定义位置。

标签: javascript maps geocode


【解决方案1】:
   //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;
                }
              }
            }
          }
        }

【讨论】:

  • 我有一个输入字符串 (=var city) 我必须找到那个城市。但是如果有人在输入字段中输入一条街道,那应该不会给出任何结果,但我无法检查字符串是街道还是城市......但如果我检查这个:if(city == super_var1[j] .long_name) { 做事; } 那应该做检查,不是吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多