检索纬度和经度坐标,显示在 UILabel 中

发布于 2024-11-26 01:18:53 字数 159 浏览 2 评论 0原文

我有兴趣获取用户当前的纬度和经度坐标,并将它们按字面意思显示为视图上 UILabel 中的 NSSString。

我不需要任何 MKMapView 或以图形方式显示任何内容,只需在 UILabel 中显示坐标。这可能吗?

有人可以为我提供一个起点吗?

谢谢

I am interested in grabbing my users current Latitude and Longitude coordinates, and displaying them literally as a NSSString in a UILabel on the view.

I don't need any MKMapView or to show anything graphically, just to display the coordinates in a UILabel. Is this possible?

Could anyone provide a starting block for me?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

幽梦紫曦~ 2024-12-03 01:18:53

是的,有可能。只需导入 #import 并声明委托 。然后您可以通过以下委托方法获取值。

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{

    CLLocationCoordinate2D location=newLocation.coordinate;
    NSString *s = [NSString stringWithFormat:@"%f,%f",location.latitude,location.longitude];
}

Ya, its possible. Just import #import <CoreLocation/CoreLocation.h> and declare the delegate <CLLocationManagerDelegate>. then you can get the values in following delegate mathod.

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{

    CLLocationCoordinate2D location=newLocation.coordinate;
    NSString *s = [NSString stringWithFormat:@"%f,%f",location.latitude,location.longitude];
}
倦话 2024-12-03 01:18:53

CoreLocation 框架可以完成这项工作。您可以通过实现此委托来获取用户的当前位置。

- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
       fromLocation:(CLLocation *)oldLocation

There is the CoreLocation framework which does this job. You can get the user's current location by implementing this delegate.

- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
       fromLocation:(CLLocation *)oldLocation
荆棘i 2024-12-03 01:18:53

请按照以下步骤操作:

  • 将 MapKit.framework 添加到您的项目

  • add to .h #import "CoreLocation/CoreLocation.h" 和 #import "MapKit/MapKit.h "

  • 使用委托作为 @interface yourInterface : UIViewController < MKMapViewDelegate, CLLocationManagerDelegate >

现在将以下方法添加到您的 .m 文件

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{  
[self setMapCenter:newLocation.coordinate];
[self._mapView selectAnnotation:[[self._mapView annotations] lastObject] animated:YES];
lblLong.text = [nsstring stringWithFormat:@"%f", newLocation.coordinate.longitude];
lblLat = [nsstring stringWithFormat:@"%f", newLocation.coordinate.latitude];
[self.locationManager stopUpdatingLocation];
}

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"ERROR");
}

这里提到 CLLocationManager *locationManager;。祝你好运。

Follow the steps:

  • Add MapKit.framework to your project

  • add to .h #import "CoreLocation/CoreLocation.h" and #import "MapKit/MapKit.h"

  • Use delegates as, @interface yourInterface : UIViewController < MKMapViewDelegate, CLLocationManagerDelegate >

Now Add following method to your .m file

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{  
[self setMapCenter:newLocation.coordinate];
[self._mapView selectAnnotation:[[self._mapView annotations] lastObject] animated:YES];
lblLong.text = [nsstring stringWithFormat:@"%f", newLocation.coordinate.longitude];
lblLat = [nsstring stringWithFormat:@"%f", newLocation.coordinate.latitude];
[self.locationManager stopUpdatingLocation];
}

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"ERROR");
}

Here mention CLLocationManager *locationManager;. good luck.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文