Google 地图上的当前经纬度

Current Latitude Longitude on Google Map

我正在使用下面的代码在 iOS 8.

中获取当前经纬度

.h

@interface DirectionViewController : UIViewController<CLLocationManagerDelegate,GMSMapViewDelegate>
{
    CLLocationManager *locationManager;
}
@property (nonatomic, retain) CLLocationManager *locationManager;

.m

@synthesize locationManager;


    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    [locationManager startUpdatingLocation];
    CLLocation *location = [locationManager location];
    // Configure the new event with information from the location
    CLLocationCoordinate2D coordinate = [location coordinate];
    NSString *latitude = [NSString stringWithFormat:@"%f", coordinate.latitude];
    NSString *longitude = [NSString stringWithFormat:@"%f", coordinate.longitude];
    NSLog(@"dLatitude : %@", latitude);
    NSLog(@"dLongitude : %@",longitude);

但在这里,我得到的纬度和经度值均为 0.00000。 任何人都可以在这里帮助我。如何在 iOS 8 中获取当前经纬度?

提前致谢!

您没有使用正确的方法获取坐标。

一旦您调用 startUpdatingLocation,它就会调用位置管理器方法(您必须实施该方法才能使其工作)。

有两个,[locationManager:didFailWithError:][1] [-locationManager:didUpdateLocations:][2]。您应该会收到警告,要求您在 .m 中添加这些方法,如果您还没有这样做,请使用正确的拼写。

didUpdateLocation 每 X 秒调用一次,坐标为 returns。您必须在设置坐标的这种非常具体的方法中构建坐标字符串。

据我所知,

- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations

Will give you an array of CLLocation objects containing the location data.
This array always contains at least one object representing the current location. 
If updates were deferred or if multiple locations arrived before they could be delivered, the array may contain additional entries. 
The objects in the array are organized in the order in which they occurred. Therefore, the most recent location update is at the end of the array.

这意味着您必须获取该数组的 lastObject 作为最近的位置。

这是我找到的一个示例 on the web,了解该方法的内部结构。 :

CLLocation *newLocation = [locations lastObject];
    CLLocation *oldLocation;
    if (locations.count > 1) {
        oldLocation = [locations objectAtIndex:locations.count-2];
    } else {
        oldLocation = nil;
    }
    NSLog(@"didUpdateToLocation %@ from %@", newLocation, oldLocation);
    MKCoordinateRegion userLocation = MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 1500.0, 1500.0);
    [regionsMapView setRegion:userLocation animated:YES];

确保事先请求不同的权限,并且您拥有这些权限。 另请注意,此方法仅在 iOS5 之后可用,并且在以前的版本中有所不同。虽然你不太可能仍然支持 iOS5,但我想我应该提一下。

AppDelegte.h

@interface AppDelegate : UIResponder <UIApplicationDelegate,CLLocationManagerDelegate>
{
  CLLocation *currentLocation;
}
@property (strong, nonatomic) CLLocationManager             *locationManager;
@property (nonatomic, assign) CLLocationCoordinate2D currentLocationCoordinate;

Appdelget.m

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

    locationManager = [[CLLocationManager alloc]init]; // initializing locationManager
    locationManager.delegate = self; // we set the delegate of locationManager to self.
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; // setting the accuracy

    [locationManager startUpdatingLocation];

}

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

  CurrentLatitude=newLocation.coordinate.latitude;
    CurrentLongitude=newLocation.coordinate.longitude;
    NSLog(@"%f",CurrentLatitude);
    NSLog(@"%f",CurrentLongitude);
    [locationManager stopUpdatingLocation];

}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"didFailWithError: %@", error);
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [errorAlert show];
}

.请添加

NSLocationAlwaysUsageDescription

NSLocationWhenInUseUsageDescription