对于那些对基于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];
}
}
任何想法都会令人感激。提前谢谢。
答案 0 :(得分:2)
分配locationManager时,应设置距离过滤器:
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
locationManager.distanceFilter = 100.0;
如果你没有设置距离过滤器,它将采用默认值kCLDistanceFilterNone,你会收到有关所有动作的通知(即方法didUpdateLocation将被频繁调用)
答案 1 :(得分:0)
除了接受的答案外,您还需要在iOS 8中添加以下代码来获取lat / long。
if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
{
[locationManager requestWhenInUseAuthorization];
}
您需要在plist文件中添加NSLocationWhenInUseUsageDescription
键。
阅读this link了解详情。