将 XML 位置数据解析/传递到地图套件 xCode 5 时遇到问题

Posted

技术标签:

【中文标题】将 XML 位置数据解析/传递到地图套件 xCode 5 时遇到问题【英文标题】:Trouble Parsing/Passing XML location data into map kit xCode 5 【发布时间】:2014-03-31 15:25:35 【问题描述】:

按照本教程了解如何在 ios 地图套件上将 XML 纬度和经度数据显示为图钉: http://highoncoding.com/Articles/805_Consuming_XML_Feed_and_Displaying_Public_Information_on_the_MapView_Control.aspx

提供的示例代码可以正确编译并在美国各地显示引脚。但是,当我尝试将 .xib “移植”到我的应用程序中时,它会调出我的 Mapview 和当前用户位置,但不会丢弃任何引脚/解析数据? 我现在在第二天,有点令人沮丧。 这是我的 .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

【问题讨论】:

您的第十一个视图控制器似乎不包含地图视图(在情节提要中)。由于尚未设置委托,它也会崩溃。 【参考方案1】:

带有选项的方法 didFinishLauching 只能在应用委托中使用。您需要将代码从此处移至 viewDidLoad 并删除 didFinishLaunching 方法。还要从情节提要 xib 中删除您对应用委托的引用。

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

【讨论】:

好的 去处理一些家族企业,今晚晚些时候我会提供建议。欣赏方向,还有人愿意插话吗?会回帖【参考方案2】:

这解决了它!我在 viewDidLoad 下移动了每一段执行的代码!我觉得自己像个菜鸟(我是)我会投票给你,但我还没有足够的代表。但是,我保持与从 greencities .zip 源导入的应用程序委托的连接。非常感谢。然而,这产生了一个新错误,即我无法访问我的滑块菜单...解决一个问题得到另一个问题...我真的很感激这个方向

【讨论】:

以上是关于将 XML 位置数据解析/传递到地图套件 xCode 5 时遇到问题的主要内容,如果未能解决你的问题,请参考以下文章

将华为地图套件集成到HarmonyOs可穿戴设备应用中

将 XML 作为参数传递和解析到 XSLT 2.0

将javascript创建的复选框传递给控制器 以保存到表格?

用户添加放置图钉,谷歌地图

将华为地图套件集成到HarmonyOs可穿戴设备应用中

将华为地图套件集成到HarmonyOs可穿戴设备应用中