Draggable annotation with iOS : callout info update - ios

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.

Related

Wrong presentation of pins in MapKit (color and annotation) [duplicate]

I am trying to make a map, where I can see my current location, and see what the street is called.
so far, I am able to put a pin on my map, but for some reason, I am not getting the callout.
and I have put a NSLog in my viewForAnnotation method, but it is not being called, so i wasn't able to test it.
can someone help me?
-(void)lat:(float)lat lon:(float)lon
{
CLLocationCoordinate2D location;
location.latitude = lat;
location.longitude = lon;
NSLog(#"Latitude: %f, Longitude: %f",location.latitude, location.longitude);
//One location is obtained.. just zoom to that location
MKCoordinateRegion region;
region.center=location;
//Set Zoom level using Span
MKCoordinateSpan span;
span.latitudeDelta=.005f;
span.longitudeDelta=.005f;
region.span=span;
[map setRegion:region animated:TRUE];
//MKReverseGeocoder *geocoder=[[MKReverseGeocoder alloc] initWithCoordinate:location];
//geocoder.delegate=self;
//[geocoder start];
if (cPlacemark != nil) {
[map removeAnnotation:cPlacemark];
}
cPlacemark=[[CustomPlacemark alloc] initWithCoordinate:location];
cPlacemark.title = mPlacemark.thoroughfare;
cPlacemark.subtitle = mPlacemark.locality;
[map addAnnotation:cPlacemark];
[cPlacemark release];
[mLocationManager stopUpdatingLocation];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
// try to dequeue an existing pin view first
if ([annotation isKindOfClass:[CustomPlacemark class]]){
MKPinAnnotationView *pinView=(MKPinAnnotationView *)[map dequeueReusableAnnotationViewWithIdentifier:#"customIdentifier"];
if (!pinView)
{
// if an existing pin view was not available, create one
MKPinAnnotationView* cPinAnnoView = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:#"customIdentifier"] autorelease];
cPinAnnoView.pinColor = MKPinAnnotationColorPurple;
cPinAnnoView.animatesDrop = YES;
cPinAnnoView.canShowCallout = YES;
// Add button
UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[leftButton addTarget:self action:#selector(annotationViewClick:) forControlEvents:UIControlEventTouchUpInside];
cPinAnnoView.leftCalloutAccessoryView = leftButton;
} else
{
pinView.annotation = annotation;
}
return pinView;
}
return nil;
}
Right now I have customized my viewForAnnotation to be like this.
But I still can't get a callout from my pin and the pin remains red.
But it should be purple of nothing at all
I had the same problem which was not setting the MapView delegate to the File Owner.
Open your nib
Right click on the MapView
Drag the delegate to the File's Owner
I had the same problem, as you mentioned. The delegate had been set to ViewController, but the viewForAnnotation selector was not being called. After some checks, I realized if you do not call addAnotation in the main thread, mapView would not call viewForAnnotation, so following update resolved my problem:
Before:
[_mMapView addAnnotation:marker];
After:
dispatch_async(dispatch_get_main_queue(), ^{
[_mMapView addAnnotation:marker];
});
In order to get the viewForAnnotation to be called, add mapView.delegate=self; to e.g. the viewDidLoad method:
- (void)viewDidLoad {
[super viewDidLoad];
mapView.delegate=self;
}
Could it be that your annotation has been added outside the current view area of the MKMapView?
For storyboard, Ctl drag the MKMapView to the orange circle on the bottom bar of ViewController, and select delegate.
This will solve the problem.
As vatrif mentioned in the comments, you must set your delegate BEFORE adding annotations to your MKMapView object.
Others have already explained, odds are high you have not connected your mapview delegate to your controller. Its the first thing to check
i have been working in ios 9 Mapview related app and I experienced the same problem.
somehow I solved my problem, in my case im resizing the mapview.
I added delegate after i resize the mapview. it works now perfectly.!
After having set the delegate for the mapview if still the viewforannotation not getting called then this is something which you have missed - set the self.mapView.showsUserLocation to YES, in interface builder you can tick the shows userLocation option in attributes inspector.

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.

viewForAnnotation Is Not Getting Called

Having scanned all the similar problems and tried all possible solutions, I still can't find the solution to my case. I tried to put multiple pins on my MapView but it seems that my pin can't be added normally. I started a NSTimer in my viewDidLoad method so that every 5 seconds, some updated pins will be put on Map. I added debugging information and found the problem was the that the method viewForAnnotation is not getting called.
(I've already set the delegate by calling [_map setDelegate:self])
My code is as following:
- (void)viewDidLoad
{
[super viewDidLoad];
_map = [[MKMapView alloc] initWithFrame:[[self view] frame]];
[_map setDelegate:self];
CLLocationCoordinate2D startLocation;
startLocation.latitude = [startLat floatValue];
startLocation.longitude = [startLong floatValue];
MKCoordinateSpan span = MKCoordinateSpanMake(0.002, 0.002);
MKCoordinateRegion region = MKCoordinateRegionMake(startLocation, span);
[_map setRegion:region];
[[self view] addSubview:_map];
[self getPlacesForLocation:[_map centerCoordinate]];
NSTimer *currentTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:#selector(theActionMethod:) userInfo:nil repeats:YES];
[currentTimer fire];
}
The getPlacesForLocationMethod:
-(void)getPlacesForLocation:(CLLocationCoordinate2D)location
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^
{
/* get data from the website
** get the geo information and put them in the MapPin struct
*/
[self putPinsOnMap];
}
}
putPinsOnMap:
-(void)putPinsOnMap
{
for(Pinfo *iter in [_PinfoArray copy])
{
MapPin *pin = [[MapPin alloc] init];
[pin setTitle:[iter from_user_name]];
[pin setSubtitle:[iter text]];
[pin setCoordinate:[iter location]];
//NSLog(#"The coordinate is %f %f", [pin coordinate].latitude, [pin coordinate].longitude);
[_map addAnnotation:pin];
/****************************************/
NSLog(#"Try to put the pin on Map");
/****************************************/
}
}
And here is the content of my MapPin.h:
#interface MapPin : NSObject<MKAnnotation>
#property (nonatomic, copy) NSString *title, *subtitle;
#property (nonatomic) CLLocationCoordinate2D coordinate;
#end
If I stay in place, then every time the (void)putPinsOnMap gets called, it prints "Try to put pin on Map" but the viewForAnnotation method is not getting called (I also added debugging info there but none of them are printed). Only a few times, the method viewForAnnotation sometimes will be called if I zoom out to a great extent.
You say you added debugging info to viewForAnnotation, but you haven't subclassed MKMapView to override the viewForAnnotation method (or at least, you are allocating an MKMapView, not any subclass). I think you are looking for the delegate method:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation;
and if you implement that method in your view controller (which you are making the delegate of the MKMapView) you might get useful information.
If mapView:viewForAnnotation: is still not called except when you zoom way out then you have probably placed your annotations incorrectly and they're just way out on some other part of the globe than the one you have initially on screen.

How to remove just added, one annotation?

I have a lot of annotations on the mapView and user location dot. Then, if user tap for 2 sec. on the map, I add an extra annotation with options. I need to remove that last added annotation from map by pressing the button. How can I remove it without to remove any other annotation?
- (void)addPin:(UILongPressGestureRecognizer*)recognizer {
if(UIGestureRecognizerStateBegan == recognizer.state) {
CGPoint tappedPoint = [recognizer locationInView:mapView];
CLLocationCoordinate2D locCoord= [mapView convertPoint:tappedPoint toCoordinateFromView:mapView];
MKPointAnnotation *annot = [[MKPointAnnotation alloc] init];
annot.coordinate = locCoord;
[self.mapView addAnnotation:annot];
}
if(UIGestureRecognizerStateChanged == recognizer.state) {
// Do repeated work here (repeats continuously) while finger is down
}
if(UIGestureRecognizerStateEnded == recognizer.state) {
// Do end work here when finger is lifted
}
}
To remove all the annotations from map view:
[vwMap removeAnnotations:vwMap.annotations];
PS: vwMap is the MKMap view object
Do the following,
If you have the annotation object
[self.mapView removeAnnotation:annot];
If you have the index of the object
[self.mapView removeAnnotation:self.mapView.annotations.lastObject];
Do this to remove your last added annotation in your delete Action:
[self.mapView removeAnnotation:[self.mapView.annotations lastObject]];
Hope helpful
I managed to remove the annotation object that is touched by doing the following, I know this wasn't the question but it may help someone out
set the mapView as delegate
- (void)mapView:(MKMapView *)thisMapView didSelectAnnotationView:(MKAnnotationView *)view {
MKPointAnnotation *thisTouchedAnnotation = view.annotation;
uint8_t annotationCount = thisMapView.annotations.count;
for(int i =0; i<annotationCount; i++)
{
if ([thisMapView.annotations objectAtIndex:i]==thisTouchedAnnotation){
[thisMapView removeAnnotation:[mapView.annotations objectAtIndex:i]];
break;
}
}
}
not flawless code but it may guide you :-)
Use this code!
NSArray *array=self.mapview.annotations;
for (MKPointAnnotation *anno in array)
{
if(anno==[array lastObject])
{
[self.mapview removeAnnotation:anno];
}
}

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;
}
}
}
}

Resources