選擇中國城市(省/市)
源代碼:https://github.com/thanwork/citychoose
主要功能
- 搜索列表中展示的中國所有城市
- 基于定位的城市選擇
- 熱點城市展示(隨便寫了9個城市诀姚。构蹬。。)
IMG_0052.png
代碼實現(xiàn)
寫代碼之前
由于定位使用的是高德定位API隅要,需要先在高德開發(fā)者網(wǎng)站上申請apikey;
所有view使用代碼實現(xiàn)谊却,并未采用storyboard創(chuàng)建古涧,而是使用snapkit
代碼實現(xiàn)
一個tableview+2個cell
-
tableview的實現(xiàn)
class CityChooseView : UIViewController,UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate,CityCellDelegate
主要就是實現(xiàn)一個tableview,同時在這個view中實現(xiàn)定位獲取的城市信息
func fetchLocation() {
aMapLocationManager.requestLocationWithReGeocode(true) { (alocation, regeocode, error) -> Void in
if(regeocode != nil){
self.locationcity = regeocode.city
self.locationprovince = regeocode.province
self.tableView.reloadData()
}
}
}
- 實現(xiàn)兩個tableviewcell侨艾,一個展示定位城市和熱點城市执虹,另一個列表方式展示所有的中國城市
使用方法
-
配置高德apikey
AMapLocationServices.sharedServices().apiKey = ""
集成snapkit,最好使用cocoapod
實現(xiàn)接口CityChooseViewDelegate
獲取國家(中國)唠梨,省袋励,市