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

Posted

技术标签:

【中文标题】从 MySQL 数据库中提取 MapView 注释的坐标,但没有显示注释?【英文标题】:Pulling coordinates for MapView annotations from MySQL database, but annotations aren't showing? 【发布时间】:2013-01-27 20:18:36 【问题描述】:

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

这似乎是您期待错误 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];    

【讨论】:

我已经实现了您的解决方案,它阻止了我的应用程序崩溃:) 它还显示了我当前的位置,但由于某种原因,它仍然没有在地图视图上显示我的数据库中的注释。请参阅上面的更新代码。 在 addAnnotation 调用处停止调试器并验证此时数据是否正常。那么,您还没有向我们展示您的班级MapViewAnnotation,所以这有点猜测:) 刚刚在上面发布了我的 MapViewAnnotation 代码 :) 我觉得我缺少一些基本的东西?哈哈。我实现了一个 NSLog 以确保数据/JSON 正常工作,并且一切看起来都很好。 不相关但.... (void)viewDidLoad [super viewDidUnload]; ...很可能是错误的:D 超级奇怪,但我重新启动了 xCode,你的初始答案才开始起作用。哈哈!避免了危机,出现了注释,一切都很棒:) THX!

以上是关于从 MySQL 数据库中提取 MapView 注释的坐标,但没有显示注释?的主要内容,如果未能解决你的问题,请参考以下文章

从 MapView 委托方法获取错误注释

如何检测在 MapView 中选择了哪个注释

在解析查询中添加 mapview 注释会随机返回 null

使用导航控制器从 mapView 推送详细视图

如何从注释标注Xcode 9设置警报

如何更改 MapView 注释中的标注气泡颜色?