Google地图上的当前纬度经度

时间:2015-04-27 12:04:17

标签: objective-c google-maps ios8 latitude-longitude

我使用下面的代码在iOS 8中获取当前的lat。

·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中获得当前的lat?

提前致谢!

2 个答案:

答案 0 :(得分:1)

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

一旦您致电startUpdatingLocation,它就会调用位置管理器方法(您必须实施这些方法才能实现此目的)。

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

每隔X秒调用<{didUpdateLocation并返回坐标。您必须在这个非常特定的方法中构建坐标字符串,其中设置了坐标。

从我能收集到的,

- (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,但我想我应该提一下。

答案 1 :(得分:1)

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

.Plsit add

NSLocationAlwaysUsageDescription

NSLocationWhenInUseUsageDescription

enter image description here