0

我正在尝试从 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
4

1 回答 1

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];    
于 2013-01-27T20:27:03.670 回答