我对一对坐标进行反向地理编码以找到用户所在的城市,但我很难将城市变为Motion-Kit布局。什么是让城市进入布局的最佳途径?我也会将API中的其他信息添加到布局中,以便可能会遇到同样的问题。在Motion-Kit布局中使用异步获取的数据
有一个基本的MK布局是这样的:
class HomeLayout < MK::Layout
def initialize(data)
@city = data[:city]
super
end
def layout
add UILabel, :location
end
def location_style
text @city
color :white.uicolor
font UIFont.fontWithName("Avenir", size: 22)
size_to_fit
center ['50%', 80]
end
end
我得到@city
此方法在HomeScreen
:
def city
loc = CLLocation.alloc.initWithLatitude App::Persistence['latitude'], longitude: App::Persistence['longitude']
geo = CLGeocoder.new
geo.reverseGeocodeLocation loc, completionHandler: lambda { |result, x|
return result[0].locality
}
# This currently returns a CLGeocoder object, but I want it to return the city as a String.
end
我得到App::Persistence['latitude']
从on_activate
在AppDelegate中,像这样:
def on_activate
BW::Location.get_once do |result|
if result.is_a?(CLLocation)
App::Persistence['latitude'] = result.coordinate.latitude
App::Persistence['longitude'] = result.coordinate.longitude
open HomeScreen.new
else
LocationError.handle(result[:error])
end
end
end
任何帮助将升值ated。提前致谢。