didAddAnnotationViews not getting called always - iphone

I added a MKAnnotation delegate handler class into a MKMapView like this.
MapAnnotation *anAnnotation = [[[MapAnnotation alloc] initWithCoordinate:coord] autorelease];
[myMapView addAnnotation:anAnnotation];
The MapAnnotation implements the MKAnnotation.
However didAddAnnotationViews doesnt get called always. Sometimes (rarely) it does and sometimes its not. I checked few places and I have used this correctly. Does it depends on span attributes as well?
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.005f;
span.longitudeDelta=0.005f;
CLLocationCoordinate2D location;
location.latitude = searchLocation.coordinate.latitude;
location.longitude = searchLocation.coordinate.longitude;
region.span=span;
region.center=location;
[myMapView setRegion:region animated:TRUE];
[myMapView regionThatFits:region];
Whats wrong with this code?

This delegate method is called for annotations that are currently visible i.e. those that are within the map region that is currently displayed on the screen.

Related

mapView "addAnnotation:addAnnotation" NOT dropping pin as desired with IF/ELSE statements

Struggling bad here on something I am now exhausted with. There are two parts here:
1) I am trying to get a pin removed if the user searches the same coordinate that is already identified and pinned on ViewDidLoad. This part WORKS. The non operable part is #2.
2) When the user searches ANY OTHER location, I want a pin added, i.e. [self.mapView addAnnotation:addAnnotation]; My code is below.
- (IBAction) showAddress // THIS IS A BUTTON WHICH LOCATES COORDINATES
{
[addressField resignFirstResponder];
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.2;
span.longitudeDelta=0.2;
CLLocationCoordinate2D location = [self addressLocation];
region.span=span;
region.center=location;
[mapView setRegion:region animated:TRUE];
[mapView regionThatFits:region];
if (location.longitude = (double) -73.2125)
{
addAnnotation = [[AddressAnnotation alloc]initWithCoordinate:location];
[self.mapView removeAnnotation:addAnnotation];
}
else
{
addAnnotation = [[AddressAnnotation alloc]initWithCoordinate:location];
[self.mapView addAnnotation:addAnnotation];
[addAnnotation release];
}
}
Any thoughts? Thanks guys!
This line:
if (location.longitude = (double) -73.2125)
does an assignment (=), not a comparison (==).
Since the assignment is always successful, it always goes to the removeAnnotation part.
However, I don't recommend comparing doubles (or any floating-point numbers) using ==.
I suggest checking instead if the two numbers are within some small distance of each other.
See this answer for an example.

how to get user current location in simulator in iphone sdk?

I am getting user location from this method
/*Region and Zoom*/
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.2;
span.longitudeDelta=0.2;
CLLocation *location1 = [locationManager location];
CLLocationCoordinate2D location = [location1 coordinate];
location.latitude = location.latitude;
location.longitude = location.longitude;
region.span=span;
region.center = location;
/*Geocoder Stuff*/
MKReverseGeocoder *geoCoder=[[MKReverseGeocoder alloc] initWithCoordinate:location];
geoCoder.delegate = self;
[geoCoder start];
mapView.showsUserLocation = TRUE;
[mapView setRegion:region animated:TRUE];
[mapView regionThatFits:region];
This works perfectly but when I try to run this code in simulator then it gets crashed at the point of [mapView setRegion:region animated:TRUE]; this tells it doesn't get the latitude and longitude values. So how can I make this code run in simulator perfectly.
I have to make this code run in simulator, is it possible?
On iOS5 Simulator by default you it will show some location in USA. If you want to change that location, in iOS Simulator menu, go to Debug -> Location -> Custom Location.
There you can set the latitude and longitude and test the app accordingly. This works with mapkit and also with CLLocationManager.
try this path-
Go to product->Edit Scheme->Options->select Allow Location simulation
it won't be your exact location! But at least you can verify your code is working.

Why didn't my map annotation move?

