我對一對座標進行反向地理編碼以找到用戶所在的城市,但我很難將城市變爲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。提前致謝。