我想从MapKit for iPhone中的Coordinate值获取Location名称

时间:2022-11-15 19:57:14

I want to get the location name from the coordinate value. Here is code,

我想从坐标值中获取位置名称。这是代码,

- (void)viewWillAppear:(BOOL)animated {  
    CLLocationCoordinate2D zoomLocation;
    zoomLocation.latitude = 39.281516;
    zoomLocation.longitude= -76.580806;
    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);
    MKCoordinateRegion adjustedRegion = [_mapView regionThatFits:viewRegion];                
    [_mapView setRegion:adjustedRegion animated:YES];      
}

So, From that Latitude and longitude , i want to know that location name.

所以,从纬度和经度来看,我想知道位置名称。

5 个解决方案

#1


77  

The Below code shall work in ios5 and above

以下代码适用于ios5及以上版本

 CLGeocoder *ceo = [[CLGeocoder alloc]init];
 CLLocation *loc = [[CLLocation alloc]initWithLatitude:32.00 longitude:21.322]; //insert your coordinates

[ceo reverseGeocodeLocation:loc
          completionHandler:^(NSArray *placemarks, NSError *error) {
              CLPlacemark *placemark = [placemarks objectAtIndex:0];
              if (placemark) {


                  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);
              }
              else {
                  NSLog(@"Could not locate");
              }
          }
 ];

#2


7  

-(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];
   // NSLog(@"%@",locationString);

  locationString = [locationString stringByReplacingOccurrencesOfString:@"\"" withString:@""];
  return [locationString substringFromIndex:6];
}

#3


3  

Use this method

使用此方法

CLGeocoder *geocoder = [[CLGeocoder alloc] init]; 

[geocoder reverseGeocodeLocation: zoomLocation completionHandler:^(NSArray* placemarks, NSError* error){
}
 ];

#4


1  

In iOS 5.0 and later, you can use CLGeocoder of Core Location framework, as for iOS lower than 5.0, MKReverseGeocoder of Map Kit Framework. Good luck!

在iOS 5.0及更高版本中,您可以使用Core Location框架的CLGeocoder,对于低于5.0的iOS,Map Kit Framework的MKReverseGeocoder。祝你好运!

#5


0  

Here is block to get address string from current location

这是从当前位置获取地址字符串的块

in .h file

typedef void(^addressCompletionBlock)(NSString *);

-(void)getAddressFromLocation:(CLLocation *)location complationBlock:(addressCompletionBlock)completionBlock;

in .m file
#pragma mark - Get Address
-(void)getAddressFromLocation:(CLLocation *)location complationBlock:(addressCompletionBlock)completionBlock
{
    //Example URL
    //NSString *urlString = @"http://maps.googleapis.com/maps/api/geocode/json?latlng=23.033915,72.524267&sensor=true_or_false";

    NSString *urlString = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=true_or_false",location.coordinate.latitude, location.coordinate.longitude];

    NSError* error;
    NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSASCIIStringEncoding error:&error];
    NSArray *jsonObject = [NSJSONSerialization JSONObjectWithData:[locationString dataUsingEncoding:NSUTF8StringEncoding]
                                                          options:0 error:NULL];

    NSString *strFormatedAddress = [[[jsonObject valueForKey:@"results"] objectAtIndex:0] valueForKey:@"formatted_address"];
    completionBlock(strFormatedAddress);
}

and to call function

并调用函数

CLLocation* currentLocation = [[CLLocation alloc] initWithLatitude:[SharedObj.current_Lat floatValue] longitude:[SharedObj.current_Long floatValue]];

    [self getAddressFromLocation:currentLocation complationBlock:^(NSString * address) {
        if(address) {
            NSLog(@"Address:: %@",address);
        }
    }];

#1


77  

The Below code shall work in ios5 and above

以下代码适用于ios5及以上版本

 CLGeocoder *ceo = [[CLGeocoder alloc]init];
 CLLocation *loc = [[CLLocation alloc]initWithLatitude:32.00 longitude:21.322]; //insert your coordinates

[ceo reverseGeocodeLocation:loc
          completionHandler:^(NSArray *placemarks, NSError *error) {
              CLPlacemark *placemark = [placemarks objectAtIndex:0];
              if (placemark) {


                  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);
              }
              else {
                  NSLog(@"Could not locate");
              }
          }
 ];

#2


7  

-(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];
   // NSLog(@"%@",locationString);

  locationString = [locationString stringByReplacingOccurrencesOfString:@"\"" withString:@""];
  return [locationString substringFromIndex:6];
}

#3


3  

Use this method

使用此方法

CLGeocoder *geocoder = [[CLGeocoder alloc] init]; 

[geocoder reverseGeocodeLocation: zoomLocation completionHandler:^(NSArray* placemarks, NSError* error){
}
 ];

#4


1  

In iOS 5.0 and later, you can use CLGeocoder of Core Location framework, as for iOS lower than 5.0, MKReverseGeocoder of Map Kit Framework. Good luck!

在iOS 5.0及更高版本中,您可以使用Core Location框架的CLGeocoder,对于低于5.0的iOS,Map Kit Framework的MKReverseGeocoder。祝你好运!

#5


0  

Here is block to get address string from current location

这是从当前位置获取地址字符串的块

in .h file

typedef void(^addressCompletionBlock)(NSString *);

-(void)getAddressFromLocation:(CLLocation *)location complationBlock:(addressCompletionBlock)completionBlock;

in .m file
#pragma mark - Get Address
-(void)getAddressFromLocation:(CLLocation *)location complationBlock:(addressCompletionBlock)completionBlock
{
    //Example URL
    //NSString *urlString = @"http://maps.googleapis.com/maps/api/geocode/json?latlng=23.033915,72.524267&sensor=true_or_false";

    NSString *urlString = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=true_or_false",location.coordinate.latitude, location.coordinate.longitude];

    NSError* error;
    NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSASCIIStringEncoding error:&error];
    NSArray *jsonObject = [NSJSONSerialization JSONObjectWithData:[locationString dataUsingEncoding:NSUTF8StringEncoding]
                                                          options:0 error:NULL];

    NSString *strFormatedAddress = [[[jsonObject valueForKey:@"results"] objectAtIndex:0] valueForKey:@"formatted_address"];
    completionBlock(strFormatedAddress);
}

and to call function

并调用函数

CLLocation* currentLocation = [[CLLocation alloc] initWithLatitude:[SharedObj.current_Lat floatValue] longitude:[SharedObj.current_Long floatValue]];

    [self getAddressFromLocation:currentLocation complationBlock:^(NSString * address) {
        if(address) {
            NSLog(@"Address:: %@",address);
        }
    }];