在我的应用程序中,我有一个mapView和一个按钮,该按钮用于在标准mapView和卫星视图之间切换mapView。
这是快照,如果我按下卫星按钮,mapView应该变为卫星视图,按钮图像应该更改为下图,
现在按下按钮它应该变为普通的mapView。
//mapView
camera=[GMSCameraPosition cameraWithLatitude:13.45 longitude:80.93 zoom:6];
mapView=[GMSMapView mapWithFrame:CGRectMake(0, 0, width, height) camera:camera];
mapView.myLocationEnabled=YES;
//button
satellite=[UIButton buttonWithType:UIButtonTypeCustom];
satellite.Frame=CGRectMake(250, 300, 65 , 65);
[satellite setBackgroundImage:[UIImage imageNamed:@"Satellite.png"] forState:UIControlStateNormal];
[satellite addTarget:self action:@selector(satelliteView:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:mapView];
[self.view addSubview:satellite];
答案 0 :(得分:2)
简单:
尝试使用UIButton的标记。默认情况下,为标准mapView指定标签,用于Ex mapButton.tag = 100;
- (IBAction)mapSatelliteSegmentControlTapped:(UIButton *)sender
{
if(sender.tag == 100)
{
self.mapView.mapType = MKMapTypeStandard;
// Change the MapButton image as well as tag
self.mapButton.tag = 101; //
[self.mapButton setImage:[UIImage imageNamed:@"mapSateliite.png"] forState:UIControlStateNormal];
}
else
{
self.mapView.mapType = MKMapTypeSatellite;
// Change the MapButton image as well as tag
self.mapButton.tag = 100; //
[self.mapButton setImage:[UIImage imageNamed:@"mapStandard.png"]
}
}
答案 1 :(得分:2)
由于您使用单个按钮在两种类型的地图之间切换,因此您必须跟踪按钮的状态。假设您的按钮默认显示正常的地图视图。
@property(nonatomic, assign) BOOL isSatelliteMap;
当视图第一次加载为isSatelliteMap = NO;
时,将其设为FALSE,这意味着您将首次加载MKMapTypeStandard
&按钮将显示SatelliteImage
。
现在按下按钮时:
-(void)ButtonPressed:(UIButton*)sender
{
if(!isSatelliteMap)
{
//set MKMapTypeSatellite
self.mapView.mapType = MKMapTypeSatellite;
[sender setImage:[UIImage imageNamed:@"ButtonMapNormal.png"] forState:UIControlStateNormal];
isSatelliteMap = YES;
}
else
{
self.mapView.mapType = MKMapTypeStandard;
[sender setImage:[UIImage imageNamed:@"ButtonMapSatellite.png"] forState:UIControlStateNormal];
isSatelliteMap = NO;
}
}
这件事可以应用于任何类型的地图。我的意思是你可以为谷歌地图做同样的事情。 希望这会有所帮助。
答案 2 :(得分:1)
您可以将下面的代码添加到内部方法中以更改按钮图像
-(void)settalite:(id)sender{
UIbutton *btn=(UIButton *)sender;
[btn setBackgroundImage:[UIImage imageNamed:@"map.png"] forState:UIControlStateNormal];
}
答案 3 :(得分:1)
我只是给你一个建议,在按钮点击方法中添加你的MKMapView
,并给出按钮标记等。 (在您的情况下,更改 GoolgeMap )
myButton.tag = 101;
以下是按钮方法。
-(void)buttonPressed:(UIButton *) sender
{
if(self.mapView)
{
[self.mapView removeFromSuperView];
}
self.mapView = [[MKMapView alloc] init];
self.mapView.frame = self.view.bounds;
[self.mapView setZoomEnabled:YES];
[self.mapView setScrollEnabled:YES];
[self.mapView setDelegate:self];
[self.view addSubview:self.mapView];
if(sender.tag ==101)
{
[self.mapView setMapType:MKMapTypeSatellite];
[btnGoHome setImage:[UIImage imageNamed:@"changedImage.png"] forState:UIControlStateNormal];
sender.tag = 100;
}
else
{
[btnGoHome setImage:[UIImage imageNamed:@"previouseImage.png"] forState:UIControlStateNormal];
[self.mapView setMapType:MKMapTypeStandard];
sender.tag = 101;
}
}
在self.view
上添加按钮,不要忘记写
[self.view bringSubviewToFront:buttonName];
答案 4 :(得分:1)
-(IBAction)satelliteView:(UIButton*)sender
{
switch (sender.tag) {
case 0:
sender.tag=1;
[sender setImage:[UIImage imageNamed:@"standardImage.png"] forState:UIControlStateNormal];
break;
case 1:
sender.tag=0;
[sender setImage:[UIImage imageNamed:@"mapSateliite.png"] forState:UIControlStateNormal];
break;
default:
break;
}
}