使用现有的分段控件来控制mkmapkit缩放

时间:2012-02-10 23:37:36

标签: iphone ios mapkit

我有一个分段控件,用于控制屏幕上的地址和电话号码以及其他一些标签。

我也尝试使用相同的控件来放大mkmapview上三个位置的特定位置,并根据他们按下的按钮在街景视图中进行缩放级别。任何帮助将不胜感激。

这是我的.m文件:

    @implementation LocationInfoViewController;
@synthesize mapView;

-(void)viewDidLoad

{
    [super viewDidLoad];
    [mapView setMapType:MKMapTypeStandard];
    [mapView setZoomEnabled:YES];
    [mapView setScrollEnabled:YES];
    [mapView setDelegate:self];

    MKCoordinateRegion morristown = { {0.0, 0.0} , {0.0, 0.0} };
    morristown.center.latitude = 40.797011;
    morristown.center.longitude = -74.482855;
    morristown.span.longitudeDelta = 0.60f;
    morristown.span.latitudeDelta = 0.60f;
    [mapView setRegion:morristown animated:YES];

    Annotation *ann1 = [[Annotation alloc] init];
    ann1.title = @"Sushi Lounge";
    ann1.subtitle = @"Morristown";
    ann1.coordinate = morristown.center;
    [mapView addAnnotation: ann1];

    MKCoordinateRegion hoboken = { {0.0, 0.0} , {0.0, 0.0} };
    hoboken.center.latitude = 40.738496;
    hoboken.center.longitude = -74.029434;
    hoboken.span.longitudeDelta = 0.60f;
    hoboken.span.latitudeDelta = 0.60f;
    [mapView setRegion:hoboken animated:YES];

    Annotation *ann2 = [[Annotation alloc] init];
    ann2.title = @"Sushi Lounge";
    ann2.subtitle = @"Hoboken";
    ann2.coordinate = hoboken.center;
    [mapView addAnnotation:ann2];

    MKCoordinateRegion totawa = { {0.0, 0.0} , {0.0, 0.0} };
    totawa.center.latitude = 40.875682;
    totawa.center.longitude = -74.301787;
    totawa.span.longitudeDelta = 0.60f;
    totawa.span.latitudeDelta = 0.60f;
    [mapView setRegion:hoboken animated:YES];

    Annotation *ann3 = [[Annotation alloc] init];
    ann3.title = @"Sushi Lounge";
    ann3.subtitle = @"Totawa";
    ann3.coordinate = totawa.center;
    [mapView addAnnotation:ann3];

}

-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation 

{
    MKPinAnnotationView *MyPin=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"current"];
    MyPin.pinColor = MKPinAnnotationColorPurple;

    UIButton *advertButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [advertButton addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];

    MyPin.rightCalloutAccessoryView = advertButton;
    MyPin.draggable = NO;
    MyPin.highlighted = YES;
    MyPin.animatesDrop=TRUE;
    MyPin.canShowCallout = YES;

    return MyPin;
}

-(void)button:(id)sender {
    NSLog(@"Button action");
}


    //Start of Segment control
-(IBAction) changeSegment:(id)sender


