【发布时间】:2010-11-25 20:28:37
【问题描述】:
我想检索一个 iPhoneOS 用户的当前城市。我该怎么做?
【问题讨论】:
标签: iphone
我想检索一个 iPhoneOS 用户的当前城市。我该怎么做?
【问题讨论】:
标签: iphone
您执行 IP 到位置查找。这通常涉及一个数据库,例如 MaxMind's 。
好吧,我有一个 iPod Touch,所以我忘记了当时 iPhone 可能没有映射到城市的 IP(我的家庭 IP 是静态的,所以这也有帮助)。您还可以阅读Core Location docs。据我所知,CLLocation 类仅以纬度、经度、高度、航向和速度进行通信。您需要拥有自己的粗略城市边界的 RTree 结构才能做出决定,或使用网络服务调用,可能是 Google Maps 或 GeoNames' FindNearbyPlaceName 网络服务 (St. Gallen Example)。
【讨论】:
【讨论】:
参见 CLLocationManager 类
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
if (newLocation) {
longitudeLabel.text=[NSString stringWithFormat:@"%f",newLocation.coordinate.longitude ];
latitudeLabel.text=[NSString stringWithFormat:@"%f",newLocation.coordinate.latitude ];
}
CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
[geoCoder reverseGeocodeLocation: locationManager.location completionHandler:
^(NSArray *placemarks, NSError *error) {
placemark = [placemarks objectAtIndex:0];
//the below code gives the city name
NSString *city=placemark.locality;
NSLog(@"I am currently at %@",city);
//String to hold address
NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
NSLog(@"I am currently at %@",locatedAt);
NSMutableArray *FullAddress=[placemark.addressDictionary valueForKey:@"FormattedAddressLines"];
//gives the state
NSString * state=[placemark administrativeArea];
country=[placemark.addressDictionary valueForKey:@"Country"];
CountryCode=[placemark.addressDictionary valueForKey:@"CountryCode"];
Name=[placemark.addressDictionary valueForKey:@"Name"];
State1=[placemark.addressDictionary valueForKey:@"State"];
Street=[placemark.addressDictionary valueForKey:@"Street"];
}];
}
查看 CLPlacemark 的委托类,它提供了一组关于当前位置的信息。
【讨论】: