访问 iPhone 的当前位置
Posted
技术标签:
【中文标题】访问 iPhone 的当前位置【英文标题】:Accessing iPhone's current location 【发布时间】:2010-10-23 22:26:22 【问题描述】:如何将 iPhone 当前位置的经纬度坐标存储到两个不同的浮点变量中?
【问题讨论】:
【参考方案1】:This 教程将帮助您做到这一点。
这是您可能感兴趣的教程中的相关代码:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
int degrees = newLocation.coordinate.latitude;
double decimal = fabs(newLocation.coordinate.latitude - degrees);
int minutes = decimal * 60;
double seconds = decimal * 3600 - minutes * 60;
NSString *lat = [NSString stringWithFormat:@"%d° %d' %1.4f\"",
degrees, minutes, seconds];
latLabel.text = lat;
degrees = newLocation.coordinate.longitude;
decimal = fabs(newLocation.coordinate.longitude - degrees);
minutes = decimal * 60;
seconds = decimal * 3600 - minutes * 60;
NSString *longt = [NSString stringWithFormat:@"%d° %d' %1.4f\"",
degrees, minutes, seconds];
longLabel.text = longt;
【讨论】:
【参考方案2】:Chetan 的答案非常好,可以为您提供经度和经度的度数。万一您只对以单位存储纬度和经度感兴趣,然后您可以使用这些单位与其他位置进行比较,您可以执行以下操作:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
CLLocationDegrees latitude = newLocation.coordinate.latitude;
CLLocationDegrees longitude = newLocation.coordinate.longitude;
...
如果您想保留这些,那么您需要为这些值提供某种存储。否则,它们将在方法结束时超出范围。
请注意,CLLocationDegrees 只是一个具有漂亮名称的双精度数。
请记住,CLLocation.coordinate 是一个简洁的结构,您可以将其存储为 CLLocationCoordinate2D - 将这些值保持在一起会更加优雅,因为它们保留了更多上下文。
【讨论】:
【参考方案3】:你可以初始化一个 CLLocationManager 来找到一个点并在后面引用它(注意这个初始化是从另一篇文章中借来的)。
CLLocationManager *curLocationManager = [[CLLocationManager alloc] init];
curLocationManager.delegate = self; //SET YOUR DELEGATE HERE
curLocationManager.desiredAccuracy = kCLLocationAccuracyBest; //SET THIS TO SPECIFY THE ACCURACY
[curLocationManager startUpdatingLocation];
//NSLog(@"currentLocationManager is %@", [curLocationManager.location description]);
[curLocationManager stopUpdatingLocation];
//NSLog(@"currentLocationManager is now %@", [curLocationManager.location description]);
//NSLog(@"latitude %f", curLocationManager.location.coordinate.latitude);
//NSLog(@"longitude %f", curLocationManager.location.coordinate.longitude);
double latitude = curLocationManager.location.coordinate.latitude;
double longitude = curLocationManager.location.coordinate.longitude;
请注意,您还需要包含(CLLocationManager *)locationManager
和(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
,并且您应该包含(void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
【讨论】:
【参考方案4】:使用以下代码在 MKMapView 中显示当前位置并在 iPhone 应用程序中设置缩放级别。
1) 在您的项目中添加 MApKit 和 CoreLocation 框架。
2) 在 ViewController.h 文件中使用以下代码:
#import "mapKit/MapKit.h"
#import "CoreLocation/CoreLocation.h"
@interface ViewController : UIViewController<MKMapViewDelegate, CLLocationManagerDelegate>
MKMapView *theMapView;
CLLocationManager *locationManager;
CLLocation *location;
float latitude, longitude;
3) 在 viewDidLoad 方法中添加如下代码:
// Add MKMapView in your View
theMapView=[[MKMapView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
theMapView.delegate=self;
[self.view addSubview:theMapView];
// Create an instance of CLLocationManager
locationManager=[[CLLocationManager alloc] init];
locationManager.desiredAccuracy=kCLLocationAccuracyBest;
locationManager.delegate=self;
[locationManager startUpdatingLocation];
// Create an instance of CLLocation
location=[locationManager location];
// Set Center Coordinates of MapView
theMapView.centerCoordinate=CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude);
// Set Annotation to show current Location
MKPointAnnotation *annotaionPoint=[[MKPointAnnotation alloc] init];
annotaionPoint.coordinate=theMapView.centerCoordinate;
annotaionPoint.title=@"New Delhi";
annotaionPoint.subtitle=@"Capital";
[theMapView addAnnotation:annotaionPoint];
// Setting Zoom Level on MapView
MKCoordinateRegion coordinateRegion;
coordinateRegion.center = theMapView.centerCoordinate;
coordinateRegion.span.latitudeDelta = 1;
coordinateRegion.span.longitudeDelta = 1;
[theMapView setRegion:coordinateRegion animated:YES];
// Show userLocation (Blue Circle)
theMapView.showsUserLocation=YES;
4) 使用以下位置更新用户位置
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
latitude=userLocation.coordinate.latitude;
longitude=userLocation.coordinate.longitude;
theMapView.centerCoordinate=CLLocationCoordinate2DMake(latitude, longitude);
MKPointAnnotation *annotationPoint=[[MKPointAnnotation alloc] init];
annotationPoint.coordinate=theMapView.centerCoordinate;
annotationPoint.title=@"Moradabad";
annotationPoint.subtitle=@"My Home Town";
【讨论】:
以上是关于访问 iPhone 的当前位置的主要内容,如果未能解决你的问题,请参考以下文章