I have a .xib file where the user can enter a location name of a place in a UITextField and click search. Then another .xib file will load where the user will see a MapView object with the location on the map.
I made a custom init method where I pass the string as follows when the user clicks the button:
AddLocViewController *locView = [[AddLocViewController alloc] initWithNibName:nil bundle:nil customLoc:textLoc.text];
[self presentModalViewController:locView animated:YES];
In the new ViewController class called AddLocViewController.m the init method and viewDidLoad is as following:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil customLoc:(NSString*)_loc
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
stringLoc1 = _loc;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self showAddress];
}
Here [self showAddress] calls a method to display the contents on the mapView object, The showAddress method works perfectly if I have a UITextField taking input and clicking a button to set the value to a NSString object stringLoc and using that in showAddress method:
- (void) showAddress
{
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.01f;
span.longitudeDelta=0.01f;
stringLoc1 = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv",
[addressField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:stringLoc1]];
NSArray *listItems = [locationString componentsSeparatedByString:@","];
double aLatitude = 0.0;
double aLongitude = 0.0;
if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:@"200"]) {
aLatitude = [[listItems objectAtIndex:2] doubleValue];
aLongitude = [[listItems objectAtIndex:3] doubleValue];
}
else {
[addressField setText: @"Address Location error"];
return;
}
[addressField resignFirstResponder];
CLLocationCoordinate2D location = {latitude: aLatitude, longitude:aLongitude};
region.span=span;
region.center.latitude = aLatitude;
region.center.longitude = aLongitude;
if(addAnnotation != nil) {
[mapView removeAnnotation:addAnnotation];
//[addAnnotation release];
addAnnotation = nil;
}
addAnnotation = [[AddressAnnotation alloc] initWithCoordinate:location];
[mapView addAnnotation: addAnnotation];
[mapView setRegion:region animated:TRUE];
[mapView regionThatFits:region];
}
Please help me pass parameter here to this class and use the showAddress method appropriately because it only now shows the proper location when a location is searched from within the class and does not show the proper location when instantiated.
Here you overwrite stringLoc1 with the content of the address field:
It is overwritten regardless of its initial value. If the textField is empty then it is overwirtten with an empty string.
I am not sure what the best solution is in the context of your remaining code. But this should work:
I am sure there are smarter ways of doing that. But this should work with minimal changes to the view lines of code that you shared with us.