无法在城市环境中使用iPhone查找位置

时间:2011-03-27 20:58:11

标签: iphone objective-c cocoa-touch core-location

我们正在开发iPhone应用程序,需要知道用户的大致位置。我们正在使用CoreLocation。我们可以在郊区环境中做到这一点,但在纽约市 - 我们无法获得任何经度/纬度信息。

我们怀疑在高层建筑中GPS信号很弱......但我们是否仍然无法获得一些大概的位置信息呢?

关于可能导致/解决此问题的任何想法?感谢。

#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

2 个答案:

答案 0 :(得分:0)

你有数据连接吗?我听说由于建筑物和网络过载,在曼哈顿的某些网络上建立连接有时很难。如果您没有获得数据,那么很难获得修复。

答案 1 :(得分:0)

我怀疑当GPS芯片由于高层建筑没有得到任何信号时,你要求类似GPS的准确度:

locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters; // 100 m

尝试评论上述声明并在现场测试 - 看看你得到的坐标有多精确。