これは、GPSベースのアプリケーションに興味がある人にとっては簡単かもしれません。私のアプリでは、ユーザーの位置を追跡し、地図上にユーザーのパスを描画する必要があります。したがって、更新された各ポイントで、次のような配列を入力する必要があります。緯度、経度、高度、現在の速度、特定の場所からの移動距離などの値がありますが、デバイスで試してみると、これらの値を取得しているときにアプリがクラッシュします。このメソッドは1秒間に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];
}
}
どんなアイデアでも評価できるでしょう。前もって感謝します。