2015-06-14 50 views
0

我剛開始與「目標C and Parse`發展。 我在我的應用程序中有一個功能,允許用戶進入新的爬升。 數據存儲在我的Parse表中。我補充說,爬到我的應用程序的地圖視圖部分。Select語句解析目標C

我想寫存儲所有從查詢結果的成某種數組的SELECT語句,然後我會轉儲到地圖視圖部分。通過這種方式,我認爲每次添加新區域時都會動態地將地圖圖釘添加到地圖中,如果它尚不存在的話。

我是一個.NET開發人員,並在這種情況下我會抓住所有的數據,然後從那裏轉儲到一個數據表,並真正與它....但我不知道最好的做法就這樣做與我的上述情況在objective c

我會後我的代碼如下圖:

MapPin.h

#import <Foundation/Foundation.h> 
#import <MapKit/MapKit.h> 

@interface MapPin : NSObject <MKAnnotation> 
{ 
CLLocationCoordinate2D coordinate; 

NSString *title; 
NSString *subtitle; 
} 

@property (nonatomic, assign) CLLocationCoordinate2D coordinate; 
@property (nonatomic, copy) NSString *title; 
@property (nonatomic, copy) NSString *subtitle; 

@end 

MapPin.m

#import "MapPin.h" 

@implementation MapPin 

@synthesize coordinate, title, subtitle; 

@end 

MapViewController.h

#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 

MapViewController.m

#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 

對不起傾倒了很多在這裏的代碼......只是想爲大家提供了什麼我了。

回答

0

回答之前,一些注意事項: 啓動小寫的方法,它是一種常見的做法。 PIN和註釋是兩個diferent概念,比方說,一個腳是視圖和註釋的模型(或業務對象),所以它的更好,如果你打電話MapPin像MapAnnontation(或只是Annontation,導致地圖是多餘的)。很可能在不久的將來,您將擁有自定義引腳,它們是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的地方試試我的代碼。 - 如果你想提高性能,請按照我的邏輯,刪除不在當前答覆中的註釋,只添加新地點,如果它們不在地圖中。

希望這會有所幫助。