我正在开发通用应用。在这里,我正在开发gps地图概念,它在iphone中工作正常,但不适用于iPad。我的要求是我通过使用GPS和放大器来获取当前位置。我给了一个目的地,我有一条标记线从我当前的位置到我的目的地(例如:banglore(当前位置)到孟买(目的地))。
这是在iphone正常工作,但在iPad不工作,它只显示谷歌地图。我需要对iPad实现我的要求,这也是我的代码库。
请你帮我解决这个问题。
- (NSString *)stringWithDistance:(double)distance {
NSLog(@"%f",distance);
NSString *format;
if (distance < METERS_CUTOFF) {
format = @"%@ metres";
} else {
format = @"%@ km";
distance = distance / 1000;
}
return [NSString stringWithFormat:format, [self stringWithDouble:distance]];
}
// Return a string of the number to one decimal place and with commas & periods based on the locale.
- (NSString *)stringWithDouble:(double)value {
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setLocale:[NSLocale currentLocale]];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setMaximumFractionDigits:2];
return [numberFormatter stringFromNumber:[NSNumber numberWithDouble:value]];
}
- (void)viewDidLoad
{
[super viewDidLoad];
deviceType = [UIDevice currentDevice].model;
NSLog(@"%@",school11);
NSLog(@"lat%@",lat);
NSLog(@"%@device",deviceType);
if (!isiPhone5)
{
// self.footer.frame=CGRectMake(0, 448, 320, 32);
}
NSLog(@"Current identifier: %@", [[NSBundle mainBundle] bundleIdentifier]);
// Do any additional setup after loading the view, typically from a nib.
}
-(void)viewWillAppear:(BOOL)animated
{
if (!isiPhone5)
{
// self.footer.frame=CGRectMake(0, 448, 320, 32);
}
[self currentlocation];
}
-(void)currentlocation{
locationManager = [[CLLocationManager alloc]init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
if ([deviceType isEqualToString:@"iPad"]) {
mapView = [[[MapView alloc] initWithFrame:
CGRectMake(0, 50, 768, 1000)] autorelease];
[self.view addSubview:mapView];
}
else{
if (!isiPhone5)
{
mapView = [[[MapView alloc] initWithFrame:
CGRectMake(0, 50, 320, 450)] autorelease];
[self.view addSubview:mapView];
}
else{
mapView = [[[MapView alloc] initWithFrame:
CGRectMake(0, 50, 320, 800)] autorelease];
[self.view addSubview:mapView];
}
}
CLLocation *locA = [[CLLocation alloc] initWithLatitude:13.2339538 longitude:80.3323613];
CLLocation *locB = [[CLLocation alloc] initWithLatitude:28.889816 longitude:77.3418147];
CLLocationDistance distance = [locA distanceFromLocation:locB];
NSLog(@"%f Meters",distance);
NSLog(@"%@", [self stringWithDistance:distance]);
}
- (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];
}
// CLLocati
- (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 = @"Vivero International Pre-School";
// home.description = @"Coyaji Rd, Nilanjali Society, Kalyani Nagar Pune,Maharashtra 411006 India";
CLLocationCoordinate2D center;
center=[self getLocationFromAddressString:@"madurai"];
latFrom=¢er.latitude;
lonFrom=¢er.longitude;
double latt = [lat floatValue];
double lann = [lon floatValue];
home.latitude=latt;
home.longitude=lann;
CLLocation *locA = [[CLLocation alloc] initWithLatitude:home.latitude longitude:home.longitude];
Place* office = [[[Place alloc] init] autorelease];
office.name = @"Office";
office.description = @"current";
office.latitude = currentLocation.coordinate.latitude;
office.longitude = currentLocation.coordinate.longitude;
[mapView showRouteFrom:home to:office];
CLLocation *locB = [[CLLocation alloc] initWithLatitude:office.latitude longitude:office.longitude];
CLLocationDistance distance = [locA distanceFromLocation:locB];
NSLog(@"%f Meters",distance);
NSLog(@"%@", [self stringWithDistance:distance]);
}
// Stop Location Manager
[locationManager stopUpdatingLocation];
}
- (void)dealloc {
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(CLLocationCoordinate2D) getLocationFromAddressString: (NSString*) addressStr {
double latitude = 0, longitude = 0;
NSString *esc_addr = [addressStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *req = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@", esc_addr];
NSLog(@"%@",req);
NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:NULL];
if (result) {
NSScanner *scanner = [NSScanner scannerWithString:result];
if ([scanner scanUpToString:@"\"lat\" :" intoString:nil] && [scanner scanString:@"\"lat\" :" intoString:nil]) {
[scanner scanDouble:&latitude];
if ([scanner scanUpToString:@"\"lng\" :" intoString:nil] && [scanner scanString:@"\"lng\" :" intoString:nil]) {
[scanner scanDouble:&longitude];
}
}
}
CLLocationCoordinate2D center;
center.latitude=latitude;
center.longitude = longitude;
NSLog(@"View Controller get Location Logitute : %f",center.latitude);
NSLog(@"View Controller get Location Latitute : %f",center.longitude);
return center;
}
在iphone中显示它正常工作但在ipad下方法没有调用
- (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 = @"Vivero International Pre-School";
CLLocationCoordinate2D center;
center=[self getLocationFromAddressString:@"madurai"];
latFrom=¢er.latitude;
lonFrom=¢er.longitude;
double latt = [lat floatValue];
double lann = [lon floatValue];
home.latitude=latt;
home.longitude=lann;
CLLocation *locA = [[CLLocation alloc] initWithLatitude:home.latitude longitude:home.longitude];
Place* office = [[[Place alloc] init] autorelease];
office.name = @"Office";
office.description = @"current";
office.latitude = currentLocation.coordinate.latitude;
office.longitude = currentLocation.coordinate.longitude;
[mapView showRouteFrom:home to:office];
CLLocation *locB = [[CLLocation alloc] initWithLatitude:office.latitude longitude:office.longitude];
CLLocationDistance distance = [locA distanceFromLocation:locB];
NSLog(@"%f Meters",distance);
NSLog(@"%@", [self stringWithDistance:distance]);
}
// Stop Location Manager
[locationManager stopUpdatingLocation];
}
最后我在iPad上收到此错误消息:
Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization]
请指导我如何解决此问题。
先谢谢。
答案 0 :(得分:0)
在iOS 8上,您必须使用以下代码。
if (IS_OS_8_OR_LATER)
{
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])
{
[self.locationManager requestAlwaysAuthorization];
}
}
[self.locationManager startUpdatingLocation];
并在info.plist中使用密钥NSLocationAlwaysUsageDescription
并为其提供说明。