0
votes

Following this tutorial on how to display XML latitude and longitude data as pins on iOS map kit: http://highoncoding.com/Articles/805_Consuming_XML_Feed_and_Displaying_Public_Information_on_the_MapView_Control.aspx

The sample code provided compiles correctly and displays pins all across the united states. However when I tried to "port" the .xib into my app It pulls up my Mapview and the current users location, but it won't drop any pins/parse the data?
I'm on Day 2 now, its a bit discouraging. Heres my .m and .h

      EleventhViewController.h
//  SlideMenu
//
//  Created by Kyle Begeman on 1/13/13.
//  Copyright (c) 2013 Indee Box LLC. All rights reserved.
//


#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import "ECSlidingViewController.h"
#import "MenuViewController.h"

//@interface EleventhViewController : NSObject <UIApplicationDelegate,MKMapViewDelegate,CLLocationManagerDelegate> {
  @interface EleventhViewController : UIViewController <MKMapViewDelegate,CLLocationManagerDelegate,UIApplicationDelegate>
{

    IBOutlet MKMapView *mapView;
    NSMutableArray *greenCities;
    CLLocationManager *locationManager;

}



//-(void) MKMapViewDelegate;
//-(void) CLLocationManagerDelegate;

//@property (nonatomic, weak) id<UIApplicationDelegate> delegate;
//@property (nonatomic, weak) id<MKMapViewDelegate> delegate;
//@property (nonatomic, weak) id<CLLocationManagerDelegate> delegate;



@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic,retain) IBOutlet MKMapView *mapView;
@property (nonatomic,retain) NSMutableArray *greenCities;
@property (strong, nonatomic) UIButton *menuBtn;
@property (strong, nonatomic) UIButton *searchBtn;



@end

Heres the .m

//
//  EleventhViewController.m
//  SlideMenu
//
//  Created by Kyle Begeman on 1/13/13.
//  Copyright (c) 2013 Indee Box LLC. All rights reserved.
//

#import "EleventhViewController.h"
#import "ECSlidingViewController.h"
#import "MenuViewController.h"
#import "GreenCitiesAppDelegate.h"
#import "GreenCitiesService.h"
#import "GreenCityAnnotation.h"
#import "GreenCityAnnotationView.h"
#import "GreenCity.h"


@interface EleventhViewController ()


//@interface EleventhViewController : UIViewController <MKMapViewDelegate>

@end

@implementation EleventhViewController
@synthesize window=_window,mapView,greenCities;
@synthesize menuBtn;
@synthesize searchBtn;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{






    self.mapView.delegate = self;

    locationManager = [[CLLocationManager alloc] init];
    [locationManager setDelegate:self];

    [locationManager setDistanceFilter:kCLDistanceFilterNone];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];

    [self.mapView setShowsUserLocation:YES];

    GreenCitiesService *greenService = [[GreenCitiesService alloc] init];

    self.greenCities = [greenService getGreenCities];

    [self.window makeKeyAndVisible];
    return YES;
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.window.layer.shadowOpacity = 0.75f;
    self.window.layer.shadowRadius = 10.0f;
    self.window.layer.shadowColor = [UIColor blackColor].CGColor;


    if (![self.slidingViewController.underLeftViewController isKindOfClass:[MenuViewController class]]) {
        self.slidingViewController.underLeftViewController  = [self.storyboard instantiateViewControllerWithIdentifier:@"Menu"];
    }


    self.menuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    menuBtn.frame = CGRectMake(9, 23, 40, 30);
    [menuBtn setBackgroundImage:[UIImage imageNamed:@"menuButton.png"] forState:UIControlStateNormal];
    [menuBtn addTarget:self action:@selector(revealMenu:) forControlEvents:UIControlEventTouchUpInside];

    [self.window addSubview:self.menuBtn];



    //Top Main Menu Search Button
    self.searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    searchBtn.frame = CGRectMake(275, 25, 40, 30);
    [searchBtn setBackgroundImage:[UIImage imageNamed:@"searchButton.png"] forState:UIControlStateNormal];
    [searchBtn addTarget:self action:@selector(revealMenu:) forControlEvents:UIControlEventTouchUpInside];

    [self.window addSubview:self.searchBtn];



}

- (void)mapView:(MKMapView *)mv didUpdateUserLocation:(MKUserLocation *)userLocation
{
    NSLog(@"didUpdateUserLocation fired!");

    CLLocationCoordinate2D maxCoord = {-90.0f,-180.0f};
    CLLocationCoordinate2D minCoord = {90.0f, 180.0f};

    for(int i = 0; i<=[self.greenCities count] - 1;i++)
    {
        GreenCity *gCity = (GreenCity *) [self.greenCities objectAtIndex:i];
        CLLocationCoordinate2D newCoord = { gCity.latitude, gCity.longitude };

        if(gCity.longitude > maxCoord.longitude)
        {
            maxCoord.longitude = gCity.longitude;
        }

        if(gCity.latitude > maxCoord.latitude)
        {
            maxCoord.latitude = gCity.latitude;
        }

        if(gCity.longitude < minCoord.longitude)
        {
            minCoord.longitude = gCity.longitude;
        }

        if(gCity.latitude < minCoord.latitude)
        {
            minCoord.latitude = gCity.latitude;
        }

        GreenCityAnnotation *annotation = [[GreenCityAnnotation alloc] initWithCoordinate:newCoord title:gCity.name subTitle:gCity.rank];

        [mv addAnnotation:annotation];
        // [annotation release];

    }


    MKCoordinateRegion region = {{0.0f, 0.0f}, {0.0f, 0.0f}};

    region.center.longitude = (minCoord.longitude + maxCoord.longitude) / 2.0;
    region.center.latitude = (minCoord.latitude + maxCoord.latitude) / 2.0;

    // calculate the span
    region.span.longitudeDelta = maxCoord.longitude - minCoord.longitude;
    region.span.latitudeDelta = maxCoord.latitude - minCoord.latitude;


    [self.mapView setRegion:region animated:YES];

}

- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views
{

}




- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)revealMenu:(id)sender
{
    [self.slidingViewController anchorTopViewTo:ECRight];
}

@end

My Project is pretty simple, uses a lot of free source code so feel free to download what I've made up to this point, I'm pretty sure it would make a good template/starting base for a lot of you:

https://www.dropbox.com/s/8xxx08zyqpr9i8v/CDF.zip

2
Your eleventh view controller doesn't seem to include a map view (in the storyboard). Also it crashes as the delegate hasn't been set.GuybrushThreepwood

2 Answers

0
votes

The method didFinishLauching with options should only be used in the app delegate. You need to move the code from here into viewDidLoad and delete the didFinishLaunching method. Also delete the reference you have to the app delegate from the storyboard xib.

You really want to then set breakpoints in the app and work out where the data load is going wrong.

0
votes

That fixed it! I moved every piece of executed code under the viewDidLoad! I feel like a noob (which I am) I would up vote you but I don't have enough rep yet. However I keep my connections to the app delegate I imported from the greencities .zip source. Thanks so much. This has however created a new bug in that I can't access my slider menu...fix one problem get another...I really appreciate the direction