cllocation and mkreversegeocoder - iphone

i'm try to retreiving city name with cllocation and mkreversegeocoder.
in my viewdidload method i istance cllocationmanager:
self.locManager = [[CLLocationManager alloc] init];
locManager.delegate = self;
locManager.desiredAccuracy = kCLLocationAccuracyBest;
[locManager startUpdatingLocation];
and after:
- (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation
*)newLocation
fromLocation:(CLLocation *)oldLocation {
//some code to retrieve my information
MKReverseGeocoder *geoCoder = [[MKReverseGeocoder alloc]
initWithCoordinate:newLocation.coordinate ];
geoCoder.delegate = self;
[geoCoder start];
}
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark
*)placemark
{
MKPlacemark *myPlacemark = placemark;
citta.text = [placemark locality];
}
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error{
citta.text =# "Unknow";
Application works only the first time i get my value. on the second time app crash.
I think is because geocoder is started and i think i must have one and only one istance running. (but i'm really not shure of this...). in which way i can controll geocoder is running?
I've see that i can istance mkreversegeocoder in my viewdidload using cllocationcoordinate2d but ... in which way i can retreive newlocation.coordinate?
I've partially resolve declaring geocoder as class variable and checking
if (self.geocoder != nil) {
[geocoder release];
}
but.... if in my
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark
*)placemark
or in my
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailedWithError:(#NSError
*)error
i release or cancel my object?
I'm feeling so stupid :D

Don't create the reverse geocoder as a local variable.
Look at the MKReverseGeocoder example in the CurrentAddress sample app provided by Apple. See MapViewController.h and MapViewController.m.
Follow the same pattern in your code.

Related

Not getting current user location

in .h file
#import <MapKit/MapKit.h>
#interface FirstViewController : UIViewController <MKMapViewDelegate,MKReverseGeocoderDelegate,CLLocationManagerDelegate>
{
}
in .m file
-(void)viewDidLoad
{
CLLocationManager *locationManager = [[[CLLocationManager alloc] init] autorelease];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
[super viewDidLoad];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
MKReverseGeocoder *geoCoder = [[MKReverseGeocoder alloc] initWithCoordinate:newLocation.coordinate];
geoCoder.delegate = self;
[geoCoder start];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"locationManager:%# didFailWithError:%#", manager, error);
}
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{
MKPlacemark * myPlacemark = placemark;
NSString *kABPersonAddressCityKey;
NSString *city = [myPlacemark.addressDictionary objectForKey:(NSString*) kABPersonAddressCityKey];
lblAddress.text = city;
NSLog(#"city detail is:--> %#",city);
}
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error
{
NSLog(#"reverseGeocoder:%# didFailWithError:%#", geocoder, error);
}
This is the code which I have done to get the current location of user & print it in the label.
But the CLLocationManager delegate method (which are written above) is not called & I am not able to get the current address.
Please help me out.
Where I am doing mistake...? guide me.
Thanks.
Instead of autoreleasing the CLLocationManager instance, assign it to an ivar in your class. Then release it in -dealloc as usual (or in one of the delegate methods if you don't need it any longer).
I suspect your location manager is getting deallocated on the next turn of the run loop before having an opportunity to fire off its delegate methods.
In your method viewDidLoad:
-(void)viewDidLoad
{
CLLocationManager *locationManager = [[[CLLocationManager alloc] init] autorelease];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
myMapView.showsUserLocation = YES; // add this line
[super viewDidLoad];
}
And you are done!!!

Tracing the address in MapKit

My location manager is not updating my coordinates of current locations so that i cant able to trace that address.......
I am using the following code,
- (void)viewDidLoad
{
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
//locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.delegate = self;
[locationManager startUpdatingLocation];
// startLocation = nil;
[super viewDidLoad];
}
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(#"Latitude:%f Longitude:%f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
geocoder =[[[MKReverseGeocoder alloc] initWithCoordinate:newLocation.coordinate]autorelease];
geocoder.delegate=self;
[geocoder start];
}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"%#Error",[error description]);
}
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark{
NSString *street=[placemark.addressDictionary objectForKey:#"Street"];
NSLog(#"%#",street);
}
It shows the error report
Error Domain=kCLErrorDomain Code=0 "The operation couldnft be completed. (kCLErrorDomain error 0.)"Error
How to overcome this?.

iPhone SDK MKPlacemark

I need help with the MapKit framework. I am new to iPhone development.
I have a webview that loads from a url with 3 params like this:
in viewDidLoad method from the view controller:
NSString *hoUrlString = [NSString stringWithFormat:#"http://fastenergy.clavis-solutions.de/?iwidth=%#&iheight=%#&zipcode=%#", hString, wString, zipcode];
I can't get the zipcode. I am calling this before the url:
locationController = [[HoCLController alloc] init];
locationController.delegate = self;
[locationController.locationManager startUpdatingLocation];
and in this method from HoCLController.m
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
, I call
MKReverseGeocoder *geocoder = [[MKReverseGeocoder alloc] initWithCoordinate:coord];
[geocoder setDelegate:self];
[geocoder start];
Then I have these 2 methods
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark {
NSLog(#"The geocoder has returned: %#", placemark.postalCode);
}
-(void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error {
NSLog(#"Error: %#", [error description]);
}
The question is , how can I get the zipcode in the url?
Thank you and please help.
You have already the answer in your question.
placemark.postalcode consists of your zipcode.
Just make sure, that all your variables consist the right value.
zipcode = placemark.postalCode;
Should be the answer, if i get your question right.
MKReverseGeocoder *geocoder = [[MKReverseGeocoder alloc] initWithCoordinate:coord];
You have to pass coordinate in this method. So check if coord is not nil.I guess this is what you want.
MKReverseGeocoder *geocoder = [[MKReverseGeocoder alloc] initWithCoordinate:newLocation.coordinate];
Oh Ok, forget it..
I miss something, sorry
Are you subclassing CLLocationManager ???
If not, why are you allocating like this: locationManager = [[HoCLController alloc] init]; instead of this:self.locationManager = [[CLLocationManager alloc] init]; I'm sorry if I'm getting something wrong, but think i'm giving you the answer..

How to retrieve current city name using wifi/3g ip address with iPhone/iOS programming (Objective-c)?

I want to retrieve the current "city name" of the iPhone using ip address of device (wifi/3g). Is there any way to do it? please help me out.
Following is the place where I want the current location.
-(BOOL)fetchAndParseRss{
NSString * CurrentLocation = <I want the retrieved Location here!!>;
NSString * URLrss = [NSString stringWithFormat:#"http://news.google.com/news?q=location:%#&output=rss", CurrentLocation];
NSURL *url = [NSURL URLWithString:URLrss];
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
}
Actually i am developing an app for myself that displays the news of current city.
You can use CLLocationManager and MKReverseGeocoder to get users city.
First add CLLocationManager and start updating user location
- (void)startLocationManager
{
if (locManager==nil)
locManager = [[CLLocationManager alloc] init];
if (locManager.locationServicesEnabled)
{
NSLog(#"User has opted out of service on it error");
} else {
locManager.delegate = self;
locManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
[locManager startUpdatingLocation];
}
}
Then add location manager delegate methods to use MKReverseGeocoder.
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(#"Manager error: %#", [error description]);
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
self.geocoder = [[MKReverseGeocoder alloc] initWithCoordinate:newLocation.coordinate];
[geocoder release];
geocoder.delegate = self;
[geocoder start];
[manager stopUpdatingLocation];
}
and of course you have to implement MKReverseGeocoder Delegate methods
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error
{
NSLog(#"reverseGeocoder:%# didFailWithError:%#", geocoder, error);
}
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{
// with the placemark you can now retrieve the city name
NSString *city = [placemark.addressDictionary objectForKey:#"City"];
NSLog(#"City is %#", city);
}
This method can be use if user gives permission to your application for location manager.
One option would be to make an HTTP request to a website like geoiptool.com and scrape the city information from the response. I'm unsure how much success you'll have when using mobile phone networks, though—wifi worked when I tried it on my phone, but 3G didn't.

Method not getting called (Core Location) Cocoa Touch

Hey guys,
I'm trying to call the update method but its not working, any idea?
#import "TrackerViewController.h"
#implementation TrackerViewController
-(IBAction)updateButton:(id)sender{
[self update];
}
-(void)update{
locmanager = [[CLLocationManager alloc] init];
[locmanager setDelegate:self];
[locmanager setDesiredAccuracy:kCLLocationAccuracyBest];
[locmanager startUpdatingLocation];
}
-(void)viewDidLoad {
[self update];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
CLLocationCoordinate2D loc = [newLocation coordinate];
latitude.text = [NSString stringWithFormat: #"%f", loc.latitude];
longtitude.text = [NSString stringWithFormat: #"%f", loc.longitude];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
}
The didUpdateToLocation will be called when an location changes.
I guess you are testing it in simulator and on simulator no change occurs in location so the method don't called.
Suggesstion You should implement other delegate methods to check whats happening; in case if there is some error. For example: locationManager:didFailWithError: