簡體   English   中英

匹配zip和geocode在rails4中獲取太多數據

[英]Matching zip with geocode fetch too many data in rails4

嗨,我已將其包含在我的gemfile中:

gem 'geokit'
gem 'geokit-rails'

並在我的模型中編寫此代碼

class Contact < ActiveRecord::Base
  validate :zip_and_city_match
  def zip_and_city_match
    p "aaaaaaaaaaaaaa"
    geo = Geokit::Geocoders::MultiGeocoder.geocode(self.zip)
    p 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
    p geo   
    p 'city'
    p geo.city     
    p "sssssssssssssssssssssss"        
  end
end

所以當我給contact_attributes"=>{"current_address1"=>"test1dinsh", "current_address2"=>"awwwwwwwww", "country"=>"IN", "city"=>"Indore", "zip"=>"452001"}然后它給了我這種類型的輸出:

"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
#<Geokit::GeoLoc:0xbec3078 @all=[#<Geokit::GeoLoc:0xbec3078 ...>, #<Geokit::GeoLoc:0xbec83e8 @all=[#<Geokit::GeoLoc:0xbec83e8 ...>], @street_address=nil, @sub_premise=nil, @street_number=nil, @street_name=nil, @city=nil, @state=nil, @state_code="Republic of Bashkortostan", @state_name="Republic of Bashkortostan", @zip="452001", @country_code="RU", @province="Republic of Bashkortostan", @success=true, @precision="city", @full_address="Republic of Bashkortostan, Russia, 452001", @lat=54.1245735, @lng=54.1369126, @provider="google", @district="Belebeyevsky District", @country="Russia", @accuracy=4, @suggested_bounds=#<Geokit::Bounds:0xbec30f0 @sw=#<Geokit::LatLng:0xbec312c @lat=54.098381, @lng=54.110801>, @ne=#<Geokit::LatLng:0xbec31f4 @lat=54.1550891, @lng=54.158854>>>, #<Geokit::GeoLoc:0xbec92fc @all=[#<Geokit::GeoLoc:0xbec92fc ...>], @street_address=nil, @sub_premise=nil, @street_number=nil, @street_name=nil, @city="Indore", @state=nil, @state_code="MP", @state_name="Madhya Pradesh", @zip="452001", @country_code="IN", @province="MP", @success=true, @precision="city", @full_address="Indore, Madhya Pradesh 452001, India", @lat=22.7081955, @lng=75.8824422, @provider="google", @district="Indore", @country="India", @accuracy=4, @suggested_bounds=#<Geokit::Bounds:0xbec8438 @sw=#<Geokit::LatLng:0xbec844c @lat=22.6845079, @lng=75.8606602>, @ne=#<Geokit::LatLng:0xbec84ec @lat=22.7359325, @lng=75.91539259999999>>>], @street_address="Mičurinova", @sub_premise=nil, @street_number=nil, @street_name="Mičurinova", @city="Subotica", @state=nil, @state_code="Vojvodina", @state_name="Vojvodina", @zip="452001", @country_code="RS", @province="Vojvodina", @success=true, @precision="street", @full_address="Mičurinova, Subotica 452001, Serbia", @lat=46.0988609, @lng=19.6422459, @provider="google", @district="Severnobački okrug", @country="Serbia", @accuracy=7, @suggested_bounds=#<Geokit::Bounds:0xbec1bc4 @sw=#<Geokit::LatLng:0xbec1bec @lat=46.0973894197085, @lng=19.6407217697085>, @ne=#<Geokit::LatLng:0xbec1c3c @lat=46.1000873802915, @lng=19.6434197302915>>, @neighborhood="Novo Selo">
'city'
"Subotica"
"sssssssssssssssssssssss"

請指導我為什么給這個城市我將如何獲取正確的城市,即我給了印多爾然后它必須與它匹配。 提前致謝

試試這個,給我正確的城市和州。

require 'geokit'
geo = GeoKit::Geocoders::MultiGeocoder.geocode('Indore, 452001')
#or 
geo = GeoKit::Geocoders::MultiGeocoder.geocode('India, 452001')
if geo.success
  geo.state # => MP
  geo.city  # => Indore
end

您不應該只使用郵政編碼,您可以嘗試國家和郵政編碼,因為您可以在其他國家/地區找到相同的郵政編碼。 它可能會返回多個或錯誤。

請參閱geokit gem自述文件的多個結果部分。 正如它所述,對於不明確的查詢,您可以獲得多個結果。 這就是當您通過單個zipcode請求結果時會發生的情況:

geo = Geokit::Geocoders::MultiGeocoder.geocode('452001')
geo.all.map{|i| i.full_address}
# => ["Mičurinova, Subotica 452001, Serbia", "Republic of Bashkortostan, Russia, 452001", "Indore, Madhya Pradesh 452001, India"] 

如您所見,有三個不同的位置找到相同的zipcode 您可以手動選擇所需的那個:

geo.all.find{|i| i.country == 'India'}

或者您可以為地理編碼服務提供更多信息(如果可能):

def zip_and_city_match
  zip_and_country = self.zip + " " + self.country
  geo = Geokit::Geocoders::MultiGeocoder.geocode(zip_and_country)
  # => should return only one result
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM