[英]Get current location using 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上进行测试。
如果有任何示例代码,那就太好了。
首先添加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);
}
在AppDelegate.h中声明以下变量。 并实施
CLLocationManagerDelegate delegate.
CLLocationManager *locationManager;
CLLocation *currentLocation;
在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);
并参考链接(没有mapview0
https://developer.apple.com/library/ios/#samplecode/LocateMe/Introduction/Intro.html
您需要使用CLLocationManager
来获取用户的位置。 CLLocation
只是用于表示位置的一类对象,它本身无法获取用户位置。
有关更多详细信息和示例,请遵循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.