So I am pretty new to object oriented programing, and I am trying to do the following in xcode – I have a MyMap.m view controller where I display your location, and now want to add weather. For this purpose I would like to send the coordinate to a geocoder class that will return first the zipcode. I decided to heat it up with the classes, so created a class which will return the zipcode.
the code in the map controller is:
…..
MKPlacemark *zip;
getzip = [[GetZipcode alloc] init]; //ADDED THIS
[getzip reverseGeocodeCurrentLocation];
zip = getzip.myPlacemark;
NSLog(@"Placemark is: %@", zip);
….
where getzip is an instance of a GetZipcode class:
@interface MyMap : UIViewController <MKMapViewDelegate, MyLocationControllerDelegate> {
GetZipcode *getzip;
//and more stuff
}
From some reason, nothing happened when I run this:
[getzip reverseGeocodeCurrentLocation];
which reverseGeocodeCurrentLocation is a function inside the GetZipcode class:
.h
- (void)reverseGeocodeCurrentLocation;
and the .m of that class is:
- (void)reverseGeocodeCurrentLocation
{
self.reverseGeocoder = [[[MKReverseGeocoder alloc] init] autorelease];
reverseGeocoder.delegate = self;
[reverseGeocoder start];
}
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{
PlacemarkViewController *placemarkViewController = [[PlacemarkViewController alloc] initWithNibName:@"PlacemarkViewController" bundle:nil];
placemarkViewController.placemark = placemark;
myPlacemark = placemark;
NSLog(@"Placemark is: %@", placemark.postalCode);
}
Hope it’s not TMI, but would really appreciate some help here. thanks!
You didn’t give the MKReverseGeocoder a coordinate to reverse geocode.
The designated initializer is -[MKReverseGeocoder initWithCoordinate:] through which you supply the coordinate.
Hope that helps
(also of course check to see that you created and init’d your GetZipcode instance and that getzip isn’t nil when you try to use it.)
EDIT:
You created an instance variable (ivar) called getzip. But you didn’t post any code that creates an instance of GetZipcode and puts that address in the pointer getzip. Somewhere you should have something like:
SECOND EDIT
So a couple more things to think about here. (1) You can’t actually read the placemark property immediately as you trying to do here:
It will take some time for a reverse geocoder to go out to the network and do it’s work and figure out the placemark. So you won’t have a placemark until you get the delegate callback:
(2) You should also implement this callback so you can see if there was a problem finding the placemark:
(3) Are you sure you are passing a valid coordinate to the reverse geocoder? You may want to log that to be absolutely sure.