2010-10-23 2 views

ответ

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

Четан превосходен и даст вам широты и долго в градусах. Только в случае, если вы заинтересованы только в хранении лат и долго в единицах, которые затем можно использовать для сравнения с другими местами, вы можете просто сделать следующее:

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

Если вы хотите сохранить это, то вы бы хотите предоставить какое-то хранилище для значений. В противном случае они выйдут из области действия в конце метода.

Обратите внимание, что CLLocationDegrees - это просто двойник с красивым именем.

Имейте в виду, что CLLocation.coordinate - это аккуратная структура, которую вы можете хранить как CLLocationCoordinate2D - гораздо более элегантно, чтобы поддерживать эти ценности вместе, поскольку они сохраняют немного больше контекста.

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

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; 
    [email protected]"New Delhi"; 
    [email protected]"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) Используйте следующие Место для updateUserLocation

-(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; 
     [email protected]"Moradabad"; 
     [email protected]"My Home Town"; 
    } 
Смежные вопросы