在 iPhone 崩溃应用中使用 GPS 跟踪位置

Posted

技术标签:

【中文标题】在 iPhone 崩溃应用中使用 GPS 跟踪位置【英文标题】:Tracking the location using GPS in iPhone crashes app 【发布时间】:2012-09-25 05:56:17 【问题描述】:

对于那些对基于 GPS 的应用程序感兴趣的人来说,这可能很简单。在我的应用程序中,我试图跟踪用户的位置,并且必须在地图上绘制用户的路径。所以在每个更新点上,我必须填充一个具有纬度、经度、高度、当前速度、从该特定位置行进的距离等值的数组。但是当我在设备上尝试它时,我的应用程序在获取这些值时崩溃了。我认为这种方法调用了 10 次以上每秒。

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


以非常快的速度获取所有这些批量值会导致问题。我尝试在此方法中实现一个条件,以便数组应以 5 秒的时间间隔更新。这就是我尝试过的。

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

      CLLocation*  updatedLocation = [newLocation retain];
        NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
        NSDate *myDate = (NSDate *)[prefs objectForKey:@"myDateKey"];

        NSDate *lastDate = (NSDate *)newLocation.timestamp;
        NSTimeInterval theDiff = [lastDate timeIntervalSinceDate:myDate];

       if (theDiff > 5.0f || myDate == nil)
            //do your webservices stuff here



          if (newLocation.horizontalAccuracy < 0)
        
            // No Signal
            if([self conformsToProtocol:@protocol(CoreLocationControllerDelegate)])
            

                [self gpsSignalStatus:@"No Signal"];
            

        
        else if (newLocation.horizontalAccuracy > 65)
        
            // Poor Signal
            if([self conformsToProtocol:@protocol(CoreLocationControllerDelegate)])
            

                [self gpsSignalStatus:@"Poor Signal"];
            
        
        else if (newLocation.horizontalAccuracy <= 65.0)
        
            // Fair
            if([self conformsToProtocol:@protocol(CoreLocationControllerDelegate)])
            

                [self gpsSignalStatus:@"Fair"];
            

        
        else if (newLocation.horizontalAccuracy <= 20.0)
        
            // Good
            if([self conformsToProtocol:@protocol(CoreLocationControllerDelegate)])
            

                [self gpsSignalStatus:@" Good"];
            

        

        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];
        latitudeLabel.text = 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];
        longitudeLabel.text = longt;
        speedLabel.text = [NSString stringWithFormat:@"SPEED: %f", [newLocation speed]];
        altitudeLabel.text = [NSString stringWithFormat:@"ALTITUDE: %f", [newLocation altitude]];
        [mapView removeAnnotations:[mapView annotations]];
        MKPointAnnotation *pa = [[MKPointAnnotation alloc] init];
        pa.coordinate = newLocation.coordinate;
        pa.title = @"Current Location";
        [mapView addAnnotation:pa];
        [pa release];
        NSString *latlongString = [NSString stringWithFormat:@"%f,%f", newLocation.coordinate.latitude,newLocation.coordinate.longitude];
        [latLongArray addObject:latlongString];
        NSLog(@"%@",latLongArray);
        NSLog(@"Speed :: %g meters/sec",newLocation.speed);


        [points addObject:newLocation];
        NSInteger tot = [points count];
        MKMapPoint* pointsToUse=malloc(sizeof(CLLocationCoordinate2D) * tot);
        if(tot>1)
        
            //i=tot-1;i<tot;i++
            for(int i=0;i<tot;i++)
            

                CLLocation *Loc=[points objectAtIndex:i];
                CLLocationDegrees latitude = Loc.coordinate.latitude;
                //   [TravellingCoordinates addObject:location];
                CLLocationDegrees longitude = Loc.coordinate.longitude;
                CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude);
                MKMapPoint point = MKMapPointForCoordinate(coordinate);
                pointsToUse[i] = point;




                NSString *latitudestr = [[NSString alloc] initWithFormat:@"%g", newLocation.coordinate.latitude];
                NSLog(@"Latitude: %@", latitudestr);


                NSString *longitudestr = [[NSString alloc] initWithFormat:@"%g", newLocation.coordinate.longitude];
                NSLog(@"Longitude: %@", longitudestr);

                NSString *altitudestr = [[NSString alloc] initWithFormat:@"%g",newLocation.altitude];
                NSLog(@"altitude: %@", altitudestr);

                NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
                [formatter setDateFormat: @"HH:mm:ss"];
                NSString *LocationTime=[formatter stringFromDate:newLocation.timestamp];
                NSLog(@"LocationTime:%@",LocationTime);
                NSTimeInterval timeSinceLastLocation = [oldLocation.timestamp timeIntervalSinceDate:newLocation.timestamp];
                CGFloat speed = newLocation.speed;
                NSString *speedstr=[[NSString alloc]initWithFormat:@"%f",speed];
                NSLog(@"%@",speedstr);

                CLLocationDistance distance=0;;
                distance=distance+[newLocation distanceFromLocation:oldLocation];
                NSString *distancestr=[[NSString alloc]initWithFormat:@"%f",distance];

               // NSString *distancestr=[[NSString alloc]initWithFormat:@"%f",distance];

                [firstJsonDictionary setObject:latitudestr forKey:@"latitude"];
                [firstJsonDictionary setObject:longitudestr forKey:@"longitude"];
                [firstJsonDictionary setObject:altitudestr forKey:@"altitude"];
                [firstJsonDictionary setObject:LocationTime forKey:@"timestamp"];
                [firstJsonDictionary setObject:speedstr forKey:@"speed"];
                [firstJsonDictionary setObject:distancestr forKey:@"distance"];

                for(int i=0;i<10;i++)

                    [arr addObject:firstJsonDictionary];
                    // [firstJsonDictionary release];
                
                NSError *error=nil;
                NSData *jsonData2 = [NSJSONSerialization dataWithJSONObject:arr options:NSJSONWritingPrettyPrinted error:&error];
                NSString *jsonString = [[NSString alloc] initWithData:jsonData2 encoding:NSUTF8StringEncoding];
              //  NSLog(@"jsonData as string:\n%@", jsonString);
                NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
                [defaults setObject:jsonString forKey:@"jsonarraydata"];



                 

            

        // create the polyline based on the array of points.
        _routeLine = [MKPolyline polylineWithPoints:pointsToUse count:tot];
        [mapView addOverlay:_routeLine];
        free(pointsToUse);
           NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
           [prefs setObject:lastDate forKey:@"myDateKey"];
           [prefs synchronize];

           [self distanceTravelled:newLocation];
       [self loadRoute];


       



    

