Ios Select语句解析目标C
我刚刚开始开发“客观分析”。 我的应用程序中有一个功能,允许用户进入新的爬升。 数据在解析时存储在我的一个表中。我将这个爬升添加到我的应用程序的地图视图部分 我想编写一个select语句,将查询的所有结果存储到某种数组中,然后将其转储到映射视图部分。通过这种方式,我认为它会在每次添加新区域时动态地向地图添加一个地图pin,当然,如果它还不存在的话 我是一名.net开发人员,在这个场景中,我会获取所有数据,然后将其转储到数据表中,然后从那里开始使用它。。。。但我不确定在目标c中的上述场景中实现这一点的最佳实践 我将在下面发布我的地图代码: MapPin.h MapViewController.h MapViewController.mIos Select语句解析目标C,ios,objective-c,iphone,parse-platform,Ios,Objective C,Iphone,Parse Platform,我刚刚开始开发“客观分析”。 我的应用程序中有一个功能,允许用户进入新的爬升。 数据在解析时存储在我的一个表中。我将这个爬升添加到我的应用程序的地图视图部分 我想编写一个select语句,将查询的所有结果存储到某种数组中,然后将其转储到映射视图部分。通过这种方式,我认为它会在每次添加新区域时动态地向地图添加一个地图pin,当然,如果它还不存在的话 我是一名.net开发人员,在这个场景中,我会获取所有数据,然后将其转储到数据表中,然后从那里开始使用它。。。。但我不确定在目标c中的上述场景中实现这一
很抱歉在这里转储了很多代码。。。只是想给大家提供我所拥有的。在回答之前,请注意: 启动方法小写,这是一种常见的做法。 pin和注释是两个不同的概念,假设pin是视图,而注释是模型或业务对象,因此最好像MapAnnontation一样调用MapPin,或者只调用Annontation,因为Map是多余的。可能在不久的将来,您将拥有自定义管脚,这是MKAnnotationView的一个子类 对于您的回复,我有一个从www.sharewifiapp.com解析中检索当前地图区域位置的应用程序。这是代码,下面是一些说明:
#pragma mark MKMapViewDelegate
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated {
[self.hotspotsQuery cancel];
}
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
//DDDebugLog(@"zoom %lu", (unsigned long)[self.mapView zoomLevel]);
// retrieve all hotspots using the filtering and the current view port
self.hotspotsQuery = [PFQuery queryWithClassName:[SWHotspot parseClassName]];
MKMapRect mRect = self.mapView.visibleMapRect;
MKMapPoint neMapPoint = MKMapPointMake(MKMapRectGetMaxX(mRect), mRect.origin.y);
MKMapPoint swMapPoint = MKMapPointMake(mRect.origin.x, MKMapRectGetMaxY(mRect));
CLLocationCoordinate2D neCoord = MKCoordinateForMapPoint(neMapPoint);
CLLocationCoordinate2D swCoord = MKCoordinateForMapPoint(swMapPoint);
PFGeoPoint* swGeoPoint=[PFGeoPoint geoPointWithLatitude:swCoord.latitude longitude:swCoord.longitude];
PFGeoPoint* neGeoPoint=[PFGeoPoint geoPointWithLatitude:neCoord.latitude longitude:neCoord.longitude];
[self.hotspotsQuery whereKey:@"location" withinGeoBoxFromSouthwest:swGeoPoint toNortheast:neGeoPoint];
[self.hotspotsQuery includeKey:@"owner"];
[self.hotspotsQuery orderByDescending:@"updatedAt"];
self.hotspotsQuery.limit=1000;
self.loadingHotspotsActivityView.hidden=NO;
[self.hotspotsQuery findObjectsInBackgroundWithBlock:^(NSArray *hotspots, NSError *error) {
self.loadingHotspotsActivityView.hidden=YES;
if (error) {
DDLogError(@"Error retrieving hotspots: %@", [error userInfo][@"error"]);
} else {
// TEST.
/*
NSLog(@"retrieved: %lu", (unsigned long)objects.count);
[self.mapView removeAnnotations:self.mapView.annotations];
[self.mapView addAnnotations:objects];
*/
// remove hotspots that are not in the current response (hotstpots)
NSMutableArray* hotspotsToRemove=[self.mapView.annotations mutableCopy];
[hotspotsToRemove removeObjectsInArray:hotspots];
[self.mapView removeAnnotations:hotspotsToRemove];
// add hotpots from the current response that were not in the original set
NSMutableArray* hotspotsToAdd=[hotspots mutableCopy];
[hotspotsToAdd removeObjectsInArray:self.mapView.annotations];
[self.mapView addAnnotations:hotspotsToAdd];
}
}];
}
要点:
-保存解析查询,以便取消它。
-当用户开始移动地图时取消,当用户停止移动地图时启动新请求。
-每次从解析查询接收到新位置时,您都可以删除所有注释并放置新注释,但从性能角度来看,这并不好。如果你想试试我的代码,上面写着TEST。
-如果要提高性能,请遵循我的逻辑,即删除不在当前回复中的注释,并且仅在它们不在映射中时添加新位置
希望这有帮助
#import "MapPin.h"
@implementation MapPin
@synthesize coordinate, title, subtitle;
@end
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface MapViewController : UIViewController
{
MKMapView *mapView;
}
@property (nonatomic, retain) IBOutlet MKMapView *mapView;
-(IBAction)SetMap:(id)sender;
-(IBAction)GetLocation:(id)sender;
-(IBAction)Directions:(id)sender;
@end
#import "MapViewController.h"
#import "MapPin.h"
@interface MapViewController ()<MKMapViewDelegate>
@end
@implementation MapViewController
@synthesize mapView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
mapView.delegate = self;
//new
//[self.mapView setShowsUserLocation:YES];
//Moss Preserve annotation and map pin
MapPin *MossPreserveAnnotation = [[MapPin alloc] init];
MossPreserveAnnotation.title = @"Moss Rock Preserve Boulder Fields";
MossPreserveAnnotation.subtitle = @"Hoover, AL";
MossPreserveAnnotation.coordinate = CLLocationCoordinate2DMake(33.3816566, -86.8415451);
[mapView addAnnotation:MossPreserveAnnotation];
//Setup map
MKCoordinateRegion mapCoordRegion;
mapCoordRegion.center.latitude = 39;
mapCoordRegion.center.longitude = -97;
mapCoordRegion.span.latitudeDelta = 60.0;
mapCoordRegion.span.longitudeDelta = 60.0;
[mapView setRegion:mapCoordRegion];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
self.navigationController.navigationBar.hidden = NO;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
// Create an MKMapItem to pass to the Maps app
MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:view.annotation.coordinate
addressDictionary:nil];
MKMapItem *MapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
[MapItem setName:view.annotation.title];
NSDictionary *launchOptions = @{MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving};
// Get the "Current User Location" MKMapItem
MKMapItem *currentLocationItem = [MKMapItem mapItemForCurrentLocation];
[MKMapItem openMapsWithItems:@[currentLocationItem, MapItem]
launchOptions:launchOptions];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
MKAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:@"MKPinAnnotationView"];
annotationView.canShowCallout = YES;
UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[detailButton setTintColor:[UIColor colorWithRed:183/255.0 green:207/255.0 blue:85/255.0 alpha:0.5]];
annotationView.rightCalloutAccessoryView = detailButton;
return annotationView;
}
-(IBAction)SetMap:(id)sender;
{
switch (((UISegmentedControl *) sender).selectedSegmentIndex)
{
case 0:
mapView.mapType = MKMapTypeStandard;
break;
case 1:
mapView.mapType = MKMapTypeSatellite;
break;
case 2:
mapView.mapType = MKMapTypeHybrid;
break;
default:
break;
}
}
-(IBAction)GetLocation:(id)sender;
{
mapView.showsUserLocation = YES;
}
-(IBAction)Directions:(id)sender;
{
NSString *urlString = @"http://maps.apple.com/maps?daddr=33.3816566,-86.8415451";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}
@end
#pragma mark MKMapViewDelegate
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated {
[self.hotspotsQuery cancel];
}
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
//DDDebugLog(@"zoom %lu", (unsigned long)[self.mapView zoomLevel]);
// retrieve all hotspots using the filtering and the current view port
self.hotspotsQuery = [PFQuery queryWithClassName:[SWHotspot parseClassName]];
MKMapRect mRect = self.mapView.visibleMapRect;
MKMapPoint neMapPoint = MKMapPointMake(MKMapRectGetMaxX(mRect), mRect.origin.y);
MKMapPoint swMapPoint = MKMapPointMake(mRect.origin.x, MKMapRectGetMaxY(mRect));
CLLocationCoordinate2D neCoord = MKCoordinateForMapPoint(neMapPoint);
CLLocationCoordinate2D swCoord = MKCoordinateForMapPoint(swMapPoint);
PFGeoPoint* swGeoPoint=[PFGeoPoint geoPointWithLatitude:swCoord.latitude longitude:swCoord.longitude];
PFGeoPoint* neGeoPoint=[PFGeoPoint geoPointWithLatitude:neCoord.latitude longitude:neCoord.longitude];
[self.hotspotsQuery whereKey:@"location" withinGeoBoxFromSouthwest:swGeoPoint toNortheast:neGeoPoint];
[self.hotspotsQuery includeKey:@"owner"];
[self.hotspotsQuery orderByDescending:@"updatedAt"];
self.hotspotsQuery.limit=1000;
self.loadingHotspotsActivityView.hidden=NO;
[self.hotspotsQuery findObjectsInBackgroundWithBlock:^(NSArray *hotspots, NSError *error) {
self.loadingHotspotsActivityView.hidden=YES;
if (error) {
DDLogError(@"Error retrieving hotspots: %@", [error userInfo][@"error"]);
} else {
// TEST.
/*
NSLog(@"retrieved: %lu", (unsigned long)objects.count);
[self.mapView removeAnnotations:self.mapView.annotations];
[self.mapView addAnnotations:objects];
*/
// remove hotspots that are not in the current response (hotstpots)
NSMutableArray* hotspotsToRemove=[self.mapView.annotations mutableCopy];
[hotspotsToRemove removeObjectsInArray:hotspots];
[self.mapView removeAnnotations:hotspotsToRemove];
// add hotpots from the current response that were not in the original set
NSMutableArray* hotspotsToAdd=[hotspots mutableCopy];
[hotspotsToAdd removeObjectsInArray:self.mapView.annotations];
[self.mapView addAnnotations:hotspotsToAdd];
}
}];
}