2012-09-25 53 views
1

这对于那些对基于GPS的应用程序感兴趣的人可能很简单。在我的应用程序中,我试图跟踪用户的位置,并且必须在地图上绘制用户的路径。在每个更新的点上,我必须填充一个数组,其中包含纬度,经度,海拔高度,当前速度,特定位置的距离等数据。但是,当我在设备上尝试时,我的应用程序在采用这些值时崩溃。认为这种方法每秒调用超过10次。在iPhone中使用GPS跟踪位置崩溃的应用程序

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

而且考虑所有这些散装值以非常快的速度使issue.I试图执行一个条件,这个方法使阵列应在5个seconds.this的时间间隔更新是我试过。

- (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]; 


     } 



    } 

任何想法都是可观的。提前致谢。

+0

向我们展示了崩溃的日志报告。 – vishy

+0

实际上,我无法在实际测试中注销它。移动一段距离后,整个用户界面将被挂起,并在稍后崩溃。 –

+0

你解决了吗? –

回答

2

当你分配你的LocationManager你应该设定一个距离滤波器:

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

如果不设置距离过滤器是采用默认值kCLDistanceFilterNone,你将被告知所有运动(即方法didUpdateLocation将被称为非常频繁)

+0

我已经给它这样locMgr.distanceFilter = kCLDistanceFilterNone;在分配时。 –

+0

这个设置没有任何意义,因为我说 - 这是默认值.. – Stas

+0

好吧,我现在会尝试 –

0

除了公认的答案,你需要添加以下代码经/纬来获取iOS中8

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

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

阅读this link了解更多详情。