2012-09-25 52 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瞭解更多詳情。