Goeff27
Goeff27

Reputation:

How can I retrieve the name of the user's current city?

I would like to retrieve an iPhoneOS user's current city. How can I do that?

Upvotes: 2

Views: 454

Answers (3)

Neeraj Neeru
Neeraj Neeru

Reputation: 570

see CLLocationManager class

- (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"];


 }];



}

see the delegate class for CLPlacemark, it provides a set of information about current location.

Upvotes: 0

dlamblin
dlamblin

Reputation: 45321

You do an IP to location lookup. This usually involves a database, like MaxMind's ones.

Well, I have an iPod Touch, so I forgot that the iPhone might not have an IP mapped to a city at the time (my home ip is static, so that helps too). You might also read the Core Location docs. As far as I see the CLLocation class only communicates in Latitude, Longitude, Altitude, Heading, and Speed. You would need to have your own RTree structure of rough city boundaries to be able to make a determination, or use a web-service call, possibly to Google Maps, or GeoNames' FindNearbyPlaceName web service (St. Gallen Example).

Upvotes: 2

Koekiebox
Koekiebox

Reputation: 5963

Have a look at:

  • IPhone SDK 3.0 and iPhone Dev SDK

    Upvotes: 0

  • Related Questions