In my application, I am using LocationManager for getting location, also I want calculate DISTANCE, SPEED and TIME.
From Location manager I am getting time and distance successfully,But when I calculate the speed from coordinates it is showing wrong values.
For getting location I am using following code.
CLLocationManager *locManager ;
float fltDistanceTravelled;
double calculatedSpeed;
//===============
locManager=[[CLLocationManager alloc] init];
locManager.delegate=self;
locManager.distanceFilter=25.0f;
locManager.desiredAccuracy=kCLLocationAccuracyBestForNavigation;
[locManager startUpdatingLocation];
//=====================
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
if(newLocation && oldLocation)
{
// getting distance
fltDistanceTravelled +=[self getDistanceInKm:newLocation fromLocation:oldLocation];
}
if(oldLocation != nil)
{
CLLocationDistance distanceChange = [newLocation getDistanceFrom:oldLocation];
NSTimeInterval sinceLastUpdate = [newLocation.timestamp timeIntervalSinceDate:oldLocation.timestamp];
// calculate speed
calculatedSpeed= (distanceChange / sinceLastUpdate) * 3.6;
}
}
but this code return wrong values of calculatedSpeed (speed) of the car.
You are already getting the speed data in the delegate method.
CLLocation has a speed property
Rather than try and calculate it yourself, just get it from newLocation.speed.
Related
I'm having a threading issue loading images in a collectionview where the data is coming from cloudkit. I know this is a threading/blocking issue because before I implemented CK, I dumped some images in a folder on my desktop and read/parsed them from there and had no issue. With CK, I just created a handful of records via the dashboard and I'm successfully getting the expected records returned and use the images from those results to populate the CV cells. I store the CK query results in an array and use the size of that array to set the numberOfItemsInSection delegate.
Here's the issue...in the numberOfItemsInSection delegate method, I'm calling the model class, which executes the CK query. Since that is obviously a network call, I put that in a background thread. From logging, I can see the query execute and the results come back very quickly - within 2-3 seconds. However, the CV cells never display and I don't see the custom cell get initialized (via logging). But if I tap the camera button and take a photo, which I've implemented, I take the resulting image and add it to the array, then call reloadData on the CV and all the cells (and images) appear, including the new image just taken with the camera.
By accident, I found out a hack that somewhat works, which is calling reloadData on the CV inside the background thread of the numberOfItemsInSection delegate method. As a result, I thought I might have stumbled on to the solution by switching back to the main thread when calling reloadData, but that put it in a sort of endless loop of continuously calling the numberOfItemsInSection method and cellForItemAtIndexPath and made it to where it lagged to a point that you could barely scroll and tapping on any of the cells wouldn't do anything.
At this point, after trying many, many various things, I'm at a complete loss on how to fix this. I know this is probably a pretty easy solution as it's very common to load images asynchronously to populate a collectionview or tableview. Can someone please provide some guidance? Thanks in advance!!!
#property (nonatomic) NSInteger numberOfItemsInSection;
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
NSLog(#"***numberOfItemsInSection***");
dispatch_queue_t fetchQ = dispatch_queue_create("load image data", NULL);
dispatch_async(fetchQ, ^{
self.numberOfItemsInSection = [self.imageLoadManager.imageDataArray count];
[self.myCollectionView reloadData]; // should be done on main thread!
});
NSLog(#"numberOfItemsInSection: %ld", (long)self.numberOfItemsInSection);
return self.numberOfItemsInSection;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell"; // string value identifier for cell reuse
ImageViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
NSLog(#"cellForItemAtIndexPath: section:%ld row:%ld", (long)indexPath.section, (long)indexPath.row);
cell.layer.borderWidth = 1.0;
cell.layer.borderColor = [UIColor grayColor].CGColor;
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
ImageData *imageData = [self.imageLoadManager imageDataForCell:indexPath.row]; // maps the model to the UI
dispatch_async(dispatch_get_main_queue(), ^{
if (imageData.imageURL.path) {
cell.imageView.image = [UIImage imageWithContentsOfFile:imageData.imageURL.path];
[cell setNeedsLayout];
} else {
// if imageURL is nil, then image is coming in from the camera as opposed to the cloud
cell.imageView.image = imageData.image;
[cell setNeedsLayout];
}
});
return cell;
}
before returning self.numberOfItemsInSection you should wait until the async call is finished. You can do that using semaphores. But then why are you doing this async? You are just getting the count of an array. And then you shouldn't reloadData there. Where do you start your CloudKit query? are you doing that onViewDidLoad? That is also an async operation. When that completes just doe a reloadData of your collectionView. Besides that doing this would be enough:
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [self.imageLoadManager.imageDataArray count];
}
If you really want to use async there, then you do have to wait for the result. You could change your code to something like:
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
NSLog(#"***numberOfItemsInSection***");
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
dispatch_queue_t fetchQ = dispatch_queue_create("load image data", NULL);
dispatch_async(fetchQ, ^{
self.numberOfItemsInSection = [self.imageLoadManager.imageDataArray count];
[self.myCollectionView reloadData]; // should be done on main thread!
dispatch_semaphore_signal(sema);
});
NSLog(#"numberOfItemsInSection: %ld", (long)self.numberOfItemsInSection);
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
return self.numberOfItemsInSection;
}
And then why do you go to the main queue in cellForItemAtIndexPath? It's already executed on the main queue.
working on iPhone osm maps app (Route me).well initialising and downloading online maps was easy but real problem lies in saving the tiles through the code while u are online and reuse them while you are offline.i checked blogs regarding the same but everyone is saving the images externally and importing it in project and then showing them,which is not my requirement.please help me to save the tile image route me picks from online source
here is how i am using online route me maps
-(void) viewDidLoad
{
[RMMapView class];
mapView.contents.tileSource = [[RMOpenStreetMapSource alloc] init];
currentMarker = [[RMMarker alloc]initWithUIImage:[UIImage imageNamed:#"radarLocatorLite.png"] anchorPoint:CGPointMake(0.5, 0.5)];
markerManager = [mapView markerManager];
locationManager.delegate=self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest ;
locationManager.distanceFilter =0;
[mapView.contents setZoom:17.0f];
[markerManager addMarker:currentMarker AtLatLong:currentLocation.coordinate];
[self initCompassView];
[locationManager startUpdatingLocation];
[locationManager startUpdatingHeading];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
currentLocation =newLocation;
[mapView moveToLatLong:newLocation.coordinate];
[markerManager moveMarker:currentMarker AtLatLon: newLocation.coordinate];
[currentRoutePath addLineToLatLong:newLocation.coordinate];
[[mapView.contents overlay] addSublayer:currentRoutePath];
// NSLog(#"i reached inside location update%f",currentRoutePath.lineWidth);
}
I have an iOS app that uses static map images saved in a sqlite database. There are some references as to how to do that, but it took me lots of trial-and-error effort to make sense of them and make it work.
It seems that you should be able to have a sqlite database and save the downloaded images into it as your app downloads them. Then you'd have to know what tile source to use: the sqlite database if the app is offline, the OSM site when online.
The structure of the database is:
tilekey text // a hash that route-me uses to locate the correct tile
zoom integer
row integer
col integer
zoom integer
image blob this stores the actual image of the map
I use a Python script to populate the database, as I want the app to always use the static map images from the database, never to use a real-time download from OSM.
Please let me know if you'd like more information, but if you search for using static maps with route-me, you should find how this is done. Good luck!
finally resolved problem by just a minor change in few places
Step 1: Go to this site "http://shiki.me/blog/offline-maps-in-ios-using-openstreetmap-and-route-me/" and follow instructions to download tile images from online and create of zip of the folder.remember the tile images folder are in order ->zoom level folder->x coord foler->y coord image respectively.
step 2: unzip the zip file in ur app at some folder
step 3:go to the file "RMAbstractMercatorWebSource.m" in map view project
and replace the following folders
-(NSString*) tileFile: (RMTile) tile
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"Tiles"];
NSString *absPath=[NSString stringWithFormat:#"%#/%d/%d/%d.png", path,tile.zoom, tile.x, tile.y];
NSLog(#"file path >>>.............%#",absPath);
return absPath;
}//I unzipped the zip file at tiles folder
-(NSString*) tilePath
{
return nil;
}
-(RMTileImage *)tileImage:(RMTile)tile
{
RMTileImage *image;
tile = [tileProjection normaliseTile:tile];
NSString *file = [self tileFile:tile];
if(file && [[NSFileManager defaultManager] fileExistsAtPath:file])
{
image = [RMTileImage imageForTile:tile fromFile:file];
}
else if(networkOperations)
{
image = [RMTileImage imageForTile:tile withURL:[self tileURL:tile]];
}
else
{
image = [RMTileImage dummyTile:tile];
}
return image;
}
this in turns first look in cache then check the specified directory and finally go for online osm tile images
I currently have a map displaying 10 or so co ordinates.The map gets the users location and centers on it as soon as it is opened. When panning the page or zooming different levels it eventually resets and centers in on the first position of the user.I have tried "stopupdating location" and Animated as "NO".I can not get it to stay in positon when the user scrolls the map.
- (void)viewDidLoad {
[super viewDidLoad];
self.petrolMap.delegate = self;
self.location = [[CLLocationManager alloc] init];
[location setDelegate:self];
[location setDistanceFilter:0]; // Do not apply a distance filter to the map
[location setDesiredAccuracy:kCLLocationAccuracyBest]; // Use the best accuracy possible when displaying the map
petrolMap.delegate=self; // Display on "Petrol Map" , the mapview for the application}
-(void) mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{MKCoordinateRegion mapRegion;
mapRegion.center = petrolMap.userLocation.coordinate;
mapRegion.span.latitudeDelta=0.02;
mapRegion.span.longitudeDelta=0.02;
[petrolMap setRegion:mapRegion animated:NO];}
Your 'location' is a location manager, when it works out where you are it'll send its delegate
locationManager:didUpdateToLocation:fromLocation:
which you don't seem to have, so all those settings you're doing to 'location' are wasted (as far as the code you've given us, it may be useful elsewhere) and telling it to stop tracking the user is of no use.
"(void) mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{MKCoordinateRegion mapRegion;" is what petrolMap is sending to its delegate. Somewhere you must have set petrolMap to track the user, it can be done in the .xib.
Anyway, to stop petrolMap sending messages make sure you run
[petrolMap setUserTrackingMode:MKUserTrackingModeNone animated:NO];
Some extra notes:
Within didUpdateUserLocation you don't need to refer to petrolMap directly because the mapView parameter is set to which ever MKMapView sent the message.
Also within didUpdateUserLocation you are using petrolMap's userLocation instead of the parameter userLocation, and even building your region. The entire code for that function could be one line
[mapView setRegion:mapRegion animated:NO];
'Animated' controls how the change in region is done. Yes means it will slide between locations, No means it will snap from one to the other instantly, either way the map will move to the new region.
Your viewDidLoad method could be cut to two lines like follows
[super viewDidLoad];
self.petrolMap.delegate = self;
Addendum:
locationManager:didUpdateToLocation:fromLocation
is deprecated in iOS6.
Unfortunately this is a few years to late for you James - but hopefully it'll help others who are stuck in this situation (like myself).
I ended up adding...
[self.mapView setUserTrackingMode:MKUserTrackingModeFollow];
Into my -(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
You should normally add "animated:YES" at the end, but this again would ping it back to my current location, even if I changed the commander to "NO" - so tried deleting it and it worked!!!
Just for reference my whole code became:
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
if(userLocationShown) return;
MKCoordinateRegion region;
MapView.showsUserLocation = YES;
region.center.latitude = MapView.userLocation.coordinate.latitude;
region.center.longitude = MapView.userLocation.coordinate.longitude;
region.span = MKCoordinateSpanMake(0.02,0.02);
[MapView setRegion:region animated:YES];
[self.mapView setUserTrackingMode:MKUserTrackingModeFollow];
[locationManager startUpdatingLocation];
[locationManager stopUpdatingLocation];
userLocationShown = YES;
and I added...
- (void)viewDidLoad {
[super viewDidLoad];
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
MapView.delegate = self;
I'm launching a localization request using Grand Central Dispatch :
- (void) findGroceriesNearMe {
dispatch_queue_t downloadQueue = dispatch_queue_create("Groceries downloader", NULL);
dispatch_async(downloadQueue, ^{
CLLocationCoordinate2D userLocation = [LocationManagerController findMeWithCaller:self];
dispatch_async(dispatch_get_main_queue(), ^{
[self userSuccessFullyFound:userLocation];
});
});
dispatch_release(downloadQueue);
}
It calls a static method in my Singleton class LocationManager Controller :
+ (CLLocationCoordinate2D) findMeWithCaller: (UIViewController *) viewController {
LocationManagerController *locationManagerController = [LocationManagerController locationManagerController];
[locationManagerController startUpdates];
while(![locationManagerController getterDone]){
//mystique pour nous-- a approfondir
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
In the startUpdates method, the CLLocationManager, property of LocationManagerController, is initialized and asked to startUpdatingLocation.
Finally, the method when location updates happen :
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
locationDenied = NO;
NSLog(#"%f,%f",newLocation.coordinate.latitude,newLocation.coordinate.longitude);
NSDate* eventDate = newLocation.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
// On vérifie que la newLocation est récente
if (abs(howRecent) > 10.0) {
return;
}
// Test if it's not an invalid measurement
if (newLocation.horizontalAccuracy < 0) return;
// Test the measurement to see if it meets the desired accuracy
if (newLocation.horizontalAccuracy <= manager.desiredAccuracy)
{
latitude = newLocation.coordinate.latitude;
longitude = newLocation.coordinate.longitude;
locationDefined = YES;
[self setterDone:YES];
}
}
My problem is that the locationManager only send 3 location updates and then stops sending updates even though I didn't ask it to stop. So basically, I never get out of the while(![locationManagerController getterDone]) loop.
By the way, before trying to implement this using GCD, it was working fine so I guess the issue has to do with my implementation of multi-threading.
Any idea ?
Edit
I don't get any error in the console. The program just keeps running but I'm stuck in that while loop and nothing else happens after the 3 first location updates.
Thanks !
From CLLocationManager class reference:
Configuration of your location manager object must always occur on a
thread with an active run loop, such as your application’s main
thread.
A guess. If you are sitting at your desk and testing with your simulator the accuracy may not get better what you want
if (newLocation.horizontalAccuracy <= manager.desiredAccuracy)
So you may get stuck in your loop. Try with higher accuracy while at your desk. Also consider if the accuracy is never better that what you want since it maybe that the gps reception is not good.
Let me know if that helps or if I was way off the mark :-)
-- Fasttouch
Not sure if this is the right place (I am sure someone will let me know if it is not) I have a iPhone application that has a UITableview that is backed by core data. I want to perform a reducing search so that only the items starting with the characters entered into the search bar are shown. This is normally done with the delegate - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText no problem. I am a little confused as I am new to Core Data how to do this. One of the big problems as I see it is going to be updating the interface to let it know what to present. I assume an alternative NSFetchedResultsController needs to be sent to the UITableView is that correct?
So here are my issues:
1) I assume I need to create a NSFetchedResultsController with only the correct items in it then tell the UITableView to use this as the dataSource and reload the table?
2) is there a better way than executing a full sorted fetch and removing those objects that do not conform. ie is there a way of doing a select where type fetch?
Thanks in advance and sorry if this is a dumb question.
Regards
Damien
Yes, you will need a new NSFetchedResultsController.
You should use a NSPredicate in your new NSFetchRequest to filter by your search text.
For example, if your managed objects have a field "name" that should be filtered:
NSPredicate *pred = [NSPredicate predicateWithFormat:#"%K beginswith[c] %#", #"name", searchText];
[fetchRequest setPredicate:pred];
I used a slightly different solution: instead of relying on a different NSFetchedResultsController, I created a NSMutableArray (filteredListContent) in my table view controller, used to store the temporary data, as inspired by Apple sample code and Mugunth Kumar's tutorial.
In tableView:cellForRowAtIndexPath:, returning the appropriate data-source array:
if(receivedTableView == self.searchDisplayController.searchResultsTableView){
Objects* object = [self.filteredListContent objectAtIndex:indexPath.row];
cell.textLabel.text = object.name;
} else {
Objects* object = [self.unfilteredListContent objectAtIndex:indexPath.row];
cell.textLabel.text = object.name;
}
As in Apple's sample code, add pretty much the same method in other methods, such as
- (NSInteger)tableView:(UITableView *)receivedTableView numberOfRowsInSection:(NSInteger)section {
if(receivedTableView == self.searchDisplayController.searchResultsTableView){
return [self.filteredListContent count];
}
return [self.unfilteredListContent count];
}
As well as in tableView:didSelectRowAtIndexPath:...
Then conformed to UISearchDisplayDelegate protocol and added the following methods:
- (void)filterContentForSearchText:(NSString*)searchText
{
if (!self.filteredListContent) {
self.filteredListContent = self.filteredListContent = [[NSMutableArray alloc] init];
}
[self.filteredListContent removeAllObjects];
for (Objects *object in [self.coreDataStuffVariable.fetchedResultsController fetchedObjects])
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:
#"(SELF contains[cd] %#)", searchText];
NSString * elementTitle = [NSString stringWithFormat:#"%#", object.name];
[elementTitle compare:searchText options:NSCaseInsensitiveSearch];
if([predicate evaluateWithObject:elementTitle])
{
[self.filteredListContent addObject:password];
}
}
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString{
[self filterContentForSearchText:searchString];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
Pretty simple. I guess it can end up badly if the core data objects are reloaded during a search, but well... if you can sleep knowing that then it may be a good solution!