Creating a view with an MKMapView and a touchable subview - iphone

Is it possible to create a view with both an MKMapview and another subview. In particular I have a map view and I have been trying to add a second subview (to the superview, not the MKMapView.) The problem is that while both view can be seen any touches to the UIView that is not the MKMapView pass through to the map. I even tried to move the map so that they do not overlap but the other UIView locates itself relative to the map, not the window.
An additional curiosity is that the code works fine on the simulator, but not on a test device that is running 3.1.3. Could this be a problem with the 3.1.3 version of MKMapView or just a quirk in the simulator?
My code is
combinedView = [[UIView alloc] initWithFrame:self.view.frame];
self.awView = [[AdWhirlView alloc] init];
awView = [AdWhirlView requestAdWhirlViewWithDelegate:self];
self.awView.delegate = self;
[combinedView addSubview:awView];
mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0,50,320,430)];
mapView = (MKMapView*)self.view;
mapView.delegate = self;
//[self.view insertSubview:mapView atIndex:0];
[combinedView addSubview:mapView];
//mapView.showsUserLocation = YES;
// Create a location coordinate object
CLLocationCoordinate2D coordinate;
coordinate.latitude = 40.1;
coordinate.longitude = -76.30575;
// Display map
mapView.region = MKCoordinateRegionMakeWithDistance(coordinate, 40000, 40000);
[combinedView bringSubviewToFront:awView];
Thanks for any advice.

Not too familiar with AdWhirlView, but is it possible you need to set a frame for that as well? CGRectMake(0,0,320,50); or something?
Otherwise, you might want to play with the autoresizeMasks of the two views. I know for certain you can have a MKMapView with sibling views, and they all work fine.

Related

GMSMapView hiding navigation bar

I implemented the GMSMapView via the Googla Maps iOS SDK
the example code from Google suggests to declare the view more or less just dropping this method in your code
- (void)loadView {
// Create a GMSCameraPosition that tells the map to display the
// coordinate -33.86,151.20 at zoom level 6.
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
longitude:151.20
zoom:6];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView_.myLocationEnabled = YES;
self.view = mapView_;
// Creates a marker in the center of the map.
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(-33.86, 151.20);
marker.title = #"Sydney";
marker.snippet = #"Australia";
marker.map = mapView_;
}
it automagically works, but the mapView happens to cover my navigationItem
it's clear that the maps take its dimension at the initWithFram:CGRectZero
but simply changing the parameter to a custom CGRect
CGRect square = CGRectMake(100, 100, 100, 100);
haven't worked for me, any other suggestion?
i only need to display the map between the Nav Item and the Tab Bar (but the second doesn't happen to be covered)
The problem was that the mapView_ was being set as the whole view
self.view = mapView_;
calculating the correct dimension and adding it as a subview was the correct way to solve it
CGRect f = self.view.frame;
CGRect mapFrame = CGRectMake(f.origin.x, 44, f.size.width, f.size.height);
mapView_ = [GMSMapView mapWithFrame:mapFrame camera:camera];
[self.view addSubview:mapView_];
Make sure your view controller for the map is part of a navigation stack of a UINavigationController. I had no problems pushing a UITabBarController with a map and a list tab onto a view controller embedded in a UINavigationController. This way the navigationbar view belongs only to the UINavigationController and the map controller view shouldn't cover it.

How to set the frame for mapview - in google GMSMap for iOS6

I am trying to load places inside Google Map View for iOS6.
How to set the frame for the Map ?
Currently it is fullscreen
-(void)loadView {
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:10.00989
longitude:76.316142
zoom:15];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
self.view = mapView_;
}
I tried by creating a new (small) view inside the current view and add map inside that,but at that time the page is not getting loaded.It shows a full black screen
-(void)loadView {
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:10.00989
longitude:76.316142
zoom:15];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
[self.view Addsubview:newView];
self.newView = mapView_;
// I also tried like this - [self.newView Addsubview:mapView_;
}
You can try... what is working for me :)
//header file
...
#property (nonatomic, weak) IBOutlet GMSMapView *mapView;
#property (weak, nonatomic) IBOutlet UIView *subview; //viewForMap
...
implementation file
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:10.8683
longitude:76.2086 zoom:6
bearing:0
viewingAngle:0
];
self.mapView = [GMSMapView mapWithFrame:_subview.bounds camera:camera];
[_subview addSubview:_mapView];
It's hard to say if your problem here is adding the map view or something upstream.
Have you set a breakpoint in -viewDidLoad to ensure that it gets called?
Check the bounds of newView to make sure it's what you expect. Is newView visible? Is it a subview of self.view ?
One trick you can use when trying to ensure your views are where you expect them is to set the background color to something obvious, like red, and then you can see plainly on screen if it's what you expect. If you do this and don't see a red box, then your problem isn't with maps, it's somewhere upstream in the code you haven't shown us.
Good luck.
Create a CGRect with the required frame dimensions and set the MapView frame with method mapWithFrame: and then add the mapview as main subview. Below is the code which explains all. CGRect fr= CGRectMake(0, 44, 768, 960); mapView_ = [GMSMapView mapWithFrame:fr camera:camera]; mapView_.myLocationEnabled = YES; [self.view addSubview:mapView_];
I don't think loadView is called when you are using a xib. I haven't tried using the Google Maps SDK for iOS with a xib, as I create all of my views programmatically.
Maybe you could add your map view in viewDidLoad? So for example:
- (void)viewDidLoad
{
[super viewDidLoad];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:10.00989
longitude:76.316142
zoom:15];
mapView_ = [GMSMapView
mapWithFrame: CGRectMake(
0, 0,
self.newView.frame.size.width, self.newView.frame.size.height)
camera: camera];
[self.newView addSubview: mapView_];
}
You need to do like this
-(void)loadView {
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:10.00989
longitude:76.316142
zoom:15];
mapView_ = [GMSMapView mapWithFrame:self.newView.bounds camera:camera];
[self.view addSubview:newView];
[self.newView addSubview:mapView_];
}

