访问iPhone的当前位置

时间:2010-10-23 22:26:22

标签: objective-c cocoa-touch core-location

如何将iPhone当前位置的纬度和经度坐标存储到两个不同的浮点变量中?

4 个答案:

答案 0 :(得分:4)

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;
}

答案 1 :(得分:0)

Chetan的答案非常好,会给你拉特和长度。如果您只想将lat和long存储在单位中,然后可以将其用于与其他位置进行比较,则可以执行以下操作:

- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
       fromLocation:(CLLocation *)oldLocation {
CLLocationDegrees latitude = newLocation.coordinate.latitude;
CLLocationDegrees longitude = newLocation.coordinate.longitude;
...
}

如果你想保留这些,那么你想为这些值提供某种存储空间。否则它们将在方法结束时超出范围。

请注意,CLLocationDegrees只是一个具有漂亮名称的双精度。

请记住,CLLocation.coordinate是一个整齐的结构,您可以将其存储为CLLocationCoordinate2D - 将这些值保持在一起更加优雅,因为它们可以保留更多的上下文。

答案 2 :(得分:0)

你可以初始化一个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

答案 3 :(得分:0)

使用以下代码显示MKMapView中的当前位置以及在iPhone App中设置缩放级别。

1)在项目中添加MApKit和CoreLocation Framework。

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";
     }