简体   繁体   English

使用CLLocationCoordinate2D获取当前位置

[英]Get current location using CLLocationCoordinate2D

I am trying to get current location of user using CLLocationCoordinate2D. 我正在尝试使用CLLocationCoordinate2D获取用户的当前位置。 I want the values in the format like 我想要像这样的格式的值

CLLocationCoordinate2D start = {-28.078694,153.382844 }; CLLocationCoordinate2D start = {-28.078694,153.382844};

So that I can use them like following: 这样我就可以像下面这样使用它们:

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

I used 我用了

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

to get current lat & long. 得到当前的经纬度。

But I am getting 0.000 for both when I try to test. 但是当我尝试测试时,两者的总和都是0.000。 I am testing on iPhone 4s. 我正在iPhone 4s上进行测试。

If there is any sample code, it will be great. 如果有任何示例代码,那就太好了。

Add CoreLocation framework first and then use the following code...and your viewController must implement CLLocationManagerDelegate 首先添加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);


}

in AppDelegate.h declare following variable. 在AppDelegate.h中声明以下变量。 and implement 并实施

CLLocationManagerDelegate delegate.

CLLocationManager *locationManager;
CLLocation *currentLocation;

in AppDelegate.hm file write following code. 在AppDelegate.hm文件中,编写以下代码。

- (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; 
}
CLLocationCoordinate2D location = [[[mapview userLocation] location] coordinate];  
NSLog(@"Location found from Map: %f %f",location.latitude,location.longitude);

and also refer the link(without mapview0 并参考链接(没有mapview0

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

You need to use CLLocationManager to get a user's location. 您需要使用CLLocationManager来获取用户的位置。 CLLocation is only a class of objects used to represent locations, it can't get user location by it self. CLLocation只是用于表示位置的一类对象,它本身无法获取用户位置。

For more details and example, please follow to Apple docs on Location Awareness Programming . 有关更多详细信息和示例,请遵循Apple文档上的位置感知编程

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM