質問

In my app, I am having a mapView and a button on it, the button is used to toggle the mapView between a standard mapView and the satellite view.

enter image description here
Here is the snap shot, if I press the satellite button, the mapView should get changed to sattelite view and the button image should get changed to the following image,
enter image description here
And now wen the button is pressed it should get changed to a normal 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];
役に立ちましたか?

解決

Simple :

Try using the tag for the UIButton . By default assign tag as for standard mapView For 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"] 
        }

}

他のヒント

Since you are using a single button to toggle between two types of maps, you have to keep track of the button's state. Lets say your button is by default showing a normal Map view.

@property(nonatomic, assign) BOOL isSatelliteMap;

Make it FALSE when the view loads for the first time as isSatelliteMap = NO;, This means that you will be loading MKMapTypeStandard for the first time & button will show SatelliteImage.

Now when the button is pressed:

-(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;
    }
}    

This thing you can apply to any type of Map. I mean you can do same thing for google Maps. Hope this helps.

you can put below code in touch up inside method for change button image

-(void)settalite:(id)sender{

    UIbutton *btn=(UIButton *)sender;
    [btn setBackgroundImage:[UIImage imageNamed:@"map.png"] forState:UIControlStateNormal];

}

I just give you suggestion, Add your MKMapView in button click method and also give tag of button such like. (In your case Change GoolgeMap)

myButton.tag = 101;

And following is button method.

-(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; 
   }
}

And add button on self.view and don't forget to write

[self.view bringSubviewToFront:buttonName];
-(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;
    }

}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top