如何在同一代码的标签中显示经度,纬度

时间:2017-10-13 13:39:45

标签: objective-c mapkit mapkitannotation

// 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和uber。意思是当我移动地图时,那时地图引脚被注明,标注的引脚给我标签中的特定区域的地址,如城市,州,密码等,但我只想要这个地址的经度,纬度值在标签中。

所以请帮我解释一下这段代码。如何获得经度纬度值?

1 个答案:

答案 0 :(得分:0)

假设您想要的标签是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];
    }