views:

708

answers:

0

Hi everybody, I'm building right now an iPhone app that basically is organized as follows:

We have a Navigation Controller with a Table View all inside of a TabBar (a classic in iPhone apps) then in one of my tabs I fill the table view with data extracted from a DB in sqlite, so this far I have no problems, I display the drill down correctly. The problem I'm having is that and the bottom of the hierarchy the user is viewing details of an object of the DB and the the user can select the address(street,number,zip) of this object and then the controller push a new one that uses a Map but the problem I have is that the first time the app launches the user can navigate correctly and the map display the address of the object correctly(with right geocoding and even annotations with personalized title,subtitle and image) but if we return and select some other categories on the drill down when we select the address and this push again the mapview, the map doesn't update the location, it shows the previous selection/address. Can someone help me please?? This is driving me nuts!!!

Here is some of the code:

My last table view with the selection-pushing method:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


 [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:NO];

 if(viewController == nil)
 {
  viewController = [[MapperViewController alloc] initWithNibName:@"MapViewController" bundle:[NSBundle mainBundle]];
 }

 switch (indexPath.section) {
  case 4:
   [self.navigationController pushViewController:viewController animated:YES];
   break;
  default:
   break;
 }
}

This is the Map controller interface:

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@class MyAnnotation;
@class MyAppDelegate,MyDataObject;

@interface MapperViewController : UIViewController <MKMapViewDelegate>
{
 IBOutlet MKMapView *mapView;
 MKPinAnnotationView *pinView;
 MyAnnotation *ann;
 MyAppDelegate *appDelegate;
 MyDataObject *data;

}

@property (nonatomic, retain) IBOutlet MKMapView *mapView;
@property (nonatomic, retain) MKPinAnnotationView *pinView;
@property (nonatomic, retain) MyAnnotation *ann;
@property (nonatomic, retain) MyDataObject *data;

@end

This is the Map controller implementation:

#import "MapperViewController.h"
#import "MyAnnotation.h"
#import "MedicaSurAppDelegate.h"

@implementation MapperViewController

@synthesize mapView;
@synthesize data;
@synthesize pinView;
@synthesize ann;

- (void)dealloc 
{
 [mapView release];
 mapView.delegate = nil;

    [super dealloc];
}


- (void)recenterMap {
 MKCoordinateRegion region = {{0.0f, 0.0f}, {0.0f, 0.0f}};
 region.center.longitude = 0.0;
 region.center.latitude = 0.0;
 region.span.longitudeDelta = 0.01f;
 region.span.latitudeDelta = 0.01f; 
 [self.mapView setRegion:region animated:YES];
}

- (void)viewDidDisappear:(BOOL)animated {
 //[super viewDidAppear:animated];


 if(nil != self.ann) {
  [self.mapView addAnnotation:self.ann];
  self.ann= nil;
 }
 if(self.mapView.annotations.count > 1) {
  [self recenterMap];
 }
}
- (void)viewDidLoad 
{


    [super viewDidLoad];

 appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];


 UIImage *image = [UIImage imageNamed: @"topbar.conLogo.color.png"];
 UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
 UISegmentedControl *buttonBarSegmentedControl;   

 buttonBarSegmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Standard", @"Satellite", @"Hybrid", nil]];  
 [buttonBarSegmentedControl setFrame:CGRectMake(30, 10, 280-30, 30)];  
 buttonBarSegmentedControl.selectedSegmentIndex = 0.0;   // start by showing the normal picker  
 [buttonBarSegmentedControl addTarget:self action:@selector(toggleToolBarChange:) forControlEvents:UIControlEventValueChanged];  
 buttonBarSegmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;  
 buttonBarSegmentedControl.backgroundColor = [UIColor clearColor];  
 [buttonBarSegmentedControl setAlpha:0.8];  

 [self.view addSubview:buttonBarSegmentedControl];  

 self.navigationItem.titleView = imageView;

 [imageView release];





 NSString *fullAddress = [NSString stringWithFormat:@"%@,%@,%@,%@,%@ ",appDelegate.street,appDelegate.zone,appDelegate.zip,appDelegate.city,appDelegate.state];

 NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&amp;output=csv", [fullAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
 NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];
 NSArray *listItems = [locationString componentsSeparatedByString:@","];

 double latitude = 0.0;
 double longitude = 0.0;

 if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:@"200"]) {
  latitude = [[listItems objectAtIndex:2] doubleValue];
  longitude = [[listItems objectAtIndex:3] doubleValue];
 }
 else {
  //Show error
 }


 [mapView setMapType:MKMapTypeStandard];
 [mapView setZoomEnabled:YES];
 [mapView setScrollEnabled:YES];


 MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
 region.center.latitude = latitude;
 NSLog(@"Lat%f ",latitude);
 region.center.longitude = longitude;
 NSLog(@"Long %f ",longitude);
 region.span.longitudeDelta = 0.01f;
 region.span.latitudeDelta = 0.01f; 
 [mapView setRegion:region animated:YES];

 [mapView setDelegate:self];

 ann = [[MyAnnotation alloc] init];
 ann.title = [NSString stringWithFormat:@"Object named: %@",appDelegate.name];
 ann.subtitle = appDelegate.street;
 ann.coordinate = region.center;
 [mapView addAnnotation:ann];
 [ann release];

}


- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
 pinView = nil;
 if(annotation != mapView.userLocation) 
 {
  static NSString *defaultPinID = @"pinView";
  pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
  if ( pinView == nil )
   pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];

  pinView.pinColor = MKPinAnnotationColorPurple;
  pinView.canShowCallout = YES;
  pinView.animatesDrop = YES;
  [pinView setSelected:YES animated:YES];
  UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"someimage.png"]];
  pinView.leftCalloutAccessoryView = imgView;
  [self performSelector:@selector(openCallout:) withObject:annotation afterDelay:0.5];


 }
 else
 {
  [mapView.userLocation setTitle:@"I am here"];
 }

    return pinView;
}


- (void)openCallout:(id<MKAnnotation>)annotation {
     [mapView selectAnnotation:annotation animated:YES];
}


- (void)toggleToolBarChange:(id)sender
{
 UISegmentedControl *segControl = sender;

 switch (segControl.selectedSegmentIndex)
 {
  case 0: // Map
  {
   [mapView setMapType:MKMapTypeStandard];
   break;
  }
  case 1: // Satellite
  {
   [mapView setMapType:MKMapTypeSatellite];
   break;
  }
  case 2: // Hybrid
  {
   [mapView setMapType:MKMapTypeHybrid];
   break;
  }
 }
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}



- (void)didReceiveMemoryWarning 
{
    [super didReceiveMemoryWarning];
}


- (void)viewDidUnload 
{

}



@end

Any help would be great, and feel free to copy code or do some corrections :D