【问题标题】:Geolocation doesn't work even on device地理定位即使在设备上也不起作用
【发布时间】:2011-04-03 15:14:00
【问题描述】:

我在模拟器上苦苦挣扎,但最近,我直接在设备 (iPhone) 上对其进行了测试,但同样的问题仍然存在,这是我的应用程序完成启动后得到的结果,并且地理定位代码假设给了我我的地图上的位置(请记住,上图是我在 iPhone 上的应用,而不是模拟器):

所以对于我的代码,我的应用程序实际上有很多视图,但是关于这个功能,我已经研究了 appdelegate(.m 和 .h) 以及关于在地图上显示位置的视图 (PositionActuelleViewController)。

appdelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    sleep(3);




    // Override point for customization after application launch.

    self.locationManager=[[[CLLocationManager alloc] init] autorelease];

    if([CLLocationManager locationServicesEnabled]) {
        self.locationManager.delegate=self;
        self.locationManager.distanceFilter=100;
        [self.locationManager startUpdatingLocation];

    }


        // Add the view controller's view to the window and display.
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];

    return YES;
}

在dealloc方法之后的同一个文件中:

     #pragma mark CLLocationManagerDelegate Methods


    -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation    {

        MKCoordinateSpan span;
        span.latitudeDelta=0.2;
        span.longitudeDelta=0.2;


        MKCoordinateRegion region;
        region.span=span;
        region.center=newLocation.coordinate;
        [viewCont.mapView setRegion:region animated:YES];

        viewCont.mapView.showsUserLocation=YES;
        viewCont.latitude.text=[NSString stringWithFormat:@"%f",newLocation.coordinate.latitude];
        viewCont.longitude.text=[NSString stringWithFormat:@"%f",newLocation.coordinate.longitude];

}

我的 PositionActuelleViewController.m 文件:

#import "PositionActuelleViewController.h"
@implementation PositionActuelleViewController

@synthesize mapView;
@synthesize latitude;
@synthesize longitude;

-(IBAction)goBackToMenu {
    [self dismissModalViewControllerAnimated:YES];
}
-(IBAction)goToRechercherView;
{
    rechercherViewController.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;

    [self  presentModalViewController:rechercherViewController animated:YES];

}

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

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

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)dealloc {
    [mapView release];
    [latitude release];
    [longitude release];
    [super dealloc];
}
@end

我在这里错过了什么?

【问题讨论】:

  • 希望你不要忘记在IB中附上mapView
  • 我做了,如果没有附加它就不会被实现,当视图加载时它会退出应用程序:)
  • 好的,我刚刚在 IB 中检查了它,它已附加到我的文件所有者 :)

标签: iphone objective-c geolocation


【解决方案1】:

委托方法-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 是否被调用过?也许如果您取消注释 self.locationManager.distanceFilter=100; 它将起作用,因为根据文档,distanceFilter 属性定义了设备在生成更新事件之前必须横向移动的最小距离(以米为单位)。

【讨论】:

  • 嗨 kakoSquid,我已取消注释 self.locationManager.distanceFilter=100;声明并尝试构建以便显示我的地图,我等了一会儿希望它可以工作,但是应用程序停止了,我发现我自己了:(这是否意味着当我在 iPhone 上使用此修改移动此构建时这可能有效??谢谢
  • 我认为这可能是导致您的问题的原因...另外,请取消注释 sleep(3),这可能会导致行为不稳定
  • sleep(3) 用于我的启动画面。
  • 顺便说一句,locationManager 方法在 appdelegate.m 中,我应该在应该显示地图的视图的 - (void)viewDidLoad 中调用它吗??
  • 哪个类是 CLLocationManager 的委托并不重要,只要它符合 CLLocationManagerDelegate 并实现委托方法即可。还要检查是否启用了位置服务:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-17
  • 2016-01-04
  • 2012-02-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多