0

한 모델에서 두 개의 주소를 지오 코딩합니다. 여행에는 StartAddress와 EndAddress가 있습니다. StartAddress start_longtitude start_lattitude EndAddress end_longtitude 나는 인근)이 인근에 시작 여행을 난을 찾기 위해 함수를 호출 할 수 있도록하나의 모델에서 두 개의 주소를 지오 코딩

가 어떻게 그것을 지오 코딩 할 수 end_lattitude : 따라서, 나는 나의 모델에 대한 세 가지 특성을 가지고 주어진 여정과 ii) 특정 여정을 끝내는 여정? 도움이된다면 크게 환영합니다

답변

0

문제와 해결책은 here으로 표현됩니다.

문제를 해결하려면 before_save 및 해당 방법을 모델에 추가하십시오. 두 번째 위치 (어쩌면 목적지)의 코드 부분을 반복하는 것을 잊지 마십시오.

before_save :geocode_endpoints 

    private 
    #To enable Geocoder to works with multiple locations 
    def geocode_endpoints 
    if from_changed? 
     geocoded = Geocoder.search(loc1).first 
     if geocoded 
     self.latitude = geocoded.latitude 
     self.longitude = geocoded.longitude 
     end 
    end 
    # Repeat for destination 
     if to_changed? 
     geocoded = Geocoder.search(loc2).first 
     if geocoded 
     self.latitude2 = geocoded.latitude 
     self.longitude2 = geocoded.longitude 
     end 
    end 
    end