私はRuby on Railsが初めてで、まだメソッドを書くことができません....
exif データを含まない画像をアップロードすると、エラーが発生します。
imgfile と imgfile.nil の両方を許可するには、このメソッドが必要です。imfile.nil の場合、デフォルト値を定義するか、このコードを無視するにはどうすればよいですか?
# Image geolocation
def get_image_loc
imgfile = EXIFR::JPEG.new(image.queued_for_write[:original].path)
return unless imgfile
lat = imgfile.exif[0].gps_latitude[0].to_f + (imgfile.exif[0].gps_latitude[1].to_f / 60) + (imgfile.exif[0].gps_latitude[2].to_f / 3600)
lng = imgfile.exif[0].gps_longitude[0].to_f + (imgfile.exif[0].gps_longitude[1].to_f / 60) + (imgfile.exif[0].gps_longitude[2].to_f / 3600)
lat = lat * -1 if imgfile.exif[0].gps_latitude_ref == "S" # (N is +, S is -)
lng = lng * -1 if imgfile.exif[0].gps_longitude_ref == "W" # (W is -, E is +)
self.img_loc_lat = lat # imgfile.gps_latitude
self.img_loc_lng = lng # imgfile.gps_longitude
end
end
これが私のshow.erbファイルでこのメソッドを呼び出す部分です
<% unless @pin.img_loc_lat.blank?
# bespoke code to load a map if the location information is available
# Google Static Maps for now
# Look to upgrade to Google Maps API 3 - there seem to be a few gems available for it
%>
<p> <br />
<img src="http://maps.googleapis.com/maps/api/staticmap?center=<%= @pin.img_loc_lat %>,<%= @pin.img_loc_lng %>&zoom=13&size=600x300&maptype=roadmap&markers=color:green%7Clabel:P%7C<%= @pin.img_loc_lat %>,<%= @pin.img_loc_lng %>&sensor=false">
</p>
<% end %>
私のギット