1) Я получил информацию, которая должна была использовать CLLocationManager
в моем приложении для отслеживания местоположения. Как мне это использовать?
в .h файле
#include <CoreLocation/CLLocationManagerDelegate.h>
#include <CoreLocation/CLError.h>
#include <CoreLocation/CLLocation.h>
#include <CoreLocation/CLLocationManager.h>
CLLocationManager * myLocationManager;
CLLocation * myLocation;
в .m файле: -
-(void)findMyCurrentLocation
{
self.myLocationManager = [[[CLLocationManager alloc] init] autorelease];
[[self myLocationManager] setDelegate:self ];
[myLocationManager startUpdatingLocation];
double latitude=34.052234;
double longitude=-118.243685;
CLLocation *defaultLocation =[[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
[self setMyLocation:defaultLocation];
[defaultLocation release];
if( [CLLocationManager locationServicesEnabled] )
{
NSLog(@"Location Services Enabled....");
locationServicesEnabled=TRUE;
UIAlertView *alert = [ [UIAlertView alloc] initWithTitle:@"Information"
message:@"Fetching your current location."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil ];
[alert release];
}
else
{
NSLog( @"Location Services Are Not Enabled...." );
locationServicesEnabled=FALSE;
UIAlertView *alert = [ [UIAlertView alloc] initWithTitle:@"Information"
message:@"Location service is not enable. Please enable it from settings."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil ];
[alert release];
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation: (CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
[self setMyLocation:newLocation];
NSString *tempLat = [ NSString stringWithFormat:@"%3.6f" , (newLocation.coordinate.latitude) ];
NSString *tempLong= [ NSString stringWithFormat:@"%3.6f" , (newLocation.coordinate.longitude)];
appDelegate.curlat = tempLat;
appDelegate.curlong = tempLong;
}
- (void)locationManager: (CLLocationManager *)manager didFailWithError: (NSError *)error
{
printf("\nerror");
UIAlertView *alert = [ [UIAlertView alloc] initWithTitle:@"Error"
message:@"Error while getting your current location."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil ];
[alert release];
}
2). Я хочу показать текущее местоположение пользователя, например информацию о названии страны, в моем приложении.
Для этого вы можете использовать Google Reverse Geo кодирование ИЛИ MKReverseGeocoder