How can I store the latitude and longitude coordinates of the iPhone's cur开发者_JS百科rent location into two different float variables?
This tutorial will help you do exactly that.
Here is the relevant code from the tutorial that you would be interested in:
- (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;
}
Chetan's answer is excellent and will give you the lat and long in degrees. Just in case you're only interested in storing the lat and long in units that you can then use for comparison with other locations you might just do the following:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
CLLocationDegrees latitude = newLocation.coordinate.latitude;
CLLocationDegrees longitude = newLocation.coordinate.longitude;
...
}
If you want to keep these then you'd want to provide some sort of storage for the values. Otherwise they'll go out of scope at the end of the method.
Note that CLLocationDegrees is merely a double with a pretty name.
Keep in mind that a CLLocation.coordinate is a neat struct that you may store as a CLLocationCoordinate2D - much more elegant to keep these values together as they retain a little more context.
You can initialize a CLLocationManager to find a point and reference it afterwords (note this initialization is borrowed from another post).
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;
Note you will also need to include (CLLocationManager *)locationManager
and (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
and you should include (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
Use following code to show current location in MKMapView and for setting Zoom Level in iPhone App.
1) Add MApKit and CoreLocation Framework in your project.
2) Use following code in ViewController.h file:
#import "mapKit/MapKit.h"
#import "CoreLocation/CoreLocation.h"
@interface ViewController : UIViewController<MKMapViewDelegate, CLLocationManagerDelegate>
{
MKMapView *theMapView;
CLLocationManager *locationManager;
CLLocation *location;
float latitude, longitude;
}
3) Add following code on viewDidLoad method:
// 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) Use following Location to 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;
annotationPoint.title=@"Moradabad";
annotationPoint.subtitle=@"My Home Town";
}
精彩评论