iPhone MKMapView: set span/region value to show all pins on map - objective-c

I'm working on a project (platform iOS 7) in which i required current location with stores around 5km, so how to calculate the span/region value to display all stores with current location on map.
MKMapRect zoomRect = MKMapRectNull;
double inset;
for (id <MKAnnotation> annotation in mapVW.annotations)
{
MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.1, 0.1);
zoomRect = MKMapRectUnion(zoomRect, pointRect);
inset = -zoomRect.size.width * 20;
}
[mapVW setVisibleMapRect:MKMapRectInset(zoomRect, inset, inset) animated:YES];
this is what I'm trying
Thanks

It's not clear what your exact issue is but the following may help:
The calculation of the inset looks wrong. It's setting the inset (padding on the sides) to 20 times the width of the whole zoom area. What you probably want is to set the inset to a small fraction of the entire width. Maybe you meant 0.20 instead of 20.0:
inset = -zoomRect.size.width * 0.20;
You also don't need to repeatedly set the inset inside the for loop since it only depends on the final width. You can move the above line after the for loop before calling setVisibleMapRect.
You mention some issue with the current location. It's not clear what the issue is but maybe you mean that this zooming code doesn't include the current location? If so, maybe the current location hasn't been determined yet when this code is called. Try moving this code to (or also call it from) the didUpdateUserLocation delegate method. Make sure showsUserLocation is YES and that the map view's delegate is set.
By the way: iOS 7 includes the new method showAnnotations:animated: which automatically determines the bounding rectangle for some given annotations and sets the map's visible region for you. It doesn't let you specify a custom inset like you are doing (though the default isn't bad). So instead of the above loop, you would do:
[mapVW showAnnotations:mapVW.annotations animated:YES];

NSArray *anno_Arrr = mapview.annotations;
[mapview showAnnotations:anno_Arrr animated:YES];

Related

Change height of a NSProgressIndicator programmatically

I cannot find a way to set the height of a NSProgressIndicator programmatically.
My try so far:
NSProgressIndicator *ind = [[NSProgressIndicator alloc] init];
[ind setStyle: NSProgressIndicatorBarStyle];
// Height does not change height of the actual indicator
[ind setFrame: NSMakeRect(0, 0, 100, 50)];
[ind setBounds: NSMakeRect(0, 0, 100, 50)];
//[ind setControlSize: 0]; does only make it smaller, not bigger
[view addSubview: ind];
I found NSProgressIndicatorBarStyle enumeration in the documentation, but I couldn't find a method to specify the thickness.
Here a screenshot describing my problem: (layer has a background of red for better understanding):
This also occurs when using the NSButton class. Is there a workaround for this?
in iOS You can't change the progress indicator height just changing its frame, due to framework restrictions. However you should be able to achieve the same result, playing with transform
_indicator.transform = CGAffineTransformMakeScale(1.0f, 0.6f);
EDIT: I just tried on Mac OS X
_indicator.layer.transform = CATransform3DMakeScale(1.0f, 0.6f, 0.0f);
and it doesn't work, so it is not like iOS, likely because of how it is implemented on Cocoa (like Ken suggested).
The only way I managed to change the height is using controlSize, but I don't think it will suit your needs (since it doesn't allow you to specify points).
[_indicator setControlSize:NSMiniControlSize]; // or NSSmallControlSize
You should be able to use an arbitrary frame by subclassing NSProgressIndicator, and overriding drawRect, at this point my recommendation would be to look around to find something that can be extended for your use, like this one
https://www.cocoacontrols.com/controls/lbprogressbar
in IB you can
select your NSProgressIndicator control
in the utilities view select the View Effects inspector
press + in Content Filters
select Lanczos Scale Transform filter
set the appropriate scale value in the Scale row
set the Aspect Ratio too if you need to change the height only
this can be added programmatically also, just google for it how to add Content Filters to NSView

How can I ensure I still get correct touch inputs when my scene is offset?