I am implementing a mapView whereby an annotation will be placed when the user search for an address. But somehow, the annotation sometime doesn't move and update to the new coordinate. It's only upon zooming the map, then will it update to the new location. The subtitle did get updated though.
- (void)searchBarSearchButtonClicked:(UISearchBar *)theSearchBar {
SVGeocoder *geocodeRequest = [[SVGeocoder alloc] initWithAddress:searchBar.text inRegion:#"sg"];
[geocodeRequest setDelegate:self];
[geocodeRequest startAsynchronous];
}
- (void)geocoder:(SVGeocoder *)geocoder didFindPlacemark:(SVPlacemark *)placemark {
if (annotation) {
[annotation moveAnnotation:placemark.coordinate];
annotation.subtitle = [NSString
stringWithFormat:#"%#", placemark.formattedAddress];
}
else {
annotation = [[MyAnnotation alloc]
initWithCoordinate:placemark.coordinate
title:#"Tap arrow to use address"
subtitle:[NSString
stringWithFormat:#"%#", placemark.formattedAddress]];
[mapView addAnnotation:annotation];
}
MKCoordinateSpan span;
span.latitudeDelta = .001;
span.longitudeDelta = .001;
MKCoordinateRegion region;
region.center = placemark.coordinate;
region.span = span;
[mapView setRegion:region animated:TRUE];
[searchBar resignFirstResponder];
}
I don't think MKMapView gets notified of changes to an annotation's location. The documentation of MKAnnotation's setCoordinate: says: "Annotations that support dragging should implement this method to update the position of the annotation." so it seems that's the only purpose of this method is for supporting dragging of pins.
Try removing the annotation from the map view before changing its coordinates and then add it back to the map view.
Nothing in your code (that you've shown) tells mapView that the annotation's location has changed. The annotation itself probably can't do it in -moveAnnotation because annotations generally don't know what map or maps they've been added to (nor should they).
The right way to move an annotation is to to remove it from the MKMapView that's using it, update its location, and then add it back to the map. You can't just change the annotation's location after it has been added to the map because the map may very well cache the location or sort the annotation according to its location, and there's no method in MKMapView to tell the map that the location has changed.
I'd change your conditional to something like this:
if (annotation == nil) {
annotation = [[MyAnnotation alloc] init];
annotation.title = #"Tap arrow to use address";
}
[mapView removeAnnotation:annotation];
[annotation moveAnnotation:placemark.coordinate];
annotation.subtitle = placemark.formattedAddress;
[mapView addAnnotation:annotation];
This assumes that it's safe to call -init in place of -initWithCoordinate:title:subtitle:; if not, you'll want to change that.

MKMapView Annotation setCoordinate crashing app

I am working on a simple GPS-like app. I created a custom object which extends MKAnnotation which also works fine. I can place it etc, but if I put this piece of code [mpm setCoordinate:loc]; in my code, the app opens and freezes while showing the basic gray-grid (not showing any downloaded map and non of my buttons work either then)
Here is my header:
#interface FirstViewController : UIViewController <MKMapViewDelegate>
{
IBOutlet MKMapView *mapView;
MyPlaceMark *mpm;
}
-(void)locationUpdate:(CLLocation *)location;
-(void)locationError:(NSError *)error;
#end
And here is the piece of code where I try to update things:
-(void)locationUpdate:(CLLocation *)location
{
CLLocationCoordinate2D loc = [location coordinate];
[mpm setCoordinate:loc]; // This line messes it up.
[mapView setCenterCoordinate:loc];
if([mapView showsUserLocation] == NO)
[mapView setShowsUserLocation:YES]; // This does not show my position either?
}
If I comment that line out, the app works fine. I need to update the annotation as it will be my marker for showing the users current location. PS: Without that line, it does center my view - so location is a valid/set variable.
My viewDidLoad looks like this:
- (void)viewDidLoad {
mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
//mapView.showsUserLocation = YES;
[mapView setShowsUserLocation:YES];
mapView.mapType = MKMapTypeStandard;
mapView.delegate = self;
CLLocationCoordinate2D location;
MKCoordinateRegion region;
location.latitude = -33.8;
location.longitude = 18.6;
MKCoordinateSpan span;
span.latitudeDelta = 0.01;
span.longitudeDelta = 0.01;
region.span = span;
region.center = location;
[mapView setRegion:region animated:TRUE];
[mapView regionThatFits:region];
mpm = [[MyPlaceMark alloc] initWithCoordinate:location]; // Creating mpm
[mapView addAnnotation:mpm]; // Adding mpm
[self.view addSubview:mapView];
[super viewDidLoad];
}
Should I re-add mpm after I changed it's location? Or would it just jump to its new location on the map?
So to recap the question: How can I update the custom MKAnnotation's location on my mapview?
Thank you for your time!
EDIT: I think the main reason for the crash is the fact that I do not create a setCoordinate method/function in my custom MKAnnotation. How would I override that but keep it same as original?
The error in log-console:
2011-06-09 14:42:40.377 AeroNav[3706:707] -[MyPlaceMark setCoordinate:]: unrecognized selector sent to instance 0x19e0e0
2011-06-09 14:42:40.493 AeroNav[3706:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MyPlaceMark setCoordinate:]: unrecognized selector sent to instance 0x19e0e0'
...
terminate called after throwing an instance of 'NSException'
[mapView setShowsUserLocation:YES] doesn't update the region that the map shows. You will have to implement the delegate methods didUpdateUserLocation: and call the setRegion:animated: there to move the region to user's location. Until this is called for the first time, the userLocation property of MKMapView is not set.
As for the MKAnnotation thing, MKAnnotation is a protocol. It doesn't have a default implementation. You will have to provide for all the methods and properties that you've agreed to conform to. So declare a property pretty similar to the one in the MKAnnotation protocol.

Placing blue dot in mapKit with a desired location

For demonstration purpose, i need to simulate the user location in a Mapkit view.
It seems that it is possible with an undocumented API to place the blue dot anywhere on the map view.
Unfortunately, i don't know witch undocumented API to use ?
Any help ?
Did you set this?
mapView.showsUserLocation = YES;
Setting a specific location is a bit more difficult, but certainly do-able without undocumented APIs. See code below:
- (void)animateToSelectedPlace:(CGFloat)zoomLevel {
MKCoordinateRegion region;
region.center = [self getCoordinateFromComponents:chosenLatitude:chosenLongitude];
MKCoordinateSpan span = {zoomLevel,zoomLevel};
region.span = span;
[mapView setRegion:region animated:YES];
}
-(CLLocationCoordinate2D)getCoordinateFromComponents:(NSNumber*)latitude:(NSNumber*)longitude {
CLLocationCoordinate2D coord;
coord.latitude = latitude.doubleValue;
coord.longitude = longitude.doubleValue;
return coord;
}
Not sure that it is possible to set CUSTOM user location (usually people use image simulating blue user point). Though i am not 100% sure, so you have a chance to try something like this to check if it is possible to deal with userLocation as with MKAnnotation ...
CLLocationCoordinate2D c = self.mapView.userLocation.location.coordinate;
[[self.mapView userLocation] setCoordinate:c];