从MySQL数据库中提取MapView注释的坐标,但没有显示注释?

时间:2013-01-27 20:18:36

标签: objective-c annotations mkmapview sigabrt

我正在尝试从MySQL数据库中提取MapView的坐标,但出于某种原因,我的坐标只是没有出现在MapView上?

见下面我的代码。

MapViewController.h文件

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

@interface MapViewController : UIViewController  <MKMapViewDelegate> 
@property (nonatomic, strong) IBOutlet MKMapView *mapView;
@property (nonatomic, retain) NSMutableArray *dispensaries;
@property (nonatomic, retain) NSMutableData *data;
@end

MapViewController.m

#import "MapViewController.h"
#import "MapViewAnnotation.h"
#import "JSONKit.h"

@implementation MapViewController
@synthesize mapView;
@synthesize dispensaries;
@synthesize data;

#pragma mark - View lifecycle

- (void)viewDidLoad {        
    [super viewDidUnload];

    NSLog(@"Getting Device Locations");
    NSString *hostStr = @"http://stylerepublicmagazine.com/dispensaries.php";
    NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:hostStr]];
    NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];
    NSLog(@"server output: %@", serverOutput);
    NSMutableArray *array = [[[serverOutput objectFromJSONString] mutableCopy] autorelease];   
    dispensaries = [serverOutput objectFromJSONString];
    NSLog(@"%@", [serverOutput objectFromJSONString]);

    for (NSDictionary *dictionary in array) {
            assert([dictionary respondsToSelector:@selector(objectForKey:)]);

            CLLocationCoordinate2D coord = {[[dictionary objectForKey:@"lat"] doubleValue], [[dictionary objectForKey:@"lng"] doubleValue]};

            MapViewAnnotation *ann = [[MapViewAnnotation alloc] init];
            ann.title = [dictionary objectForKey:@"Name"];
            ann.coordinate = coord;
            [mapView addAnnotation:ann];
    }        

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

    self.mapView.delegate = self;
}


- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation     {
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800);
    [self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];

    MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
    point.coordinate = userLocation.coordinate;
    point.title = @"You Are Here";
    point.subtitle = @"Your current location";

    [self.mapView addAnnotation:point];
}

MapViewAnnotation.h

#import <Foundation/Foundation.h>
#import <MapKit/MKAnnotation.h>
#import <CoreLocation/CoreLocation.h>

@interface MapViewAnnotation : NSObject <MKAnnotation> {
    CLLocationCoordinate2D coordinate;
    NSString *title;
    NSString *subtitle;
}    
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@end

MapViewAnnotation.m

#import "MapViewAnnotation.h"

@implementation MapViewAnnotation    
@synthesize title, coordinate, subtitle;

-(void)dealloc{
    [title release];
    [super dealloc];
}    

@end

1 个答案:

答案 0 :(得分:0)

这似乎是你期待错误的JSON的问题:
您将serverOutput包装在一个数组中:

 NSMutableArray *array = [NSMutableArray arrayWithObject:[serverOutput objectFromJSONString]];    

您尝试访问JKDictionary(Json Kit字典)WITCH实际上是一个数组,因此不回答objectForKey:

尽量确保:

for (NSDictionary *dictionary in array) {
    assert([dictionary respondsToSelector:@selector(objectForKey:)]);

-

潜在解决方案

我相信你的额外包装会让它变坏。尝试:

NSMutableArray *array = [[[serverOutput objectFromJSONString] mutableCopy] autorelease];