我在iOS中使用Firebase Geofire使用Objective-C从数据库中显示数据库数据如下所示MKAnnotation
然后我很容易加载注释
这是ViewController
类的代码
#import "ViewController.h"
#import "DetailViewController.h"
#import "Geofire.h"
@interface ViewController ()
{
CLLocationDegrees parkLat;
CLLocationDegrees parkLong;
NSString *keyy;
}
@property (nonatomic, strong) NSMutableDictionary *dict;
@property (nonatomic, strong) Pin *pinAnnotation;
@end
@implementation ViewController
@synthesize dict;
@synthesize title;
@synthesize pinAnnotation;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Initializers
FIRDatabaseReference *ref = [[FIRDatabase database] reference];
self.dict = [[NSMutableDictionary alloc] init];
_geo = [[GeoFire alloc] initWithFirebaseRef: [[[ref child:@"locations"] child:@"keyword"] child:@"teacher"]];
keyy = @"Park";
// Setting the title Variable
[[FIRAuth auth] addAuthStateDidChangeListener:^(FIRAuth * _Nonnull auth, FIRUser * _Nullable user) {
title = user.displayName;
}];
// Setting the Geo For your current
[[FIRAuth auth] addAuthStateDidChangeListener:^(FIRAuth * _Nonnull auth, FIRUser * _Nullable user) {
[self.geo setLocation:[[CLLocation alloc] initWithLatitude: lat longitude:log] forKey:[NSString stringWithFormat:@"%@", user.displayName] withCompletionBlock:^(NSError *error) {
if (error){
NSLog(@"%@", error.localizedDescription);
} else {
}
}];
}];
// Setting The Dummy park location
[self.geo setLocation:[[CLLocation alloc] initWithLatitude: parkLat longitude:parkLong] forKey: keyy];
}
- (void)loadPins{
self.regionQuery = [self.geo queryWithRegion:region];
// Observes the Coordinates return an array
[_regionQuery observeEventType:GFEventTypeKeyEntered withBlock:^(NSString *key, CLLocation *location) {
pinAnnotation = [[Pin alloc] init];
[pinAnnotation setKey: key];
[pinAnnotation setCoordinate:location.coordinate];
[pinAnnotation setSubtitle:keyy];
[pinAnnotation setName:@""];
[self.mapView addAnnotation:pinAnnotation];
// Setting the array if annotations
self.annotations[key] = pinAnnotation;
[self setData];
FIRDatabaseReference *ref = [[FIRDatabase database] reference];
[[[[ref child:@"locations"] child:@"keyword"] child:@"teacher"] observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
NSLog(@"%@", snapshot.value);
NSDictionary *dictionary = snapshot.value;
for (NSString *key in dictionary) {
NSDictionary *subDictionary = dictionary[key];
NSString *titlee = subDictionary[@"title"];
NSLog(@"%@",titlee);
[pinAnnotation setTitle: titlee];
}
}];
}];
}
- (void)setData{
// This sets the data i have made some changes in the geofire class so i can add params to the ref
dict = _geo.valuee;
[dict setObject:title forKey:@"title"];
title = [dict objectForKey:@"title"];
}
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
// load pins as the view appears
[self loadPins];
}
// Unwanted right now
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
if ([annotation isKindOfClass:[MKUserLocation class]]){
return nil;
}
// Normal Annotation Code
MKPinAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pin"];
[annotationView setRightCalloutAccessoryView:[UIButton buttonWithType:UIButtonTypeDetailDisclosure]];
[annotationView setCanShowCallout:YES];
return annotationView;
}
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{
// Going to the Detail View
DetailViewController *detailVC = [self.storyboard instantiateViewControllerWithIdentifier:@"Detail"];
NSString *aTitle = view.annotation.title;
[detailVC setText:aTitle];
[self presentViewController:detailVC animated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
loadPins
当我设置标题时,当您在方法内的 for 循环中查看时,我会轻松加载"Arfb Kashyap"
注释例如,他们的MKAnnotation
坐标是(请查看仪表板图片内部)
37.33233141
122.0312186
所以标题应该是"Aryan Kashyap"
但他们也是另一个注释
37.3317034
-122.0336955
所以标题应该是"Arfb Kashyap"
提前致谢!