如何显示经度,纬度在相同的代码

问题描述:

//ViewController.h如何显示经度,纬度在相同的代码

#import <UIKit/UIKit.h> 
#import <MapKit/MapKit.h> 
#import <CoreLocation/CoreLocation.h> 
@interface ViewController : UIViewController<CLLocationManagerDelegate,MKMapViewDelegate> 
@property (weak, nonatomic) IBOutlet UILabel *addresslbl; 

@property (weak, nonatomic) IBOutlet MKMapView *mapView; 

@end 

标签//Viewcontroller.m

#import "ViewController.h" 

@interface ViewController() 
{ 
    CLLocationManager *locationManager; 
    CLLocationCoordinate2D updateLocation; 
    NSString *strAddress; 
    CLGeocoder *geocoder; 

} 

@end 

@implementation ViewController 
#define AzaveaCoordinate CLLocationCoordinate2DMake(19.167391, 73.247686) 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [_mapView setRegion:MKCoordinateRegionMake(AzaveaCoordinate, MKCoordinateSpanMake(0.010, 0.010)) animated:YES]; 


    geocoder = [[CLGeocoder alloc]init]; 

    locationManager = [[CLLocationManager alloc] init]; 

    locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    [locationManager requestWhenInUseAuthorization]; 
    self.mapView.delegate = self; 
} 
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 
{ 
    NSLog(@"didFailWithError: %@", error); 
} 

-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated 
{ 
    CLLocation *location = [[CLLocation alloc]initWithLatitude:mapView.centerCoordinate.latitude longitude:mapView.centerCoordinate.longitude]; 
    [self geocode:location]; 

} 

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation 
{ 
    CLLocation *currentLocation = newLocation; 
    self.mapView.centerCoordinate = currentLocation.coordinate; 
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(currentLocation.coordinate,1500,1500); 
    [self.mapView setRegion: region animated:true]; 
    [self geocode:currentLocation]; 
} 

    -(void)geocode:(CLLocation *)location 

{ 
    // geocoder = [[CLGeocoder alloc]init]; 
    [geocoder cancelGeocode]; 
    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *data, NSError *error) 
    { 
     CLPlacemark *placemark = [data lastObject]; 

      NSDictionary * addressDict = placemark.addressDictionary; 

      NSArray * addressList = addressDict[@"FormattedAddressLines"]; 

     NSString * address = [NSString stringWithFormat:@"%@,%@,%@",addressList[0],addressList[1],addressList[2]]; 
     NSLog(@" Address is :%@",address); 
      self.addresslbl.text = address; 


}]; 
} 


- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 


@end 

这是移动地图代码像OLA和尤伯杯。意思是当我移动地图时,当时地图引脚被注意到,标记中给出的引脚给出了该特定区域的地址,如城市,州,pincode等,但我只想要这个地址的经度,纬度值在标签。

所以请帮我关于这段代码。我怎样才能获得经度纬度值?

假设您想要的标签是self.addresslbl,改变这个方法:

-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated 
{ 
    CLLocation *location = [[CLLocation alloc]initWithLatitude:mapView.centerCoordinate.latitude longitude:mapView.centerCoordinate.longitude]; 
    [self geocode:location]; 
} 

要:

-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated 
    { 
     self.addresslbl.text = [NSString stringWithFormat:@"%f, %f", mapView.centerCoordinate.longitude, mapView.centerCoordinate.latitude]; 
    } 
+0

感谢R.lin ..thanks一个lot..its工作 – NARAYAN

+0

OK以及如果你能接受那个很好的答案。 @NARAYAN –