2011-03-27 48 views
2

我們正在開發iPhone應用程序,需要知道用戶的大致位置。我們正在使用CoreLocation。我們可以在郊區環境中做到這一點,但在紐約市 - 我們無法獲得任何經度/緯度信息。在城市環境中用iPhone查找位置

我們懷疑它的cos信號在高原地區是弱的......但是我們不應該仍然能夠獲得一些大概的位置信息嗎?

有關可能導致/解決此問題的任何想法?謝謝。

#import "RootViewController.h" 
#import "SlickEatsAppDelegate.h" 
#import "SlickEatsWelcomePage.h" 
#import "XMLParserShowTodaysOffer.h" 
#import "SlickEatsSplashScreen.h" 

@implementation RootViewController 
@synthesize locationManager,currentLocation,appDelegate; 
@synthesize getStartedButton,splashLogoImageView,howItWorkImageView,workButton,infoView; 
@synthesize xmlParserShowTodaysOffer;//activityIndicator 
#pragma mark - 
#pragma mark View lifecycle 



- (void)viewDidLoad { 
    [super viewDidLoad]; 

    [self connectedToNetwork]; 

    self.navigationController.navigationBar.hidden = TRUE; 
    if(locationManager == nil) 
    { 
     [[self locationManager] startUpdatingLocation]; 
    } 

    //[self.view addSubview:SlickEatsView]; 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem; 
} 



- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 
} 

/*-(IBAction)getStartedButton_Clicked:(id)sender 
{ 
    activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 
    self.activityIndicator.frame = CGRectMake(135, 150, 40, 40); 

    [self.view addSubview:activityIndicator]; 
    if (self.activityIndicator.isAnimating == NO) 
    { 
     [self.activityIndicator startAnimating]; 
    } 
    /*if(locationManager == nil) 
    { 
     [[self locationManager] startUpdatingLocation]; 
    }*/ 
    /*SlickEatsWelcomePage *welcomePage = [[SlickEatsWelcomePage alloc]initWithNibName:@"SlickEatsWelcomePage" bundle:nil]; 
    appDelegate = (SlickEatsAppDelegate *)[[UIApplication sharedApplication]delegate]; 

    xmlParserShowTodaysOffer = [XMLParserShowTodaysOffer alloc]; 
    xmlParserShowTodaysOffer.currentCity = appDelegate.myCurrentCity; 
    xmlParserShowTodaysOffer.currentLatitude = appDelegate.myCurrentLatitude; 
    xmlParserShowTodaysOffer.currentLongitude = appDelegate.myCurrentLongitude; 
    [xmlParserShowTodaysOffer initXMLParser]; 
    [xmlParserShowTodaysOffer release]; 

    [self.navigationController pushViewController:welcomePage animated:YES]; 
    [welcomePage release]; 
    [activityIndicator removeFromSuperview]; 
    //[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"9763912503"]]; 
}*/ 
- (CLLocationManager *)locationManager { 


    locationManager = [[CLLocationManager alloc] init]; 
    locationManager.delegate = self; 
    locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move 
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters; // 100 m 
    [locationManager startUpdatingLocation]; 
    return locationManager; 


} 

#pragma mark - 
#pragma mark CLLocationManagerDelegate Methods 

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
      fromLocation:(CLLocation *)oldLocation { 
    appDelegate = (SlickEatsAppDelegate *)[[UIApplication sharedApplication]delegate]; 
    int degrees = newLocation.coordinate.latitude; 
    double decimal = fabs(newLocation.coordinate.latitude - degrees); 
    int minutes = decimal * 60; 
    double seconds = decimal * 3600 - minutes * 60; 
    NSString *lat = [NSString stringWithFormat:@"%d° %d' %1.4f\"", 
        degrees, minutes, seconds]; 

    //latLabel.text = lat; 
    NSLog(@"Current..Latitude::%@",lat); 
    NSString *CurrentLatitude = [NSString stringWithFormat:@"%lf",newLocation.coordinate.latitude]; 
    NSLog(@"Current..Latitude::%@",CurrentLatitude); 
    //self.myCurrentLatitude=lat; 

    degrees = newLocation.coordinate.longitude; 
    decimal = fabs(newLocation.coordinate.longitude - degrees); 
    minutes = decimal * 60; 
    seconds = decimal * 3600 - minutes * 60; 
    NSString *longt = [NSString stringWithFormat:@"%d° %d' %1.4f\"", degrees, minutes, seconds]; 
    //longLabel.text = longt; 
    NSLog(@"Current..Longitude::%@",longt); 

    NSString *CurrentLongitude = [NSString stringWithFormat:@"%lf",newLocation.coordinate.longitude]; 
    NSLog(@"Current..Longitude::%@",CurrentLongitude); 

    appDelegate.myCurrentLatitude = CurrentLatitude; 
    appDelegate.myCurrentLongitude = CurrentLongitude; 


    MKReverseGeocoder *geoCoder = [[MKReverseGeocoder alloc] initWithCoordinate:newLocation.coordinate]; 
    geoCoder.delegate = self; 
    [geoCoder start]; 

} 

// this delegate is called when the reverseGeocoder finds a placemark 
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark 
{ 
    MKPlacemark * myPlacemark = placemark; 
    // with the placemark you can now retrieve the city name 
    NSString *city =[ myPlacemark.addressDictionary objectForKey:(NSString*) kABPersonAddressCityKey]; 
    // NSString *city = (NSString *)[myPlacemark.locality length]; 
    NSLog(@"Current Add::%@",city); 
    appDelegate.myCurrentCity = city; 

    //[self sendLocation]; 
} 

// this delegate is called when the reversegeocoder fails to find a placemark 
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error 
{ 
    NSLog(@"reverseGeocoder:%@ didFailWithError:%@", geocoder, error); 
} 



- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 
{ 
    NSLog(@"error%@",error); 
    switch([error code]) 
    { 
     case kCLErrorNetwork: // general, network-related error 
     { 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"please check your network connection or that you are not in airplane mode" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; 
      [alert show]; 
      [alert release]; 
     } 
      break; 
     case kCLErrorDenied:{ 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"user has denied to use current Location " delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; 
      [alert show]; 
      [alert release]; 
     } 
      break; 
     default: 
     { 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"unknown network error" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; 
      [alert show]; 
      [alert release]; 
     } 
      break; 
    } 
} 


-(void)sendLocation 
{ 

    NSLog(@"in Send Location.."); 

    SlickEatsWelcomePage *welcomePage = [[SlickEatsWelcomePage alloc]initWithNibName:@"SlickEatsWelcomePage" bundle:nil]; 
    appDelegate = (SlickEatsAppDelegate *)[[UIApplication sharedApplication]delegate]; 

    xmlParserShowTodaysOffer = [XMLParserShowTodaysOffer alloc]; 
    xmlParserShowTodaysOffer.currentCity = appDelegate.myCurrentCity; 
    xmlParserShowTodaysOffer.currentLatitude = appDelegate.myCurrentLatitude; 
    xmlParserShowTodaysOffer.currentLongitude = appDelegate.myCurrentLongitude; 
    [xmlParserShowTodaysOffer initXMLParser]; 
    [xmlParserShowTodaysOffer release]; 

    [self.navigationController pushViewController:welcomePage animated:YES]; 
    [welcomePage release]; 
    //[locationManager release]; 
} 

-(IBAction)workButton_clicked:(id)sender 
{ 
    /*activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 
    self.activityIndicator.frame = CGRectMake(135, 150, 40, 40); 

    [self.splashLogoImageView addSubview:activityIndicator]; 
    if (self.activityIndicator.isAnimating == NO) 
    { 
     [self.activityIndicator startAnimating]; 
    }*/ 
    SlickEatsSplashScreen *intro = [[SlickEatsSplashScreen alloc]initWithNibName:@"SlickEatsSplashScreen" bundle:nil]; 
    [self.navigationController pushViewController:intro animated:YES]; 
    [intro release]; 
    //[activityIndicator removeFromSuperview]; 
} 

- (void)connectedToNetwork { 

    BOOL aflag= ([NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.google.co.in/"]]!=NULL)?YES:NO; 



    if (!aflag) { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Sorry!....You are not connected to network " 
           delegate:self cancelButtonTitle:@"Exit" otherButtonTitles:nil]; 
     [alert show]; 
     [alert release]; 

    } 
} 


/* 
- (void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 
} 
*/ 
/* 
- (void)viewWillDisappear:(BOOL)animated { 
    [super viewWillDisappear:animated]; 
} 
*/ 
/* 
- (void)viewDidDisappear:(BOOL)animated { 
    [super viewDidDisappear:animated]; 
} 
*/ 

/* 
// Override to allow orientations other than the default portrait orientation. 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Return YES for supported orientations. 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 
*/ 




/* 
// Override to support conditional editing of the table view. 
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Return NO if you do not want the specified item to be editable. 
    return YES; 
} 
*/ 


/* 
// Override to support editing the table view. 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 

    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the row from the data source. 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
    else if (editingStyle == UITableViewCellEditingStyleInsert) { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view. 
    } 
} 
*/ 


/* 
// Override to support rearranging the table view. 
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { 
} 
*/ 


/* 
// Override to support conditional rearranging of the table view. 
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Return NO if you do not want the item to be re-orderable. 
    return YES; 
} 
*/ 




#pragma mark - 
#pragma mark Memory management 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Relinquish ownership any cached data, images, etc that aren't in use. 
} 

- (void)viewDidUnload { 
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand. 
    // For example: self.myOutlet = nil; 
} 


- (void)dealloc { 
    [super dealloc]; 
    [workButton release]; 
    [howItWorkImageView release]; 
    [splashLogoImageView release]; 
    [getStartedButton release]; 
    [locationManager release]; 
} 


@end 
+0

你有什麼模型的iOS設備有問題嗎?因爲早期的iPhone沒有GPS。 – 2011-03-27 22:09:30

+0

我們正在測試iphone 4s ... – dazhi 2011-03-27 22:16:32

+0

即使是iPhone 4,你也應該得到一個基於最近的手機塔的位置。你沒有得到任何東西嗎? – 2011-03-27 22:18:52

回答

0

我懷疑你正在請求時,GPS芯片是沒有得到任何信號類似GPS的精確度,由於高層建築:

locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters; // 100 m 

嘗試註釋掉上面的語句和測試出來的字段 - 瞭解您獲得的座標的精確程度。

+0

感謝您的建議,它似乎有道理。我在iOS開發人員庫上看到了有關desiredAccuracy的所有內容: 此屬性的默認值爲kCLLocationAccuracyBest。 將精度設置爲百萬或千米是否更好? – dazhi 2011-03-28 03:53:35

+0

我們將精度更改爲1公里,但它沒有幫助.. – dazhi 2011-03-28 07:45:49

+0

您是否嘗試過使用kCLLocationAccuracyBest並查看從CLLocation.horizo​​ntalAccuracy獲得的實際精度? – adib 2011-04-26 06:06:30

0

你有數據連接嗎?我聽說,由於建築物和網絡過載,有時候在曼哈頓的某些網絡上連接有時很難。如果您沒有獲取數據,那麼得到修復將會非常困難。

+0

是的,肯定有數據連接。谷歌地圖,foursquare等所有工作,並正確顯示我的位置... – dazhi 2011-03-27 22:12:19