开发者社区> 问答> 正文

按下gps按钮后,如何获取用户位置

我想在按下gps按钮时在控制台中输出当前用户的位置。我使用在模拟器中创建的gpx文件来模拟位置。在我的startUpdatingLocation中,我启动位置服务并开始生成应该报告用户当前位置的更新。代码如下

{
    if ([self isRunning]) return;

    // Start location services to get the true heading.
    // Defines the minimum distance the device must move horizontally before update is generated
    locationManager.distanceFilter = 1;
    // Defines the accuracy of the location data = best possible accuracy
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    // Start the generation of updates that report the user´s current location‚
    // .startUpdatingLocation() will keep on calling didUpdateLocations locations: every few seconds or whenever there's location change until you stop it by calling locationManager.stopUpdatingLocation.
    [locationManager startUpdatingLocation];

#if defined(ENABLE_WIFI_LOCALISATION)
    if (scan) {
        NSLOG(@"starting thread %ld", (long)[exitLock condition]);

        [exitLock lock];
        [exitLock unlockWithCondition:THREAD_RUNNING];
        [exitLock lock];
        thread = [[NSThread alloc] initWithTarget:self selector:@selector(scanThread) object:nil];
        [thread start];
    } else {
#endif
        [exitLock lock];
        [exitLock unlockWithCondition:THREAD_RUNNING];
        [exitLock lock];
#if defined(ENABLE_WIFI_LOCALISATION)
    }
#endif

    [self changeState:NMStateAuto];
}

callUpdateLocationDelegate:```- (void)callUpdateLocationDelegate { if (self.delegate) { if ([self.delegate respondsToSelector:@selector(navigationManager:didUpdateToLocation:withMapIds:)]) { #if 0 if (currentLocation == nil) { //CLLocationCoordinate2D location = LocationCoordinate2DMake(50.109981, 8.648460); CLLocationCoordinate2D location = LocationCoordinate2DMake(50.110671, 8.649667); [mapIds removeAllObjects]; [mapIds addObject:[NSNumber numberWithInt:5]]; // overview [mapIds addObject:[NSNumber numberWithInt:7]]; [mapIds addObject:[NSNumber numberWithInt:9]]; // 3.1

            CLLocation *l = [[CLLocation alloc] initWithCoordinate:location altitude:0 horizontalAccuracy:minimumAccuracy verticalAccuracy:0 timestamp:[NSDate date]];
            [self.delegate navigationManager:self
                         didUpdateToLocation:l
                                  withMapIds:mapIds];
            [l autorelease];
            return;
        }

#endif [self.delegate navigationManager:self didUpdateToLocation:currentLocation withMapIds:mapIds]; } } }


didUpdateToLocation```- (void)navigationManager:(NavigationManager *)manager
      didUpdateToLocation:(CLLocation *)newLocation
               withMapIds:(NSArray *)locationMapIds
{
    if (newLocation && waitingForLocation) {
        waitingForLocation = NO;
        self.targetView.hidden = YES;
    }
    if (newLocation && [self haveTargets]) {
        // make sure that the navigation hint is visible although no arrow is visible in current view
        if (![locationMapIds containsObject:self.datasource.map->mapId]) {
            [self getNearestTargetFrom:newLocation.coordinate mapId:[locationMapIds count] > 0 ? [locationMapIds objectAtIndex:0]: [NSString overviewMapId]];
        }
    }
    if (gotoNewLocation) {
        if ([locationMapIds count]) {
            self.targetView.hidden = YES;
            gotoNewLocation = NO;
            if (![[locationMapIds objectAtIndex:0] isEqual:self.datasource.map->mapId]) {
                NSString* link = [NSString stringWithFormat:@"%@://dbh?id=%@",
                                  [Config egcfUrlScheme],
                                  [[locationMapIds objectAtIndex:0] stringByAddingPercentEscapesAlsoForAmpersand]];
                [self performSelector:@selector(navigateToMapLink:) withObject:link afterDelay:0]; // prevent deadlock in NavigationManager
                return;
            }
        } else if (newLocation != nil) {
            self.targetView.hidden = YES;
            locationLayer.hidden = YES;
            gotoNewLocation = NO;
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Navigation not possible",nil) message:NSLocalizedString(@"You are currently outside of the exhibition area.",nil) delegate:nil
                                                  cancelButtonTitle:NSLocalizedString(@"OK",nil) otherButtonTitles:nil];
            [alert show];
            [alert release];
            return;
        }
    }
    if([locationMapIds containsObject:self.datasource.map->mapId])
    {
        CGPoint newPosition = [MapCalcHelper getPointOf:newLocation.coordinate inMap:self.datasource.map];
        CLLocationAccuracy newDeviation = newLocation.horizontalAccuracy;

        if (!locationLayer.hidden &&
            currentPosition.x == newPosition.x &&
            currentPosition.y == newPosition.y &&
            currentDeviation == newDeviation &&
            [MapSettings shared].path == nil) {
            return;
        }

        if (locationLayer.hidden || [MapSettings shared].path != nil) {
            [CATransaction setDisableActions:YES];
            locationLayer.hidden = NO;
        } else {
            [CATransaction setAnimationDuration:2]; 
            [CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
        }

        if (newDeviation != currentDeviation || deviationLayer.path == NULL) {
            if (deviationLayer.path == NULL) {
                [self setDeviationPath];
            }
            [self setDeviationSize:newDeviation];
        }

        locationLayer.position = newPosition;

        if (currentPosition.x != newPosition.x ||
            currentPosition.y != newPosition.y || [MapSettings shared].path != nil) {
            if ([MapSettings shared].path != nil) {
                [self handleUpdatedRoutingLocation:newLocation position:newPosition];
            } else if ([self haveTargets]) {
                DistAndAzimuth nearest = [self getNearestTargetFrom:newLocation.coordinate mapId:(locationMapIds && [locationMapIds count]) > 0 ? [locationMapIds objectAtIndex:0]:self.datasource.map->mapId];

                if (nearest.distance <= 1) {
                    arrowLayer.hidden = YES;
                    pointLayer.hidden = NO;
                } else {
                    [self setArrowTransform:(nearest.azimuth * M_PI / 180.0)];

                    arrowLayer.hidden = NO;
                    pointLayer.hidden = YES;
                }

            } else {
                arrowLayer.hidden = YES;
                pointLayer.hidden = NO;
            }
        }


        currentPosition = newPosition;
        currentDeviation = newDeviation;
    }
    else
    {
        locationLayer.hidden = YES;
        if ([MapSettings shared].path != nil) {
            CGPoint newPosition = [MapCalcHelper getPointOf:newLocation.coordinate inMap:self.datasource.map];
            [self handleUpdatedRoutingLocation:newLocation position:newPosition];
        }
    }
}

当我按下gpsBtn时,它将返回当前位置

    NSLog(@"GPS Button pressed");

}

展开
收起
南南唐语 2019-12-02 20:44:06 590 0
0 条回答
写回答
取消 提交回答
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载