【问题标题】:how to show longitude, latitude in label of this same code如何在同一代码的标签中显示经度、纬度
【发布时间】:2017-10-13 13:39:44
【问题描述】:

//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

//视图控制器.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 之类的可移动地图代码。意味着当我移动地图时,地图针会被记下,记下的针会在标签中给出特定区域的地址,如城市、州、密码等,但我只想要这个地址的经度、纬度值在标签中。

所以请帮助我了解此代码。我怎样才能得到经度纬度值?

【问题讨论】:

    标签: objective-c mapkit mapkitannotation


    【解决方案1】:

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

    【讨论】:

    • 好吧,如果你能接受这个答案就好了。 @NARAYAN
    猜你喜欢
    • 2012-02-03
    • 1970-01-01
    • 2012-04-14
    • 2020-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多