【TUYA SDK集成八十一难】iOS智能场景海外用户如何获取cityId

App开发相关产品技术讨论,包括OEM App、App SDK、设备控制界面开发、小程序开发等话题


Post Reply
taojingGino
Posts: 23

问题:创建场景条件对象时,需要使用 TuyaSmartCityModel 中的 cityId 值作为定位信息,可是文档中查询城市列表的接口(getCityListWithCountryCode)里只返回了中国大陆地区的城市,那海外的城市如何获得cityId呢?

解决方案
1.你可以通过系统方法获取当前用户经纬度

Code: Select all

// 初始化并开始更新
self.locManager = [[CLLocationManager alloc] init];
self.locManager.delegate = self;
self.locManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locManager.distanceFilter = 5.0;
[self.locManager startUpdatingLocation];


// 代理方法实现
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
	//这里获取当前用户经纬度
  	NSLog(@"%f,%f",newLocation.coordinate.latitude,newLocation.coordinate.longitude);
} 
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
	NSLog(@"%@",error);
}

2.然后通过经纬度获取TuyaSmartCityModel,然后获取TuyaSmartCityModel中的cityId

Code: Select all

[[TuyaSmartSceneManager sharedInstance] getCityInfoWithLatitude:@"your_location_latitude" longitude:@"your_location_longitude" success:^(TuyaSmartCityModel *city) {
  	NSLog(@"get city info success:%@", city);
} failure:^(NSError *error) {
  	NSLog(@"get city info failure:%@", error);
}];

原理:因为全球城市数据较难维护,很多开发者经常使用了getCityListWithCountryCode,而忽略了海外用户的定位,这里我们推荐使用经纬度来查询城市信息。

Post Reply