iPhone - Get City name from Latitude and Longtiude

前端 未结 10 909
难免孤独
难免孤独 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:19
    - (void)reverseGeocode:(CLLocation *)location {
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
        NSLog(@"Finding address");
        if (error) {
            NSLog(@"Error %@", error.description);
        } else {
            CLPlacemark *placemark = [placemarks lastObject];
            self.myAddress.text = [NSString stringWithFormat:@"%@", ABCreateStringWithAddressDictionary(placemark.addressDictionary, NO)];
        }
    }];
    }
    
    0 讨论(0)
  • 2020-12-05 06:20

    It works for me :)

    CLGeocoder *ceo = [[CLGeocoder alloc]init];
    CLLocation *loc = [[CLLocation alloc]initWithLatitude:26.93611 longitude:26.93611];
    
    [ceo reverseGeocodeLocation: loc completionHandler:
     ^(NSArray *placemarks, NSError *error) {
         CLPlacemark *placemark = [placemarks objectAtIndex:0];
         NSLog(@"placemark %@",placemark);
         //String to hold address
         NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
         NSLog(@"addressDictionary %@", placemark.addressDictionary);
    
         NSLog(@"placemark %@",placemark.region);
         NSLog(@"placemark %@",placemark.country);  // Give Country Name
         NSLog(@"placemark %@",placemark.locality); // Extract the city name
         NSLog(@"location %@",placemark.name);
         NSLog(@"location %@",placemark.ocean);
         NSLog(@"location %@",placemark.postalCode);
         NSLog(@"location %@",placemark.subLocality);
    
         NSLog(@"location %@",placemark.location);
         //Print the location to console
         NSLog(@"I am currently at %@",locatedAt);
     }];
    
    0 讨论(0)
  • 2020-12-05 06:25

    As per documentation CLGeocoder does not work below iOS5. You need to take another route to support iOS4 and iOS5 both.

    You can look at MKReverseGeocoder, however it is deprecated in iOS5 but still will serve the purpose. For confirmation you can check so called question

    +(NSString *)getAddressFromLatLon:(double)pdblLatitude withLongitude:(double)pdblLongitude
    {
        NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%f,%f&output=csv",pdblLatitude, pdblLongitude];
        NSError* error;
        NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSASCIIStringEncoding error:&error];
        locationString = [locationString stringByReplacingOccurrencesOfString:@"\"" withString:@""];
        return [locationString substringFromIndex:6];
    }
    

    You can use this function for getting address from latitude, longitude. You can change according to requirement. We put this as Class method so we can directly use it as

    NSString *strAddressFromLatLong = [CLassName getAddressFromLatLon:37.484848 withLongitude:74.48489];
    

    EDIT

    Please stop using above function as it has stopped working reported in comments (Not tested by me). I recommend to start using SVGeocoder

    0 讨论(0)
  • 2020-12-05 06:28
    //Place below parser code where you are reading latlng and place your latlng in the url
    NSXMLParser *parser = [[NSXMLParser alloc]initWithContentsOfURL:[NSURL URLWithString:@"http://maps.googleapis.com/maps/api/geocode/xml?latlng=40.714224,-73.961452&sensor=false"]];
    [parser setDelegate:self];
    [parser parse];
    
    // Below are the delegates which will get you the exact address easyly
    -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
    {    
        if([elementName isEqualToString:@"formatted_address"]){
            got = YES; //got is a BOOL
        }
    }
    
    -(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
    {
        if(got){
            NSLog(@"the address is = %@",string);
        }
    }
    
    -(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
    }
    
    //what we are doing is using xmlparser to parse the data which we get through the google map api copy above link and use in browser you will see the xml data brought
    

    Sorry for my bad english hope it willhelp you

    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题