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 に答える