麻烦解析/将XML位置数据传递到地图工具包xCode 5中

时间:2014-03-31 15:25:35

标签: ios iphone xml mapkit

按照本教程,如何在iOS地图工具包上显示XML纬度和经度数据: http://highoncoding.com/Articles/805_Consuming_XML_Feed_and_Displaying_Public_Information_on_the_MapView_Control.aspx

提供的示例代码正确编译并显示整个美国各地的引脚。但是,当我尝试" port" .xib进入我的应用程序它提取了我的Mapview和当前用户的位置,但它不会丢弃任何引脚/解析数据?
我现在在第2天,有点令人沮丧。 继承人我的.m和.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

继承人.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

我的项目非常简单,使用了大量免费的源代码,因此可以随意下载我已经做到的这一点,我很确定它会成为一个很好的模板/起始基础很多人:

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

2 个答案:

答案 0 :(得分:0)

带有选项的didFinishLauching方法应该只在app delegate中使用。您需要将代码从此处移动到viewDidLoad并删除didFinishLaunching方法。同时从故事板xib中删除您对app delegate的引用。

你真的想在应用程序中设置断点并找出数据加载出错的地方。

答案 1 :(得分:0)

修好了!我在viewDidLoad下移动了每一段已执行的代码!我觉得自己像个菜鸟(我是)我会投票给你,但我还没有足够的代表。但是,我保持与从greencities .zip源导入的app委托的连接。非常感谢。然而,这已经创建了一个新的错误,我无法访问我的滑块菜单...修复一个问题得到另一个...我真的很欣赏方向