如何检索用户当前城市的名称?

时间:2009-09-05 09:11:09

标签: iphone

我想检索iPhoneOS用户当前的城市。我怎么能这样做?

3 个答案:

答案 0 :(得分:2)

您执行IP到位置查找。这通常涉及数据库,如MaxMind's个。

好吧,我有一台iPod Touch,所以我忘记了当时iPhone可能没有映射到城市的IP(我的家庭IP是静态的,所以这也有帮助)。您可能还会阅读Core Location docs。据我所知,CLLocation课程仅以纬度,经度,海拔高度,航向和速度进行通信。您需要拥有自己的粗糙城市边界的RTree结构才能确定或使用网络服务电话,可能是Google MapsGeoNames' FindNearbyPlaceName网络服务(St. Gallen Example)。

答案 1 :(得分:0)

答案 2 :(得分:0)

参见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的委托类,它提供了一组有关当前位置的信息。