進度太快,試圖學習MapKit和註解的進出。iOS SDK - MapKit MKAnnotationView問題 - 位置和多個註釋?
我已經取得了一些很好的進展,但當我嘗試將位置服務添加到組合中時遇到問題。
如果我回到零到
- (MKAnnotationView *)mapView:(MKMapView *)mapView
viewForAnnotation:(id <MKAnnotation>)annotation
方法,我變得非常接近我正在尋找一個結果,我有我的註釋針,我有我的用戶的位置在一個標準的顯示辦法。
但如果我回到myPins(有如下設置的所有參數),我得到正確的行爲上所有引腳但性能也爲用戶的位置集(這是一個紅色的每當視圖移動或選擇一個引腳時,它就會落入到位)。煩人。
如何設置註釋的屬性與位置分開?有沒有辦法返回多個MKAnnotationViews?我知道我錯過了一些基本概念,但希望你能給我提供任何幫助。提前致謝。
這裏是我的主要的viewController,總數:
//
// ViewController.m
// mapFun
//
#import "ViewController.h"
@implementation ViewController
@synthesize segmentedControl;
@synthesize mapView,location, myLocations, myAnnotation, region;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.mapView.delegate = self;
//location manager stuff
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
if([CLLocationManager locationServicesEnabled])
{
[self.mapView setShowsUserLocation:YES];
[locationManager startUpdatingHeading];
}
[self loadUpLocations];
//set first zoom center and span, to zoom from to initial zoom
region.center=CLLocationCoordinate2DMake(
33.75070416856952,
-84.37034368515015);
MKCoordinateSpan span;
span.latitudeDelta=.3;
span.longitudeDelta=.3;
region.span=span;
[mapView setRegion:region animated:YES];
}
- (void)viewDidUnload
{
[self setMapView:nil];
[self setSegmentedControl:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
//zoom to initial zoom
[self setInitialZoom];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
//Load up a buncha locations
-(void)loadUpLocations{
myLocations = [LoadObjectsFromFile loadFromFile:@"locations2" ofType:@"plist"];
//loop through and make annotations
for (NSString *loc in myLocations) {
NSDictionary *value =[myLocations objectForKey:loc];
//create instance of MapAnnotations
CLLocationCoordinate2D temp =
CLLocationCoordinate2DMake(
[[value objectForKey:@"latitude"] doubleValue],
[[value objectForKey:@"longitude"] doubleValue]);
myAnnotation = [[MapAnnotations alloc]initWithLocation:temp];
myAnnotation.title = [value objectForKey:@"title"];
myAnnotation.subtitle = [value objectForKey:@"subtitle"];
myAnnotation.info =[value objectForKey:@"info"];
[self.mapView addAnnotation:myAnnotation];
}
}
- (void)setInitialZoom{
// MKCoordinateRegion region;
region.center = CLLocationCoordinate2DMake(33.77, -84.37);
//set level of zoom
MKCoordinateSpan span;
span.latitudeDelta=.04;
span.longitudeDelta=.04;
region.span=span;
[mapView setRegion:region animated:YES];
}
// used when selecting annotations
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
MKPointAnnotation *selectedAnnotation = view.annotation;
NSLog(@"The annotation selected is %@.",selectedAnnotation.title);
//set level of zoom
CLLocationCoordinate2D newCenter =
CLLocationCoordinate2DMake(
selectedAnnotation.coordinate.latitude,
selectedAnnotation.coordinate.longitude);
MKCoordinateSpan span;
span.latitudeDelta=.02;
span.longitudeDelta=.02;
MKCoordinateRegion zoomRegion = MKCoordinateRegionMake(newCenter, span);
[ self.mapView setRegion:zoomRegion animated:YES];
}
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
// [self zoomOutToInitial:nil];
}
- (IBAction)zoomOutToInitial:(id)sender {
[self.mapView setRegion:region animated:YES];
}
- (IBAction)segChanged:(id)sender {
if (self.segmentedControl.selectedSegmentIndex==0) {
[self.mapView setMapType:MKMapTypeStandard];
}
if (self.segmentedControl.selectedSegmentIndex==1) {
[self.mapView setMapType:MKMapTypeSatellite];
}
if (self.segmentedControl.selectedSegmentIndex==2) {
[self.mapView setMapType:MKMapTypeHybrid];
}
}
// This gets called every time an annotation is in the map view
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
NSLog(@"Map view can see annotation %@.",annotation.title);
MKPinAnnotationView *myPins;
if (!myPins) {
myPins= [[MKPinAnnotationView alloc]initWithAnnotation:self.myAnnotation
reuseIdentifier:@"myPins"];
}
myPins.animatesDrop = YES;
myPins.canShowCallout = YES;
myPins.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
return nil;
//return myPins;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
[manager stopUpdatingHeading];
}
@end
回答我的問題。不管怎麼說,還是要謝謝你。
我終於從蘋果找到了答案中稱爲有用文件「Location Awareness Programming Guide」。
這裏的修正 MKAnnotationView方法:
// This gets called every time an annotation is in the map view
- (MKAnnotationView *)mapView:(MKMapView *)theView
viewForAnnotation:(id <MKAnnotation>)annotation
{
NSLog(@"Map view can see annotation %@.",annotation.title);
//if the annotation is a user location
if ([annotation isKindOfClass:[MKUserLocation class]]) {
return nil;
}
//uses my custom class
if ([annotation isKindOfClass:[MapAnnotations class]]) {
// try for reuse of pins
MKPinAnnotationView *myPins = (MKPinAnnotationView *)
[theView dequeueReusableAnnotationViewWithIdentifier:@"myPins"];
if (!myPins) {
myPins= [[MKPinAnnotationView alloc]initWithAnnotation:annotation
reuseIdentifier:@"myPins"];
myPins.animatesDrop = YES;
myPins.canShowCallout = YES;
myPins.rightCalloutAccessoryView =
[UIButton buttonWithType:UIButtonTypeDetailDisclosure];
}else
myPins.annotation = annotation;
return myPins;
}
return nil;
}
是的。這就是我所做的,上面,但謝謝。希望這會讓別人更加明顯地尋找這個答案。 – 2012-03-25 14:50:44