对于那些对基于 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];
}
}
任何想法都将是可观的。提前致谢。