iOS view controller two-stage rotation animation crash

I'm facing a problem that is driving me crazy. I read about it around but I didn't find a solution. I'm developing an application that presents modally the view controllers depending on the device orientation. When the device orientation is face up a TabBarController is presented modally. The TabBarController contains two viewControllers one to show a map and the other one (not yet implemented) is for other purposes. When I change the device orientation the application freezes showing the following messages:
MobileAR[8642:707] Using two-stage rotation animation. To use the
smoother single-stage animation, this application must remove
two-stage method implementations.
2011-12-07 21:47:37.566 MobileAR[8642:707] Using two-stage rotation
animation is not supported when rotating more than one view controller
or view controllers not the window delegate The problem that occurs
is that when the loadView: method of the map view controller
There are no problems with the modal presentation of the controller everything works fine as long as the mapviewcontroller loadView: do something else. Here is the code in order to have a better understanding:
If the loadView: is in this way:
- (void)loadView{
UIView *faceUpView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 718)];
self.view = faceUpView;
[faceUpView release];
MKMapView *mkMap = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 1024, 718)];
mkMap.mapType = MKMapTypeStandard;
[self.view addSubview:mkMap];
[mkMap release];
}
doesn't generate any problems, the view, thereby the map is properly visualized within the TabBarController.
Instead if in the loadView: I initialize a view like this:
- (void)loadView{
//Initialize the MapView
MapView *mv = [[MapView alloc] initWithFrame:CGRectMake(0, 0, 1024, 718)];
mv.mapViewController = self;
self.mMapView = mv;
//Release of the local variable
[mv release];
}
it freezes as soon as I put the device "Face Up",even if I leave just the first line: MapView *mv = [[MapView alloc] initWithFrame:CGRectMake(0, 0, 1024, 718)];
Here the implementation of its initializer, MapView.m
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.layer.borderWidth = 2; //Set the border of the box
//State that the subviews are confined within its bounds
self.clipsToBounds = YES;
//Initialize the View that contains the map with position and size
// MKMapView *mv = [[MKMapView alloc] initWithFrame:CGRectMake(-106, -106, BOX_SIDE, BOX_SIDE)];
MKMapView *mv = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, BOX_WIDTH, BOX_HEIGHT)];
//Set the map as standard map (Not satellite nor Hybrid)
mv.mapType = MKMapTypeStandard;
//Retains the map
self.mapView = mv;
//Add the map to the super view
[self addSubview:mv];
//Release the map view previously allocated
[mv release];
}
return self;
}
I didn't ovverride any of the two-stages rotation method neither the one-stage as I read in some answers.
I really appreciate any help!!!!
Thank you

