如何在mkmapview中查找用户的当前位置

rakesh

我想在代码的下方找到用户的当前位置,以便从定义明确的地址本垒查找坐标。我可以找到用户的当前位置..请帮助我

.h文件#import #import

@interface ViewController : UIViewController<MKMapViewDelegate,CLLocationManagerDelegate>
@property(nonatomic,retain)  MKMapView *myMapView;
@property (nonatomic, strong) CLGeocoder *myGeocoder;
@end

.m文件

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>

@interface ViewController ()
{
    CLLocationManager *locationManager;
}

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];


    self.myMapView.showsUserLocation=YES;

    self.view.backgroundColor=[UIColor whiteColor];
    self.myMapView=[[MKMapView alloc]initWithFrame:self.view.bounds];

    self.myMapView.mapType=MKMapTypeHybrid;
    self.myMapView.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:self.myMapView];


    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate=self;
    locationManager.desiredAccuracy=kCLLocationAccuracyBest;
    locationManager.distanceFilter=kCLDistanceFilterNone;

    [locationManager startUpdatingLocation];

   NSString *oreillyAddress =@"india";
  self.myGeocoder = [[CLGeocoder alloc] init];
    [self.myGeocoder
     geocodeAddressString:oreillyAddress completionHandler:^(NSArray *placemarks, NSError *error)
   {
        if ([placemarks count] > 0 && error == nil)
            {
       NSLog(@"Found %lu placemark(s).", (unsigned long)[placemarks count]);
             CLPlacemark *firstPlacemark = [placemarks objectAtIndex:0]; NSLog(@"Longitude = %f", firstPlacemark.location.coordinate.longitude); NSLog(@"Latitude = %f", firstPlacemark.location.coordinate.latitude);


         }
         else if ([placemarks count] == 0 &&
                  error == nil){ NSLog(@"Found no placemarks.");
        }
         else if (error != nil){
             NSLog(@"An error occurred = %@", error); }
     }];


    // Do any additional setup after loading the view, typically from a nib.
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return YES;
}
- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
    float latitude=newLocation.coordinate.latitude;
    float longitude=newLocation.coordinate.longitude;
    NSLog(@"%f",latitude);
    NSLog(@"%f",longitude);
    [locationManager stopUpdatingLocation];

}

- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error
{
    NSLog(@"error==>%@",error);
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
Bhumeshwer凯特

用这个

self.myMapView.showsUserLocation = YES;

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在扑动中的当前位置上创建圆

如何在iOS中从用户获取当前位置

iOS MKMapView上的当前位置按钮

如何在iOS8中使用MKMapView的“显示用户位置”?

如何获得迭代器在ByteString中的当前位置?

如何在悬停时的当前位置停止CSS动画?

如何在angularjs中获取元素的当前位置

如何在Appcode中的当前文件中查找符号

如何在Asp .Net core 2.1中获得Ubuntu系统的当前用户?

Javascript:如何在光标的当前位置插入域?

如何在Xcode的地图中显示我的当前位置?

如何在Dialogflow chatbot中获取用户的当前位置?

在python中如何获取Windows中的当前用户SID?

如何在Sublime Text 3中查看光标位置的当前列/字符号?

如何在PHP中获取当前月份的当前周数

通过Facebook API查找用户登录时的当前位置(纬度和经度)?

如何在不使用核心位置服务的情况下获取用户的当前城市名称?

当以管理员身份运行时,如何在Cygwin中获得实际的当前用户?

如何在ng-autocomplete中添加用户的当前位置

如何在android中获取准确的当前位置?

如何在Android中获取用户的当前位置

如何计算查询中的当前索引位置?

如何在模板中显示项目中的当前用户角色?

如何在 Java 中查找视图的当前 JTextField 的名称?

班级中的当前用户

如何在 Java 中获取 Linux 的当前用户名

从 2D 平面 iOS 中的当前位置查找用户的方位

SwiftUI:如何获取用户的当前位置?

自动在 SMS (Swift) 中包含用户的当前位置