打开应用程序时获取当前位置
Posted
技术标签:
【中文标题】打开应用程序时获取当前位置【英文标题】:fetch current location while opening the app 【发布时间】:2014-08-07 00:54:09 【问题描述】:嗨,在我的应用程序中,我必须显示从当前位置到目的地位置的路线图。为此,我使用google map
URL 来显示路线图,一切正常。但问题是当第一次安装应用程序时,它会获得现金并且它没有获取当前位置。
当我最小化应用程序时,它会显示警报消息。
"appname" would like to use Your current Location
点击确定后,我关闭了应用程序并再次打开它,现在它显示了路线图。
我的代码。
-(void)currentlocation
locationManager = [[CLLocationManager alloc]init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
[self->locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];
mapView = [[[MapView alloc] initWithFrame:
CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)] autorelease];
[self.view addSubview:mapView];
Place* home = [[[Place alloc] init] autorelease];
home.name = @"Home";
home.description = @"Sweet home";
home.latitude = 13.0051850;
home.longitude = 77.62698590;
Place* office = [[[Place alloc] init] autorelease];
office.name = @"Office";
office.description = @"Bad office";
office.latitude = coordinate.latitude;
office.longitude = coordinate.longitude;
[mapView showRouteFrom:home to:office];
我已经使用了我想要的上面的代码,当用户打开应用程序时它必须显示警报,一旦用户单击确定它就会显示路线图请告诉我如何实现我已经卡在这里很长时间了有时间请帮帮我。
谢谢。
【问题讨论】:
【参考方案1】:试试这个吧..
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil)
Place* home = [[[Place alloc] init] autorelease];
home.name = @"Home";
home.description = @"Sweet home";
home.latitude = 13.0051850;
home.longitude = 77.62698590;
Place* office = [[[Place alloc] init] autorelease];
office.name = @"Office";
office.description = @"Bad office";
office.latitude = currentLocation.coordinate.latitude;
office.longitude = currentLocation.coordinate.longitude;
[mapView showRouteFrom:home to:office];
// Stop Location Manager
[locationManager stopUpdatingLocation];
【讨论】:
以上是关于打开应用程序时获取当前位置的主要内容,如果未能解决你的问题,请参考以下文章