Adding adWhirl view with a MKMapView

I am trying to implement an adWhirl view in the same view as one that has an MKMapView object. If I follow the standard steps that work fine with my tableViews, that is
awView = [AdWhirlView requestAdWhirlViewWithDelegate:self];
[self.view addSubview:awView];
then the funny behavior starts. In the simulator it receives the touch and sends you on. But when running on my test device (running 3.1.3) the touch passes through to the map.
I have been told that this is because awView is being cast as a subView of mapView and that it must be its own view. But how? I have tried creating a separate UIView and then placing awView in it, then locating it at a fixed location, but instead of the fixed location, it loads relative to mapView and still does not receive touches.
Any suggestions?
Addendum: I thought that I was adding both as subviews but have not had any success. What I had done is to create two views in IB. The top one (firstView) has two subviews (bannerForAd and mapView.) This is what I have now
self.awView = [AdWhirlView requestAdWhirlViewWithDelegate:self];
self.awView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
[bannerForAd addSubview:awView];
mapView = [[MKMapView alloc] initWithFrame:self.view.frame];
mapView = (MKMapView*)self.view;
mapView.delegate = self;
[firstView addSubview:mapView];
[firstView bringSubviewToFront:bannerForAd];
Now the map shows fine, but the ad is not even visible.
Stick both the AdWhirlView and the MKMapView as a subview of a UIView.

My view is displaying y=-20 dispite its frame set to y=0. After rotation it snaps back to y=0

I started by creating a universal window based app. Starting with the iPhone version I created a UIViewController and associated nib.
My App delegate:
rootViewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
[window makeKeyAndVisible];
[window addSubview:rootViewController.view];
return YES;
My RootViewController:
- (void)viewDidLoad {
[super viewDidLoad];
adBannerView = [[ADBannerView alloc] initWithFrame:CGRectZero()];
[self.view addSubview:adBannerView];
}
I've tried instanciating buttons instead of the adBanner and I get the same result.
My RootViewController's nib has not been changed since x-code created it for me.
My MainWindow_iPhone.xib also is stock.
What's causing this?
Update
After changing the app's orientation the adBannerView (or button...) will snap into the correct place at y=0. I've tried setting adBannerView's y location to 20 presumably to compensate for the status bar and that makes everything display correctly until I change orientation. Then everything moves down 20 pixels and will leave a 20 pixel space between the adBannerView and the status bar.
Try to add the next line in your viewDidLoad (right after [super viewDidLoad];):
self.view.frame = [[UIScreen mainScreen] applicationFrame];
CGRectZero is literally a zero rect (0, 0, 0, 0), so ADBannerView should never show up if it really has a width and height of 0. You probably want to try initWithFrame:self.view.frame or so…
You should set the size identifier before adding the view:
- (void)viewDidLoad {
[super viewDidLoad];
adBannerView = [[ADBannerView alloc] initWithFrame:CGRectZero()];
if(UIInterfaceOrientationIsPortrait([[UIDevice currentDevice] orientation]))
adBannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50;
else
adBannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier480x32;
[self.view addSubview:adBannerView];
// now you can treat it like any other subview
// For example, if you want to move it to the bottom of the view, do this:
CGRect frame = adBannerView.frame;
frame.origin.y = self.view.frame.size.height - frame.size.height;
[adBannerView setFrame:frame];
}
Whenever the interface rotates, you should notify the banner to change its size.
Assuming you have access to WWDC videos (which is available for free), check video session 305. It demos adding the banner.