任何想法都会很明显。提前致谢。

【问题讨论】:

向我们展示崩溃的日志报告.. 实际上我无法在现场测试中将其注销。移动一段距离后,整个ui被挂起,稍后它会崩溃。 【参考方案1】:

当你分配你的 locationManager 时,你应该设置一个距离过滤器:

CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
locationManager.distanceFilter = 100.0;

如果您不设置距离过滤器,它将采用默认值 kCLDistanceFilterNone 并且您将收到有关所有移动的通知(即方法 didUpdateLocation 将被非常频繁地调用)

【讨论】:

我已经给了它这样的 locMgr.distanceFilter = kCLDistanceFilterNone;在分配时。 这是什么意思?locationManager.distanceFilter = 100.0; 它是“在生成更新事件之前设备必须水平移动的最小距离(以米为单位)。” 但是同样的事情发生了..在移动了大约 500 米后,整个应用程序挂起。我在视图中设置了一个计时器。移动这么远的距离后,计时器也没有更新时间跨度> 您应该提供崩溃日志 - 在模拟器上测试您的应用(您可以在 ios 模拟器中模拟移动 - 调试 - 位置 - 城市运行)【参考方案2】:

除了接受的答案外,您还需要添加以下代码以在 iOS 8 中获取 lat/long。

if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
    
        [locationManager requestWhenInUseAuthorization];
    

并且您需要在您的 plist 文件中添加 NSLocationWhenInUseUsageDescription 键。

阅读this link了解更多详情。

【讨论】:

以上是关于在 iPhone 崩溃应用中使用 GPS 跟踪位置的主要内容,如果未能解决你的问题,请参考以下文章

即使用户没有打开应用程序,是不是可以在 android 中跟踪 GPS 位置

由于使用 GPS 获取当前位置较晚导致应用程序崩溃

如何在我自己的应用程序中追踪另一部 iPhone?我需要使用 icloud 或 gps 吗? [关闭]

iPhone 网络应用程序可以获取 GPS 位置吗?

如何在 Android 中使用 gps 获取当前位置(街道、城市等)

核心位置的准确性