使用CLLocationCoordinate2D获取当前位置

时间:2012-03-14 05:25:55

标签: iphone objective-c ios cllocationmanager

我正在尝试使用CLLocationCoordinate2D获取用户的当前位置。我希望像

这样的格式的值

CLLocationCoordinate2D start = {-28.078694,153.382844};

所以我可以像下面这样使用它们:

 NSString *urlAddress = [NSString 
       stringWithFormat:@"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f",  
       start.latitude, start.longitude, 
       destination.latitude, destination.longitude];  

我用过

CLLocation *location = [[CLLocation alloc]init ];
CLLocationDegrees currentLatitude = location.coordinate.latitude;
CLLocationDegrees currentLongitude = location.coordinate.longitude;

获得当前的lat&长。

但是当我尝试测试时,我得到了0.000。我在iPhone 4s上测试。

如果有任何示例代码,那就太棒了。

6 个答案:

答案 0 :(得分:15)

首先添加CoreLocation框架然后使用以下代码...并且您的viewController必须实现CLLocationManagerDelegate

-(void)findCurrentLocation
{

    CLLocationManager *locationManager = [[CLLocationManager alloc] init];
    if ([locationManager locationServicesEnabled])
    {
        locationManager.delegate = self; 
        locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
        locationManager.distanceFilter = kCLDistanceFilterNone; 
        [locationManager startUpdatingLocation];
    }


    CLLocation *location = [locationManager location];
    CLLocationCoordinate2D coordinate = [location coordinate];

    NSString *str=[[NSString alloc] initWithFormat:@" latitude:%f longitude:%f",coordinate.latitude,coordinate.longitude];
    NSLog(@"%@",str);


}

答案 1 :(得分:7)

AppDelegate.h中的

声明了以下变量。并实施

CLLocationManagerDelegate delegate.

CLLocationManager *locationManager;
CLLocation *currentLocation;
在AppDelegate.h.m文件中

编写以下代码。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {       

    self.locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

    self.currentLocation = newLocation;
    self.currentLat =  newLocation.coordinate.latitude; 
    self.currentLong =  newLocation.coordinate.longitude; 
}

答案 2 :(得分:2)

CLLocationCoordinate2D location = [[[mapview userLocation] location] coordinate];  
NSLog(@"Location found from Map: %f %f",location.latitude,location.longitude);

并且还引用链接(没有mapview0

https://developer.apple.com/library/ios/#samplecode/LocateMe/Introduction/Intro.html

答案 3 :(得分:2)

您需要使用CLLocationManager来获取用户的位置。 CLLocation只是一类用于表示位置的对象,它无法通过它自己获取用户位置。

有关详情和示例,请关注Location Awareness Programming上的Apple文档。

答案 4 :(得分:1)

使用位置管理器获取lat和long,请按照本教程获取示例代码http://www.edumobile.org/iphone/miscellaneous/how-to-get-current-latitude-and-longitude-in-iphone/

答案 5 :(得分:0)

-(void)findCurrentLocation
{
    if ([CLLocationManager locationServicesEnabled])
    {
        CLLocationManager *locationManager = [[CLLocationManager alloc] init];
        locationManager.delegate = self;
        locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        locationManager.distanceFilter = kCLDistanceFilterNone;
        [locationManager startUpdatingLocation];
    }
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    [self setLocation:[[manager location] coordinate]];
}