iPhone - Get City name from Latitude and Longtiude

前端 未结 10 908
难免孤独
难免孤独 2020-12-05 06:04

I want get my Current City name in my iPhone App.

I\'m currently getting the latitude and longitude using CLLocationManager and than i am passing my coordinates into

相关标签:
10条回答
  • 2020-12-05 06:05

    I found this code and it worked for me: https://gist.github.com/flyworld/4222448.

    Just change placemark.ISOcountryCode to placemark.locality.

    0 讨论(0)
  • 2020-12-05 06:10

    Try this request here you will find all data about the current location, street/city/region name, house number but for your request just paste this.

    NSString *urlString = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=false",pdblLatitude, pdblLongitude];
    NSError* error;
    NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSASCIIStringEncoding error:&error];
    
    NSData *data = [locationString dataUsingEncoding:NSUTF8StringEncoding];
    id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    
    NSDictionary *dic = [[json objectForKey:kResults] objectAtIndex:0];
    NSString *cityName = [[[dic objectForKey:@"address_components"] objectAtIndex:1] objectForKey:@"long_name"];
    
    0 讨论(0)
  • 2020-12-05 06:11

    Please check the bellow function

    func getDataCity(tmpLat:Double,tmpLong:Double) {
    
        let tmpCLGeocoder = CLGeocoder.init()
        if tmpLat > 0 , tmpLong > 0
        {
            let tmpDataLoc = CLLocation.init(latitude: tmpLat, longitude: tmpLong)
    
            tmpCLGeocoder.reverseGeocodeLocation(tmpDataLoc, completionHandler: {(placemarks,error) in
    
                guard let tmpPlacemarks = placemarks else{
                    return
                }
                let placeMark = tmpPlacemarks[0] as CLPlacemark
    
                guard let strLocality = placeMark.locality else{
                    return
                }
                // strLocality is your city
                guard let strSubLocality = placeMark.subLocality else{
    
                    return
                }
                // strSubLocality is your are of city
            })
        }
    }
    
    0 讨论(0)
  • 2020-12-05 06:14

    This is fine, worked for me.

    I'm getting the latitude and longitude using CLLocationManager and than i am passing my coordinates into CLGeocoder.

    import @corelocation  and for getting city,country #import <AddressBook/AddressBook.h>
        -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
        {
        CLLocation *location=[locations lastObject];
            CLGeocoder *geocoder=[[CLGeocoder alloc]init];
    
            CLLocationCoordinate2D coord;
            coord.longitude = location.coordinate.longitude;
            coord.latitude = location.coordinate.latitude;
            // or a one shot fill
            coord = [location coordinate];
            NSLog(@"longitude value%f", coord.longitude);
            NSLog(@"latitude value%f", coord.latitude);
            [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
                CLPlacemark *placemark = placemarks[0];
                NSDictionary *addressDictionary = [placemark addressDictionary];
                city = addressDictionary[(NSString *)kABPersonAddressCityKey];
                stateloc = addressDictionary[(NSString *)kABPersonAddressStateKey];
                country = placemark.country;
    
    
                NSLog(@"city%@/state%@/country%@",city,stateloc,country);
               [self getImagesFromServer:city];
    
            }];
    
            [self stopSignificantChangesUpdates];
    
        }
    
    - (void)stopSignificantChangesUpdates
    {
        [self.locationManager stopUpdatingLocation];
        self.locationManager = nil;
    }
    
    0 讨论(0)
  • 2020-12-05 06:17

    I am using this and getting the ZIP code and City Name. Modified the method added by Janak.

    - (void) getAddressFromLatLon:(CLLocation *)bestLocation
    {
        NSLog(@"%f %f", bestLocation.coordinate.latitude, bestLocation.coordinate.longitude);
        CLGeocoder *geocoder = [[CLGeocoder alloc] init] ;
        [geocoder reverseGeocodeLocation:bestLocation
                       completionHandler:^(NSArray *placemarks, NSError *error)
        {
            if (error){
                NSLog(@"Geocode failed with error: %@", error);
                return;
            }
            CLPlacemark *placemark = [placemarks objectAtIndex:0];
            NSLog(@"placemark.ISOcountryCode %@",placemark.ISOcountryCode);
            NSLog(@"locality %@",placemark.locality);
            NSLog(@"postalCode %@",placemark.postalCode);
    
        }];
    
    }
    
    0 讨论(0)
  • 2020-12-05 06:18

    I improved @Constantin Saulenco great answer- the json results in not always ordered in same order - so city is not always at the same index - this func will search for the right one. Added country as well.

    NSString *urlString = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=false",location.coordinate.latitude, location.coordinate.longitude];
    NSError* error;
    NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSASCIIStringEncoding error:&error];
    
    NSData *data = [locationString dataUsingEncoding:NSUTF8StringEncoding];
    id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    
    NSDictionary *dic = [[json objectForKey:@"results"] objectAtIndex:0];
    NSArray* arr = [dic objectForKey:@"address_components"];
    //Iterate each result of address components - find locality and country
    NSString *cityName;
    NSString *countryName;
    for (NSDictionary* d in arr)
    {
        NSArray* typesArr = [d objectForKey:@"types"];
        NSString* firstType = [typesArr objectAtIndex:0];
        if([firstType isEqualToString:@"locality"])
            cityName = [d objectForKey:@"long_name"];
        if([firstType isEqualToString:@"country"])
            countryName = [d objectForKey:@"long_name"];
    
    }
    
    NSString* locationFinal = [NSString stringWithFormat:@"%@,%@",cityName,countryName];
    
    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题