ios定期获取我的位置并设置按钮获取我的位置-谷歌地图不起作用

ios定期获取我的位置并设置按钮获取我的位置-谷歌地图不起作用,ios,objective-c,google-maps,cllocationmanager,Ios,Objective C,Google Maps,Cllocationmanager,我正在使用Google地图模块,使用1.9.0捆绑包,定期查找我的位置,比如说10秒,并创建一个按钮以获取我的位置按钮作为mapview设置之一 当执行和位置服务打开时,无论是否单击右下角按钮,都没有响应,位置管理器不工作。我可以显示任何使用定位服务的迹象 我不确定我在设置这个地图时遗漏了什么,因为我严格按照SDKDemos-ios谷歌演示项目来设置我的模块。请告诉我确切的设置方法好吗 以下是我的代码: @implementation MapViewController -(bool)isN

我正在使用Google地图模块,使用1.9.0捆绑包,定期查找我的位置,比如说10秒,并创建一个按钮以获取我的位置按钮作为mapview设置之一

当执行和位置服务打开时,无论是否单击右下角按钮,都没有响应,位置管理器不工作。我可以显示任何使用定位服务的迹象

我不确定我在设置这个地图时遗漏了什么,因为我严格按照SDKDemos-ios谷歌演示项目来设置我的模块。请告诉我确切的设置方法好吗

以下是我的代码:

@implementation MapViewController


-(bool)isNetworkAvailable
{
    SCNetworkReachabilityFlags flags;
    SCNetworkReachabilityRef address;
    address = SCNetworkReachabilityCreateWithName(NULL, "www.apple.com" );
    Boolean success = SCNetworkReachabilityGetFlags(address, &flags);
    CFRelease(address);

    bool canReach = success
    && !(flags & kSCNetworkReachabilityFlagsConnectionRequired)
    && (flags & kSCNetworkReachabilityFlagsReachable);

    return canReach;
}


- (void)requestAlwaysAuthorization
{
    CLAuthorizationStatus status = [CLLocationManager authorizationStatus];

    // If the status is denied or only granted for when in use, display an alert
    if (status == kCLAuthorizationStatusAuthorizedWhenInUse || status == kCLAuthorizationStatusDenied) {
        NSString *title;
        title = (status == kCLAuthorizationStatusDenied) ? @"Location services are off" : @"Background location is not enabled";
        NSString *message = @"To use background location you must turn on 'Always' in the Location Services Settings";

        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
                                                            message:message
                                                           delegate:self
                                                  cancelButtonTitle:@"Cancel"
                                                  otherButtonTitles:@"Settings", nil];
        [alertView show];
    }
    // The user has not enabled any location services. Request background authorization.
    else if (status == kCLAuthorizationStatusNotDetermined) {
        [locationManager requestAlwaysAuthorization];
    }
}


- (void)viewDidLoad

{
    [super viewDidLoad];

    if([self isNetworkAvailable]){
        NSLog(@"connected ");
    }else {
        NSLog(@"not connected ");
    }

    CarArray = [[NSMutableArray alloc] init];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    float latitide = [defaults floatForKey:@"lati"];
    float longitude = [defaults floatForKey:@"longi"];
    NSString *desp = [defaults objectForKey:@"desp"];


    GMSMarker *marker = [[GMSMarker alloc] init];
    NSLog(@"assadsd arrived map");
    if(latitide!=0.00&&longitude!=0.00) {
        CLLocationCoordinate2D position = CLLocationCoordinate2DMake(latitide, longitude);

        marker.position = CLLocationCoordinate2DMake(position.latitude, position.longitude);
        camera = [GMSCameraPosition cameraWithLatitude:latitide  longitude:longitude  zoom:12];
    }else{
        camera = [GMSCameraPosition cameraWithLatitude:22.2855200   longitude:114.1576900  zoom:12];
        marker.position = CLLocationCoordinate2DMake(22.2855200, 114.1576900);
    }


    mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
    marker.snippet = @"HK";

    mapView_.mapType = kGMSTypeSatellite;
    mapView_.delegate = self;

    mapView_.settings.myLocationButton = YES;
    mapView_.settings.compassButton = YES;


    dispatch_async(dispatch_get_main_queue(), ^{
        mapView_.myLocationEnabled = YES;
    });


    if(desp.length > 0 ){
        marker.title = desp;
    }

    // Setup location services
    if (![CLLocationManager locationServicesEnabled]) {
        NSLog(@"Please enable location services");
        return;
    }

    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
        NSLog(@"Please authorize location services");
        return;
    }


    locationManager = [[CLLocationManager alloc]init];
    locationManager.delegate = self;


           [locationManager requestWhenInUseAuthorization];



          [locationManager requestAlwaysAuthorization];



    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.distanceFilter = 5.0f;


    [mapView_ addObserver:self
                   forKeyPath:@"myLocation"
                      options:(NSKeyValueObservingOptionNew |
                               NSKeyValueObservingOptionOld)
                      context:NULL];


    marker.map = mapView_;



    self.view = mapView_;

    [locationManager startUpdatingLocation];

    GMSCircle *geoFenceCircle = [GMSCircle circleWithPosition:  CLLocationCoordinate2DMake(22.2855200, 114.1576900) radius:1400];
    geoFenceCircle.tappable = true;
    [geoFenceCircle setFillColor:[UIColor colorWithRed:1 green:0 blue:0 alpha:.5]];

    geoFenceCircle.strokeWidth = 13;
    geoFenceCircle.strokeColor = [UIColor orangeColor];
    geoFenceCircle.map = mapView_; // Add it to the map.


    NSLog(@"assadsd configured d map");

}


- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context {
    if (!firstLocationUpdate_) {
        // If the first location update has not yet been recieved, then jump to that
        // location.
        firstLocationUpdate_ = YES;
        CLLocation *location = [change objectForKey:NSKeyValueChangeNewKey];
        mapView_.camera = [GMSCameraPosition cameraWithTarget:location.coordinate
                                                         zoom:14];
    }
}


- (void)dealloc {
    [mapView_ removeObserver:self
                  forKeyPath:@"myLocation"
                     context:NULL];
}




-(void)mapView:(GMSMapView *)mapView didDragMarker:(GMSMarker *)marker {
    [mapView clear];

    // Re-draw marker
    marker.map = mapView;

    // Create your circle with the new marker
    GMSCircle *circ = [GMSCircle circleWithPosition:marker.position radius:1000];
    circ.fillColor = [UIColor grayColor];
    circ.strokeColor = [UIColor redColor];
    circ.strokeWidth = 5;
    circ.map = mapView;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"buttonIndex:%ld",(long)buttonIndex);

    if (alertView.tag == 121 && buttonIndex == 1)
    {
        //code for opening settings app in iOS 8
        [[UIApplication sharedApplication] openURL:[NSURL  URLWithString:UIApplicationOpenSettingsURLString]];
    }
}


- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {

    NSLog(@"%@",error.userInfo);
    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
        NSLog(@"Please authorize location services");
        return;
    }

    NSLog(@"CLLocationManager error: %@", error.localizedFailureReason);
    return;

}

-(void) handleDoubleTap {
    NSLog(@"location double tap ");
}
-(UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}



// CLLocationDelegate
- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations{
    CLLocation *location = [locations lastObject];

    if (markera == nil) {
        markera = [[GMSMarker alloc] init] ;
        markera.position   = CLLocationCoordinate2DMake(22.86, 111.20);
        markera.groundAnchor = CGPointMake(0.5f, 0.97f); // Taking into account walker's shadow
        markera.map = mapView_;

    }else {
        [CATransaction begin];
        [CATransaction setAnimationDuration:2.0];
        markera.position = location.coordinate;
        [CATransaction commit];

    }


        GMSCameraUpdate *move = [GMSCameraUpdate setTarget:location.coordinate zoom:17];
        [mapView_ animateWithCameraUpdate:move];
}
我已经在我的项目中设置了pinfo.list的以下参数

NSLocationAlwaysUsageDescription->Location是查找您所在位置所必需的 NSLOCATIONWhenUsageDescription->Location是查找您所在位置所必需的

我的测试设备是8.3,iphone6

你的
-(void)locationManager:(CLLocationManager*)管理器没有更新位置:(NSArray*)位置{
被呼叫了吗

如果没有,则需要确保
MapViewController
实现
CLLocationDelegate
(例如:
@interface ViewController:UIViewController
MapViewController.h
文件中)

您可以从GitHub或本指南中进行尝试

此外,您应该在真实设备中测试它,否则您必须在XCode模拟器中模拟一个位置(参见下图,但它可能并不总是有效)

您必须将
nslocation当不使用usagedescription
NSLocationAlwaysUsageDescription
添加到
info.plist
文件时(注意:不要重命名或更改
info.plist
文件的扩展名,扩展名必须是
.plist
):

您的
-(void)locationManager:(CLLocationManager*)经理是否已更新位置:(NSArray*)位置{
是否已被呼叫

如果没有,则需要确保
MapViewController
实现
CLLocationDelegate
(例如:
@interface ViewController:UIViewController
MapViewController.h
文件中)

您可以从GitHub或本指南中进行尝试

此外,您应该在真实设备中测试它,否则您必须在XCode模拟器中模拟一个位置(参见下图,但它可能并不总是有效)

您必须将
nslocation当不使用usagedescription
NSLocationAlwaysUsageDescription
添加到
info.plist
文件时(注意:不要重命名或更改
info.plist
文件的扩展名,扩展名必须是
.plist
):


我已经实现了CLLocationManagerDelegate,但从未调用过didUpdateLocations方法,并且didChangeAuthorizationStatus中的状态始终变为0,请在真实设备(不是模拟器)中尝试,然后尝试输入answer.ld:library not found for-lPods clang:error:linker命令失败,退出代码为1(使用-v查看调用)您应该双击打开
GoogleMapsObjCPod.xcworkspace
,而不是
GoogleMapsObjCPod.xcodeproj
,我知道,但它仍然显示以下错误。我试图将您的编码转移到我的工作部件中。它仍然失败。我已经实现了CLLocationManagerDelegate,但从未调用过didUpdateLocations方法,并且didChangeAuthorizationStatus中的s始终变为0,请在真实设备(不是模拟器)中尝试,然后尝试输入答案。ld:library not found for-lPods clang:error:linker命令失败,退出代码为1(使用-v查看调用)您应该双击打开
GoogleMapsObjCPod.xcworkspace
,而不是
GoogleMapsObjCPod.xcodeproj
,我知道,但它仍然显示以下错误。我试图将您的编码移动到我的工作部件中。它仍然失败