{
   //streetLabel--------------------------------------------- 
    if(control.selectedSegmentIndex == 0) {
        streetLabel.text = @"12 Schuyler Place";
    }
    if(control.selectedSegmentIndex == 1) {
        streetLabel.text = @"200 Hudson St.";
    }

    if(control.selectedSegmentIndex == 2) {
        streetLabel.text = @"235 Route 46 West";
    }

    //phoneLabel---------------------------------------------

    if(control.selectedSegmentIndex == 0) {
        phoneLabel.text = @"(973) 539-1135";
    }
    if(control.selectedSegmentIndex == 1) {
        phoneLabel.text = @"(201) 386-1117";
    }

    if(control.selectedSegmentIndex == 2) {
        phoneLabel.text = @"(973) 890-0007";
    }
    //Monday----------------------------------------------
    if(control.selectedSegmentIndex == 0) {
        hoursLabel1.text = @"11:30am - 11:00pm";
    }
    if(control.selectedSegmentIndex == 1) {
        hoursLabel1.text = @"11:30pm - 11:30pm";
    }

    if(control.selectedSegmentIndex == 2) {
        hoursLabel1.text = @"11:30am - 10:30pm";
    }
    //Tuesday------------------------------------------
    if(control.selectedSegmentIndex == 0) {
        hoursLabel2.text = @"11:30am - 11:00pm";
    }
    if(control.selectedSegmentIndex == 1) {
        hoursLabel2.text = @"11:30am - 11:30pm";
    }
    if(control.selectedSegmentIndex == 2) {
        hoursLabel2.text = @"11:30am -  10:30pm";
    }
    //Wednesday------------------------------------------
    if(control.selectedSegmentIndex == 0) {
        hoursLabel3.text = @"11:30am - 11:30pm";
    }
    if(control.selectedSegmentIndex == 1) {
        hoursLabel3.text = @"11:30am - 11:30pm";
    }
    if(control.selectedSegmentIndex == 2) {
        hoursLabel3.text = @"11:30am -  12:00am";
    }
    //Thursday------------------------------------------
    if(control.selectedSegmentIndex == 0) {
        hoursLabel4.text = @"11:30am - 12:00am";
    }
    if(control.selectedSegmentIndex == 1) {
        hoursLabel4.text = @"11:30am - 12:00pm";
    }
    if(control.selectedSegmentIndex == 2) {
        hoursLabel4.text = @"11:30am -  11:30pm";
    }
    //Friday------------------------------------------
    if(control.selectedSegmentIndex == 0) {
        hoursLabel5.text = @"11:30am - 12:00pm";
    }
    if(control.selectedSegmentIndex == 1) {
        hoursLabel5.text = @"11:30am - 1:00am";
    }
    if(control.selectedSegmentIndex == 2) {
        hoursLabel5.text = @"11:30am -  12:00am";
    }
    //Saturday------------------------------------------
    if(control.selectedSegmentIndex == 0) {
        hoursLabel6.text = @"12:00pm - 12:00am";
    }
    if(control.selectedSegmentIndex == 1) {
        hoursLabel6.text = @"12:00pm - 1:00am";
    }
    if(control.selectedSegmentIndex == 2) {
        hoursLabel6.text = @"12:00pm -  12:00am";
    }
    //Sunday------------------------------------------
    if(control.selectedSegmentIndex == 0) {
        hoursLabel7.text = @"12:00pm - 11:00pm";
    }
    if(control.selectedSegmentIndex == 1) {
        hoursLabel7.text = @"12:00pm - 11:30pm";
    }
    if(control.selectedSegmentIndex == 2) {
        hoursLabel7.text = @"12:00pm - 10:30pm";
    }
    if (control.selectedSegmentIndex == 0) {
        mapView.center.x == 40.738496;


    }
}





- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView

{

}
- (void)viewDidUnload
{

    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

*/

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

2 个答案:

答案 0 :(得分:0)

更好地做更多阅读,这是您可能想要学习的示例代码:

MapCallouts

答案 1 :(得分:0)

一个简单的解决方案是在ann1中制作注释变量ann2ann3viewDidLoad ivars而不是局部变量。

然后,您就可以轻松访问它们并在changeSegment:方法中更改地图的坐标:

if (control.selectedSegmentIndex == 0) {
    //set streetLabel, phoneLabel, and hoursLabelX here also
    [mapView setRegion:MKCoordinateRegionMakeWithDistance(ann1.coordinate, 1000, 1000) animated:YES];
}
else 
if (control.selectedSegmentIndex == 1) {
    //set streetLabel, phoneLabel, and hoursLabelX here also
    [mapView setRegion:MKCoordinateRegionMakeWithDistance(ann2.coordinate, 1000, 1000) animated:YES];
}
else
if (control.selectedSegmentIndex == 2) {
    //set streetLabel, phoneLabel, and hoursLabelX here also
    [mapView setRegion:MKCoordinateRegionMakeWithDistance(ann3.coordinate, 1000, 1000) animated:YES];
}

顺便说一句,您可以通过将所有适用于相同条件的语句放在上面的注释中而不是单独提及,来大大简化和缩短changeSegment:中的代码。

相关问题