2012-03-14 119 views
1

我正在尝试使用CLLocationCoordinate2D获取用户的当前位置。我想要的格式值如下:使用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; 

获取当前纬度&长。

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

如果有任何示例代码,它会很好。

+0

您是否使用了'MKMapView'? – 2012-03-14 05:27:56

+0

你怎么得到你的坐标?添加迄今为止尝试的代码。 – deanWombourne 2012-03-14 05:28:00

+0

我正在使用CLLocation * location = [[CLLocation alloc] init]; CLLocationDegrees currentLatitude = location.coordinate.latitude; CLLocationDegrees currentLongitude = location.coordinate.longitude; – iOSDev 2012-03-14 05:30:03

回答

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


} 
+3

'[locationManager location]'在这个例子中可能会返回'nil'或非常旧的位置。获得当前位置的最好方法是等待'locationManager'向其代理发送回叫消息。但仍然有回调消息传递的位置可能较旧,因此,您需要始终检查位置的时间戳。 – 2012-03-14 05:47:12

+2

locationServicesEnabled已弃用最新版本.. – Rams 2012-03-14 06:01:54

+0

它不适用于我的纬度和经度值来这样的0.00000 – vijay 2015-02-27 11:09:55

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

+0

我没有使用mapview,只是将开始和目的地添加到谷歌地图。然后在UIWebView中显示它们。我怎样才能得到它没有mapview? – iOSDev 2012-03-14 05:37:39

+0

是的,你可以不用'MKMapView'。很难理解,为什么使用'UIWebView'。 – 2012-03-14 05:39:16

+0

任何示例代码? – iOSDev 2012-03-14 05:40:10

2

您需要使用CLLocationManager获取用户的位置。CLLocation只是一个类的对象来表示的地方,也可以”牛逼通过其自身获得用户的位置。

有关详细信息和示例,请按照苹果的文档上Location Awareness Programming

+0

任何示例代码,以获得确切的纬度和纵向? – iOSDev 2012-03-14 05:38:41

+0

请查阅Apple文档[位置感知编程](https://developer.apple。COM /库/ IOS /#文档/ UserExperience /概念/ LocationAwarenessPG/CoreLocation/CoreLocation.html)。 – 2012-03-14 05:41:15

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; 
} 
+1

self.locationManager = [[CLLocationManager alloc] init];线路上有泄漏。 – Rams 2012-03-19 05:58:06

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