ios 使用 CLLocationCoordinate2D 获取当前位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9696396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:17:44  来源:igfitidea点击:

Get current location using CLLocationCoordinate2D

iphoneobjective-cioscllocationmanager

提问by iOSDev

I am trying to get current location of user using CLLocationCoordinate2D. I want the values in the format like

我正在尝试使用 CLLocationCoordinate2D 获取用户的当前位置。我想要像这样格式的值

CLLocationCoordinate2D start = {-28.078694,153.382844 };

CLLocationCoordinate2D 开始 = {-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. I am testing on iPhone 4s.

但是当我尝试测试时,两者都得到 0.000。我正在 iPhone 4s 上进行测试。

If there is any sample code, it will be great.

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

回答by Mudit Bajpai

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


}

回答by Vibhooti

in AppDelegate.h declare following variable. and implement

在 AppDelegate.h 中声明以下变量。并实施

CLLocationManagerDelegate delegate.

CLLocationManager *locationManager;
CLLocation *currentLocation;

in AppDelegate.h.m 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; 
}

回答by Rams

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

回答by Aleksejs Mjaliks

You need to use CLLocationManagerto get a user's location. CLLocationis only a class of objects used to represent locations, it can't get user location by it self.

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

For more details and example, please follow to Apple docs on Location Awareness Programming.

有关更多详细信息和示例,请参阅 Apple 文档位置感知编程

回答by Manish Agrawal

use location manager to get lat and long, follow this tutorial for sample code http://www.edumobile.org/iphone/miscellaneous/how-to-get-current-latitude-and-longitude-in-iphone/

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

回答by dominic

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