How can I accept touch input beyond the scene's bounds, so that no matter what I set self.position to, touches can still be detected?
I'm creating a tile based game from Ray Winderlich on Cocos2d version 3.0. I am at the point of setting the view of the screen to a zoomed in state on my tile map. I have successfully been able to do that although now my touches are not responding since I'm out of the coordinate space the touches used to work on.
This method is called to set the zoomed view to the player's position:
-(void)setViewPointCenter:(CGPoint)position{
CGSize winSize = [CCDirector sharedDirector].viewSizeInPixels;
int x = MAX(position.x, winSize.width/2);
int y = MAX(position.y, winSize.height/2);
x = MIN(x, (_tileMap.mapSize.width * _tileMap.tileSize.width) - winSize.width / 2);
y = MIN(y, (_tileMap.mapSize.height * _tileMap.tileSize.height) - winSize.height / 2);
CGPoint actualPosition = ccp(x, y);
CGPoint centerOfView = ccp(winSize.width/2, winSize.height/2);
NSLog(#"centerOfView%#", NSStringFromCGPoint(centerOfView));
CGPoint viewPoint = ccpSub(centerOfView, actualPosition);
NSLog(#"viewPoint%#", NSStringFromCGPoint(viewPoint));
//This changes the position of the helloworld layer/scene so that
//we can see the portion of the tilemap we're interested in.
//That however makes my touchbegan method stop firing
self.position = viewPoint;
}
This is what the NSLog prints from the method:
2014-01-30 07:05:08.725 TestingTouch[593:60b] centerOfView{512, 384}
2014-01-30 07:05:08.727 TestingTouch[593:60b] viewPoint{0, -832}
As you can see the y coordinate is -800. If i comment out the line self.position = viewPoint then the self.position reads {0, 0} and touches are detectable again but then we don't have a zoomed view on the character. Instead it shows the view on the bottom left of the map.
Here's a video demonstration.
How can I fix this?
Update 1
Here is the github page to my repository.
Update 2
Mark has been able to come up with a temporary solution so far by setting the hitAreaExpansion to a large number like so:
self.hitAreaExpansion = 10000000.0f;
This will cause touches to respond again all over! However, if there is a solution that would not require me to set the property with an absolute number then that would be great!
-edit 3-(tldr version):
setting the contentsize of the scene/layer to the size of the tilemap solves this issue:
[self setContentSize: self.tileMap.contentSize];
original replies below:
You would take the touch coordinate and subtract the layer position.
Generally something like:
touchLocation = ccpSub(touchLocation, self.position);
if you were to scale the layer, you would also need appropriate translation for that as well.
-edit 1-:
So, I had a chance to take another look, and it looks like my 'ridiculous' number was not ridiculous enough, or I had made another change. Anyway, if you simply add
self.hitAreaExpansion = 10000000.0f; // I'll let you find a more reasonable number
the touches will now get registered.
As for the underlying issue, I believe it to be one of content scale that is not set correctly, but again, I'll now leave that to you. I did however find out that when looking through some of the tilemap class, that tilesize is said to be in pixels, not points, which I guess is somehow related to this.
-edit 2-:
It bugged me with the sub-optimal answer, so I looked a little further. Forgive me, I hadn't looked at v3 until I saw this question. :p
after inspecting the base class and observing the scene/layer's value of:
- (BOOL)hitTestWithWorldPos:(CGPoint)pos;
it became obvious that the content size of the scene/layer was being set to the current view size, which in the case of an iPad is (1024, 768)
The position of the layer after the setViewPointCenter call is fully above the initial view's position, hence, the touch was being suppressed. by setting the layer/scene contentSize to the size of the tilemap, the touchable area is now expanded over the entire map, which allows the node to process the touch.

Setting UITableView ContentInset

I am changing content insets bottom value manually.
literatureTableView.contentInset = UIEdgeInsetsMake(0, 0, 100, 0);
This works fine but i want to set the value depending on the height of the table contents. I tried some things with tableView.bounds.size.height etc. but nothing is working.
If you need to know the calculated bottom inset prior to displaying the reloaded data, then you must calculate the heights manually.
However, if your app permits you to call reloadData and then adjust the inset, you can arrive at it much more simply by doing this:
CGFloat trueContentHeight;
CGFloat heightWithInsets = self.tableView.contentSize.height;
UIEdgeInsets insets = self.tableView.contentInset;
trueContentHeight = heightWithInsets - (insets.top + insets.bottom);
Then you can calculate the bottom inset with the trueContentHeight and the height of the tableView's bounds.
youll need to add up the height of your cells and headers (if you have any). You can use
tableView:heightForRowAtIndexPath: from self to get the height's manually and add them up if they are not all the same.

How to get the padding from the edge of the UITableview to the UITableViewCell

On the iPad, the Grouped style tableview's cells are inset deeper from the edge of the tableview than on the iPhone.
I need to retrieve the Left and Right distances from the edges of the tableview to where the cell begins. What i'm referring to is similar to "Margins". I read the UITableview API up and down and can't find a property that returns this.
I need to use this in calculation to compute where to position content in my cells.
Thanks in advance!
Alex
I haven't tested this but i'm pretty sure you should just be able to pick up the frame of both and then compare from there.
CGRect cellFrame = yourCell.frame;
CGRect tableFrame = yourUITableView.frame;
The CGRect values are (x coordinate, y coordinate, width, height).
Also you can just print out the frames using :
NSLog(#"your cell frame is %#",NSStringFromCGRect(yourCell.frame);
NSLog(#"your table frame is %#",NSStringFromCGRect(yourUITableView.frame);
I solved this with overriding the layoutSubviews call for the iPad and setting the grouped view margins to what I want them to be, rather then what the apparently hidden value is. Other answers in Stack point out that it can vary from 10 to 45 pixels in width.
In your CustomTableViewCell class
- (void)layoutSubviews
{
CGRect f = self.bounds;
f = CGRectInset(f, 10, 0);
self.contentView.frame = f;
self.backgroundView.frame = f;
}
You could force it to keep contentView and backgroundView to be equal to that of the TableCell width which is that TableView width, but in this case I still wanted my grouped view to be inset a little bit. It also allows you to better match with a custom header/footer view which will go edge to edge without work.

How do I convert Cocoa co-ords from top left == origin to bottom left == origin

I use CGWindowListCopyWindowInfo to get a list of all windows. It gives me the co-ordinates of each window based upon the origin being the top-left of the screen.
If I use NSWindow's setFrame method, the co-ordinates on based upon the origin being the bottom-left of the screen.
What's a clean, reliable way to convert from one to the other?
Added: By clean and reliable, I mean, something sure to work regardless whether the user has multiple screens or is using Spaces. I figure there must be a known idiom using library APIs.
Math is quite reliable :-)
yFromBottom = screenHeight - windowHeight - yFromTop
Main screen height is
[[[NSScreen screens] objectAtIndex:0] frame].size.height
I would suggest using an NSAffineTransform. If you draw with respect to the default origin and then apply a transform to the view, you can essentially flip things around in one fell swoop.
Try something like this (from here):
NSRect boundsInWindow = [myView convertRect:[myView bounds] toView:nil];
NSRect visibleRectInWindow = [myView convertRect:[myView visibleRect] toView:nil];
// Flip Y to convert NSWindow coordinates to top-left-based window coordinates.
float borderViewHeight = [[myView window] frame].size.height;
boundsInWindow.origin.y = borderViewHeight - NSMaxY(boundsInWindow);
visibleRectInWindow.origin.y = borderViewHeight - NSMaxY(visibleRectInWindow);