gpt4 book ai didi

ios - 将 Parse PFGeoPoints 数组显示为 map 注释错误

转载 作者:行者123 更新时间:2023-11-29 12:26:56 25 4
gpt4 key购买 nike

我试图在 map 上显示存储在 NSArray 中的对象。在执行获取用户 2 英里范围内位置的方法后,这些对象是从名为 Affiliates 的 Parse.com 类中提取的。我已经使用 NSLog 来确认查询正在正确执行,所以我知道提取也正在正确执行。

当我尝试运行该应用程序时,我在 for(NSDictionary) 部分抛出一个错误并且该应用程序卡住。错误状态为“Thread 1: EXC_BAD_ACCESS (access code 1, address 0x80120)” 在咨询了 Google 后我发现这是某种类型的内存分配问题,但我不知道它为什么会发生或如何修复它。

#import "ViewController.h"
#import "MapAnnotation.h"

@import CoreLocation;

@interface ViewController () <CLLocationManagerDelegate>

@property (nonatomic,strong) NSArray *affiliates;

@end

@implementation ViewController

- (void)viewDidLoad {


[super viewDidLoad];

// Ask for authorization to collect location from user
CLLocationManager * locationManager = [[CLLocationManager alloc] init];
// Check for iOS 8. Without this guard the code will crash with "unknown selector" on iOS 7.
if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {

locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
[locationManager requestWhenInUseAuthorization];
}

//Set map options
self.mapView.showsUserLocation = YES;
self.mapView.delegate = self;
self.mapView.scrollEnabled = YES;
self.mapView.zoomEnabled = YES;
self.mapView.userTrackingMode = YES;

// Store the user's location as a Parse PFGeoPoint and call the fetchAffiliatesNearPoint method
[PFGeoPoint geoPointForCurrentLocationInBackground:^(PFGeoPoint *geoPoint, NSError *error) {
if (!error) {
[self fetchAffiliatesNearPoint:geoPoint];
NSLog(@"Got User Location! %@", geoPoint);
}
}];

/* This is where I am having issues

for(NSDictionary *affiliates in affiliates) {
CLLocationCoordinate2D annotationCoordinate = CLLocationCoordinate2DMake([affiliates[@"latitude"] doubleValue], [affiliates[@"longitude"] doubleValue]);

MapAnnotation *annotation = [[MapAnnotation alloc] init];
annotation.coordinate = annotationCoordinate;
annotation.title = affiliates[@"name"];
annotation.subtitle = affiliates[@"url"];
[self.mapView addAnnotation:annotation];
}

*/

}


//Fetch an array of affiliates that are within two miles of the user's current location.
- (void)fetchAffiliatesNearPoint:(PFGeoPoint *)geoPoint
{

PFQuery *query = [PFQuery queryWithClassName:@"Affiliates"];
[query whereKey:@"geometry" nearGeoPoint:geoPoint withinMiles:2.0];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error)
{
self.affiliates = objects;
NSLog(@"Nearby Locations %@", _affiliates);
}
}];
}


-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{


//Zoom map to users current location
if (!self.initialLocation) {
self.initialLocation = userLocation.location;
MKCoordinateRegion mapRegion;
mapRegion.center = mapView.userLocation.coordinate;
mapRegion.span.latitudeDelta = .025;
mapRegion.span.longitudeDelta = .025;

[mapView setRegion:mapRegion animated: YES];
}

}
@end

最佳答案

如果要将自定义对象添加到 MapKit map ,您的自定义对象必须符合 MKAnnotation 协议(protocol)。要使您的对象符合协议(protocol),您只需要实现一个属性:

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

其余的属性(例如 titlesubtitle)和方法是可选的。

您需要做的就是在 PFGeoPoint 上创建一个类别(假设您使用的是 Objective-C)并为 coordinate 属性实现一个 getter。

PFGeoPoint+Annotation.h

@interface PFGeoPoint (Annotation) <MKAnnotation>

@end

PFGeoPoint+Annotation.m

@implementation PFGeoPoint (Annotation)

- (CLLocationCoordinate2D)coordinate {
return CLLocationCoordinate2DMake(self.latitude, self.longitude);
}

@end

最后,要将其添加到 map ,您将使用:

- (void)addAnnotation:(id <MKAnnotation>)annotation;

例如:[mapView addAnnotation:geoPoint];

关于ios - 将 Parse PFGeoPoints 数组显示为 map 注释错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28788975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com