我想在按下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");
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。