[iOS][Background] updating map pins | drawing lines - ios

Hy :)
I am trying to update in background the position of the device (iPhone) and it works with the simulator http://grab.by/meLq (the annotations are only added in Background).
But when I am testing with my iPhone it does not work, I have got a line between the last position (before being in background) and the position when the becomes active.
What about the coding ?
I made a test of making a list when the app is in background and when the application becomes active again I load the list of positions recorded. (work with the simulator)
- (void)handleAddLocations {
NSLog(#"%s | %#", __PRETTY_FUNCTION__, self.locationBackgroundList);
if ([self.locationBackgroundList count] > 0) {
for (CLLocation *backgroundLocation in self.locationBackgroundList) {
if (YES){
LocAnnotation* annotation = [[LocAnnotation alloc] initWithCoordinate:backgroundLocation.coordinate];
NSLog(#"%s %#", __PRETTY_FUNCTION__, [annotation description]);
[self.mapView addAnnotation:annotation];
}
MKUserLocation *userLocation = [[MKUserLocation alloc] init];
[userLocation setCoordinate:backgroundLocation.coordinate];
[self mapView:self.mapView didUpdateUserLocation:userLocation];
}
[self.mapView updateConstraints];
[self.locationBackgroundList removeAllObjects];
self.locationBackgroundList = [[NSMutableArray alloc] init];
}
}
Thanks for helping ;)

Its a better option to add the annotation by creating a list in of locations in background.This is not possible to manage uielements when the app is in background.For this you should add all the positions in array while yous app is in background.

Related

MKMapView Overlay in IOS6

Am adding custom overlays to MKMapView and need to clear the map content before adding overlay (i.e when zoomed or panned default map should be invisible)
Something similar to "canReplaceMapContent" in IOS7 and later.
Is there any method to perform this action in IOS6?
Thanks in advance.,
You method below:
- (void) removeMapOverlay {
[self.mapView removeOverlays:[self.mapView overlays]];
NSMutableArray *tempArray = [NSMutableArray arrayWithArray:[self.mapView annotations]];
if ([tempArray containsObject:[MKUserLocation class]]) {
[tempArray removeObject:[MKUserLocation class]];
}
NSArray *annotationArray = [NSArray arrayWithArray:tempArray];
tempArray = nil;
[self.mapView removeAnnotations:annotationArray];
}
Edit:
When you pinch/zoom or pan in the map. There are two delegate methods are available you can use to check map is loading or not?
- (void)mapViewWillStartLoadingMap:(MKMapView *)mapView {
NSLog(#"loading...");
}
- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView {
NSLog(#"Map loaded...");
}
What i suggest you to use above two methods. Create a bool variable or some means not to load annotations when zoom. How ever i'll keep update if there is other simple way to do it.
You could draw a custom, opaque overlay on top of Apple's maps, but there is still a very slight chance that you will see the map beneath from time to time. I'd recommend an alternative, open source toolset that you can control like Mapbox for complete control.

MKMapView does not move and zoom to search results

I'm implementing search in my MKMapView and I've faced two problems:
When I perform search, location appears in the result and map moves to the found location only after I start to move to the destination. This happens, when the search results are out of the view bounds. If they are inside of the map view bounds or near them it's fine.
It "hops" all the time from one search result to another or to the user's location. I don't expect such behaviour from it.
I've tried several things and I suppose, that the problem is in: didAddAnnotationViews:
[self.locationManager stopUpdatingLocation];
MKAnnotationView *annotationView = [views objectAtIndex:0];
NSLog(#"_Here_ %#", [views description]);
id<MKAnnotation> mp = [annotationView annotation];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([mp coordinate], 250, 250);
[mv setRegion:region animated:YES];
[self.mapView selectAnnotation:mp animated:YES];
Though, I also thought that the problem is in didUpdateToLocation, so I disable updating after the first pin is drop (by search or by tap):
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
double miles = 0.3;
double scalingFactor =
ABS( cos(2 * M_PI * newLocation.coordinate.latitude /360.0) );
MKCoordinateSpan span;
span.latitudeDelta = miles/69.0;
span.longitudeDelta = miles/( scalingFactor*69.0 );
MKCoordinateRegion region;
region.span = span;
region.center = newLocation.coordinate;
[self.mapView setRegion:region animated:YES];
self.mapView.showsUserLocation = YES;
}
Finally, search method:
-(void)searchBarSearchButtonClicked:(UISearchBar *)theSearchBar
{
MKLocalSearchRequest *searchRequest = [[MKLocalSearchRequest alloc] init];
[searchRequest setNaturalLanguageQuery:theSearchBar.text];
searchRequest.region = MKCoordinateRegionMakeWithDistance(self.mapView.userLocation.coordinate, 1000, 1000);
MKLocalSearch *localSearch = [[MKLocalSearch alloc] initWithRequest:searchRequest];
[localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {
if (!error) {
NSMutableArray *annotations = [NSMutableArray array];
[response.mapItems enumerateObjectsUsingBlock:^(MKMapItem *item, NSUInteger idx, BOOL *stop) {
for (id<MKAnnotation>annotation in self.mapView.annotations)
{
if (annotation.coordinate.latitude == item.placemark.coordinate.latitude &&
annotation.coordinate.longitude == item.placemark.coordinate.longitude)
{
return;
}
}
MKPointAnnotation *addAnnotation = [[MKPointAnnotation alloc] init];
addAnnotation.title = [item.placemark.addressDictionary objectForKey:#"Street"];
addAnnotation.coordinate = item.placemark.coordinate;
[annotations addObject:addAnnotation];
}];
for (id<MKAnnotation>annotation in self.mapView.annotations) {
[self.mapView removeAnnotation:annotation];
}
[self.mapView addAnnotations:annotations];
} else {
NSLog(#"Search Request Error: %#", [error localizedDescription]);
}
}];
//Hide the keyboard.
[self.searchBar resignFirstResponder];
}
My aim is to create a MapView, where user can pin the location by tap or via search and, obviously, see the search result.
For the first problem:
When I perform search, location appears in the result and map moves to
the found location only after I start to move to the destination. This
happens, when the search results are out of the view bounds. If they
are inside of the map view bounds or near them it's fine.
This happens because you are moving the map to the annotations found (at least the first one) in the didAddAnnotationViews delegate method.
But that delegate method is only called when an annotation is in the visible area. If an annotation is added to the map but it's not in the visible area (yet), viewForAnnotation won't get called and therefore didAddAnnotationViews won't get called.
Then, when you manually move the map, the annotations that were added start coming into the visible area and then the delegate method gets called and suddenly the map jumps to one of those annotations.
Don't call setRegion inside the didAddAnnotationViews delegate method.
Sometimes, doing so can also cause an endless cycle of viewForAnnotation and didAddAnnotationViews calls because when the region is changed, it causes other annotations to come into view that weren't previously, so viewForAnnotation gets called and then didAddAnnotationViews gets called, and so on.
Instead, set the region right after you call addAnnotations: (or better, just call showAnnotations:) in the searchBarSearchButtonClicked: method.
I would also remove the call to stopUpdatingLocation from didAddAnnotationViews. You probably don't even need the location manager at all if you set the map's showsUserLocation to YES.
For the second problem:
It "hops" all the time from one search result to another or to the
user's location. I don't expect such behaviour from it.
This is also partly due to calling setRegion in didAddAnnotationViews but also because setRegion is called in didUpdateToLocation.
So for the reason described for the first problem, the two delegate methods and the user's manual movements are fighting with each other and the map ends up hopping around.
Don't call setRegion in the didUpdateToLocation method (or, call it once by keeping track in a BOOL whether you've already zoomed to the user location or not).
Not affecting the behavior, but setting showsUserLocation to YES in the didUpdateToLocation doesn't make sense. Why not set this in viewDidLoad or turn it on in the storyboard/xib?
Also, there's no need to calculate the region span manually like that (it's better to let the MapKit do that work for you). Just convert the miles to meters and call MKCoordinateRegionMakeWithDistance.

mapView viewForOverlay never called

So I want to display where my app's user walked on a MKMapView, I collect datas with the following code :
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
// calc. distance walked
CLLocationDistance meters = [newLocation distanceFromLocation:oldLocation];
self.totalMetters += meters;
[[self labelDistance] setText:[self formatDistanceIntoString:self.totalMetters]];
// create another annotation
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
annotation.coordinate = newLocation.coordinate;
// Also add to our map so we can remove old values later
[self.locations addObject:annotation];
// Remove values if the array is too big
while (self.locations.count > 100)
{
annotation = [self.locations objectAtIndex:0];
[self.locations removeObjectAtIndex:0];
// Also remove from the map
[self.map removeAnnotation:annotation];
}
Once it's finished, I call my draw method :
[self drawRoute];
Which contains the following :
- (void)drawRoute {
NSLog(#"drawRoute");
NSInteger pointsCount = self.locations.count;
CLLocationCoordinate2D pointsToUse[pointsCount];
for(int i = 0; i < pointsCount; i++) {
MKPointAnnotation *an = [self.locations objectAtIndex:i];
pointsToUse[i] = CLLocationCoordinate2DMake(an.coordinate.latitude,an.coordinate.latitude);
}
MKPolyline *myPolyline = [MKPolyline polylineWithCoordinates:pointsToUse count:pointsCount];
[self.map addOverlay:myPolyline];
}
Finally my mapView delegate :
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay {
NSLog(#"route");
if ([overlay isKindOfClass:MKPolyline.class]) {
MKPolylineView *lineView = [[MKPolylineView alloc] initWithOverlay:overlay];
lineView.strokeColor = [UIColor greenColor];
return lineView;
}
return nil;
}
Obviously my controller is MKMapView Delegate conform
#interface NewWalkViewController : UIViewController <CLLocationManagerDelegate, MKMapViewDelegate>
And the mapView in the Storyboard is linked to the controller (outlet and delegate)
I use the "bike road" debug tool and there is the output :
2014-01-25 20:27:30.132 The walking dog[2963:70b] new location : 37.330435
2014-01-25 20:27:30.133 The walking dog[2963:70b] drawRoute
As I can see the method for drawing the overlay is never called, and I don't have a single clue how to fix it.
The main problem is that in drawRoute, this line is passing latitude for both parameters to CLLocationCoordinate2DMake:
pointsToUse[i] = CLLocationCoordinate2DMake
(an.coordinate.latitude,an.coordinate.latitude);
This results in the line being drawn in a different part of the world than where the actual an.coordinate is. For example, if an.coordinate is 37,-122 (somewhere near San Francisco), the line is being drawn instead at 37,37 (somewhere in southern Turkey).
Since you are not actually positioning the map at the wrong location (you are looking for the line at the "right" location), viewForOverlay is never called because the map only calls it when it's possible that the overlay will be visible.
Change that line to:
pointsToUse[i] = CLLocationCoordinate2DMake
(an.coordinate.latitude,an.coordinate.longitude);
or simply:
pointsToUse[i] = an.coordinate;
As James Frost mentions in the comments, as of iOS 7, you should be using rendererForOverlay instead of viewForOverlay though the map view will still call viewForOverlay in iOS 7 if rendererForOverlay has not been implemented. Though this isn't preventing your overlay from displaying in the current case, you should implement the new delegate method as well as the old one (if the iOS 7 app will also be running on iOS 6 or earlier).
Another important but unrelated issue is that you are unnecessarily creating multiple, overlapping overlays. In drawRoute, since the overlay you are creating includes all the locations, you should remove any existing overlays before adding the new one. Otherwise, the map ends up with an overlay for location 0 to 1, an overlay for location 0 to location 2, an overlay for location 0 to location 3, etc. The previous overlays are not obviously visible since they have the same color and line width. Before the addOverlay line, put:
[self.map removeOverlays:self.map.overlays];

MKAnnotation not getting selected in iOS5

My app places a pushpin on the map and then selects its using animation so the user has a visual clue and can immediately read the title/subtitle. The following code works in both iOS4 and iOS5, but in iOS5, the annotation doesn't get selected automatically unless I change the animation to NO in the selectAnnotation method.
Any ideas why?
MapAnnotations *pushpin = [[MapAnnotations alloc] initWithCoordinate:coordinate];
pushpin.title = [selectedStation valueForKey:#"name"];
pushpin.subtitle = [selectedStation valueForKey:#"address"];
[stationMap addAnnotation:pushpin];
[stationMap selectAnnotation:pushpin animated:YES];
[pushpin release]; pushpin = nil;
Not sure why it would work before but the animation probably requires the annotation view to be created and ready which is unlikely immediately after adding the annotation.
What you can do is move the selection to the didAddAnnotationViews delegate method which should work on all iOS versions:
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
for (MKAnnotationView *av in views) {
if ([av.annotation isKindOfClass:[MapAnnotations class]]) {
MapAnnotations *pushpin = (MapAnnotations *)av.annotation;
if (_this_pushpin_is_the_one_to_select) {
[mapView selectAnnotation:av.annotation animated:YES];
break; //or return;
}
}
}
}

Draggable annotation with iOS : callout info update

I'm having a problem with the callout info of a draggable annotation : I use an MKReverseGeocoder to get the address of the coordinates corresponding to the annotation position.
But there is something I don't manage to control : every time the draggable annotation is dropped, the callout info shows up. Very often, the MKReverseGeocoder did not have time to update the address info before this happens. So in those usual cases, the annotation callout shows the address info corresponding to the previous coordinates of the annotation.
Here is the code :
1)Delegate method called when annotation is dropped :
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)annotationView didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState{
if (oldState == MKAnnotationViewDragStateDragging) {
MKReverseGeocoder *reverseGeocoder = [[MKReverseGeocoder alloc] initWithCoordinate:annotationView.annotation.coordinate];
reverseGeocoder.delegate = self;
[reverseGeocoder start];
while (reverseGeocoder.querying) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
[reverseGeocoder release];
}
}
2)Delegate method called when reverseGeocoder finds an answer :
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark {
NSString *newSubtitle = [NSString stringWithFormat:#"%# %#, %# %#, %#",placemark.subThoroughfare,placemark.thoroughfare,placemark.postalCode,placemark.locality, placemark.country];
Grocery *draggableGrocery = [myMapView.annotations objectAtIndex:0];
draggableGrocery.address = newSubtitle;
self.addressNewGrocery = newSubtitle;
}
Any idea ?
Thanks !
I was to post the same question, I've been searching for the same issue since yesterday.
UPDATE:
I just figured out a way, and I'm not sure if it is the right one. Knowing that I have a MKAnnotation subclass called selectedAnnotation, I simply deselected, then selected that object after I got the new title in the revereseGeocoder:didFindPlacemark function:
[mapView deselectAnnotation:selectedAnnotation animated:NO];
[mapView selectAnnotation:selectedAnnotation animated:NO];
for your case your code should look like this :
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark {
NSString *newSubtitle = [NSString stringWithFormat:#"%# %#, %# %#, %#",placemark.subThoroughfare,placemark.thoroughfare,placemark.postalCode,placemark.locality, placemark.country];
Grocery *draggableGrocery = [myMapView.annotations objectAtIndex:0];
draggableGrocery.address = newSubtitle;
self.addressNewGrocery = newSubtitle;
// add the following lines [I assumed that draggableGrocery [Grocery class] is a subclass of MKAnnotation]
[mapView deselectAnnotation:draggableGrocery animated:NO];
[mapView selectAnnotation:draggableGrocery animated:NO];
}
Another option is to set annotationView.canShowCallout = NO just before starting the reverse geocoder, and then set the flag back to YES when done.

Resources