주석을위한 제목 및 부제목과 함께 여러 GeoPoint를 얻기 위해 쿼리를 수행하고 있습니다. 쿼리는 블록의 구문 분석에서 데이터를 다운로드하고 self.locations를 기록 할 수 있습니다. 그러나, viewDidLoad에 self.locations를 기록/사용하려고하면 null이 반환됩니다. 내가 여기서 무엇을 놓치고 있니?여러 GeoPoint를 구문 분석에서 mapView에 추가하려고 시도했습니다.
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import <Parse/Parse.h>
@interface PeopleMoverMap : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>
@property (nonatomic) double userLat;
@property (nonatomic) double userLong;
@property (nonatomic) NSArray *locations;
@property (nonatomic) CLLocationManager *locationManager;
@property (nonatomic) NSMutableArray *lats;
@property (nonatomic) NSMutableArray *lngs;
@property (strong, nonatomic) IBOutlet MKMapView *mapView;
-(void)retrieveLocations;
@end
하는 .m 파일 :
#import "PeopleMoverMap.h"
#import "MapAnnotation.h"
@interface PeopleMoverMap()
@end
@implementation PeopleMoverMap
- (void)viewDidLoad
{
[super viewDidLoad];
[self performSelector:@selector(retrieveLocations)];
self.mapView.delegate = self;
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];
// Add multiple pins
for (NSDictionary *location in self.locations) {
PFGeoPoint *tempGeo = location[@"GeoPoint"];
CLLocationCoordinate2D annotationCoordinate = CLLocationCoordinate2DMake(tempGeo.latitude, tempGeo.longitude);
MapAnnotation *annotation = [[MapAnnotation alloc] init];
annotation.coordinate = annotationCoordinate;
// annotation.title = location[@"Name"];
// annotation.subtitle = location[@"Place"];
[self.mapView addAnnotation:annotation];
}
// Get user coordinates
self.userLat = self.locationManager.location.coordinate.latitude;
self.userLong = self.locationManager.location.coordinate.longitude;
NSLog(@"%f:%f", self.userLat, self.userLong);
// Initialize and set arrays
self.lats = [[NSMutableArray alloc] init];
self.lngs = [[NSMutableArray alloc] init];
[self.lats addObject:[NSNumber numberWithDouble:self.userLat]];
[self.lngs addObject:[NSNumber numberWithDouble:self.userLong]];
for (NSDictionary *dict in self.locations) {
PFGeoPoint *tempGeo = dict[@"GeoPoint"];
[self.lats addObject:[NSNumber numberWithDouble:tempGeo.latitude]];
[self.lngs addObject:[NSNumber numberWithDouble:tempGeo.longitude]];
}
// Sort numbers in arrays
[self.lats sortUsingSelector:@selector(compare:)];
[self.lngs sortUsingSelector:@selector(compare:)];
// Get smalles/biggest coordinates
double smallestLat = [self.lats[0] doubleValue];
double smallestLng = [self.lngs[0] doubleValue];
double biggestLat = [[self.lats lastObject] doubleValue];
double biggestLng = [[self.lngs lastObject] doubleValue];
// Get Center Point and Span
CLLocationCoordinate2D annotationsCenter = CLLocationCoordinate2DMake((biggestLat + smallestLat)/2, (biggestLng + smallestLng)/2);
MKCoordinateSpan annotationsSpan = MKCoordinateSpanMake((biggestLat - smallestLat) * 1.75, (biggestLng - smallestLng) * 1.75);
// Create and set Region
MKCoordinateRegion region = MKCoordinateRegionMake(annotationsCenter, annotationsSpan);
[self.mapView setRegion:region];
}
-(void)retrieveLocations
{
PFQuery *retrievePMLocations = [PFQuery queryWithClassName:@"peopleMoverLocations"];
[retrievePMLocations orderByAscending:@"Name"];
[retrievePMLocations findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
self.locations = [[NSArray alloc] initWithArray:objects];
}
}];
}
@end