iOS SDK - MapKit MKAnnotationView问题 - 位置和多个注释?
问题描述:
进度太快,试图学习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;
}
答
的基本概念是返回当且仅当问观点实际上是用户位置视图。 您只需测试转发给您的注释是否属于用户位置类,如下所示:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
// do your usual stuff
}
是的。这就是我所做的,上面,但谢谢。希望这会让别人更加明显地寻找这个答案。 – 2012-03-25 14:50:44