Combining MKOverlays - uiview

I need to draw many (hundreds to thousands) of square overlays on a map. The positions and sizes of these overlays stay constant. I think that I can speed up rendering by combining these square overlays into a single overlay so drawMapRect only needs to be called once. Is this possible?

In my case, removing overlays is not an option. A way I found to drastically improve performance was to have my overlay class store an array of the square overlays. Then in my overlayView's drawMapRect method, I looped over the array to draw all the overlays. Something very similar is done in the HazardMap Apple Developer Example. See drawMapRect in HazardMapView.m

I had similar problem for MKAnnotation.
And I found following link:
http://www.fiveminutes.eu/having-fun-with-ios-map-kit-grouping-annotations/
Iterate annotation list and calculate a distance of two annotation's coordinate.
If there's nearby annotation exist, remove it from the list.
I think this approach can be applicable to MKOverlays too.

Related

Shadows in pin annotation

Why when I load my pin annotation in map, my pin annotations have different number shadow? Some have no shadows and some have many shadows.
I receive the data for pin from the firebase.
How to remove shadow or how to make a small shadow in all pin?
Check image
The darker shadow means you have multiple pins in the same location. There are two main causes for this and depending on the reason, there are two ways to solve this:
1. The data contains multiple elements with the same location.
Consider using clustering, which will display the number of annotations in the location. This will be a lot easier for the user to understand than a darker shadow. :)
Here's a tutorial from Apple: Decluttering a Map with MapKit Annotation Clustering
2. You forgot to erase the annotations before adding them again.
Remove the annotations before adding them to the map again, for example when you're refreshing the view.
mapView.removeAnnotations(annotationsToRemove) where annotationsToRemove is an array containing the specific annotations you need to remove.
Or, if you want to add all the annotations again, you can remove all the annotations with
mapView.removeAnnotations(mapView.annotations)

Interact with complex figure in iOS

I need to be able to interact with a representation of a cilinder that has many different parts in it. When the users taps over on of the small rectangles, I need to display a popover related to the specific piece (form).
The next image demonstrates a realistic 3d approach. But, I repeat, I need to solve the problem, the 3d is NOT required (would be really cool though). A representation that complies the functional needs will suffice.
The info about the parts to make the drawing comes from an API (size, position, etc)
I dont need it to be realistic really. The simplest aproximation would be to show a cilinder in a 2d representation, like a rectangle made out of interactable small rectangles.
So, as I mentioned, I think there are (as I see it) two opposite approaches: Realistic or Simplified
Is there a way to achieve a nice solution in the middle? What libraries, components, frameworks that I should look into?
My research has led me to SceneKit, but I still dont know if I will be able to interact with it. Interaction is a very important part as I need to display a popover when the user taps on any small rectangle over the cylinder.
Thanks
You don't need any special frameworks to achieve an interaction like this. This effect can be achieved with standard UIKit and UIView and a little trigonometry. You can actually draw exactly your example image using 2D math and drawing. My answer is not an exact formula but involves thinking about how the shapes are defined and break the problem down into manageable steps.
A cylinder can be defined by two offset circles representing the end pieces, connected at their radii. I will use an orthographic projection meaning the cylinder doesn't appear smaller as the depth extends into the background (but you could adapt to perspective if needed). You could draw this with CoreGraphics in a UIView drawRect.
A square slice represents an angle piece of the circle, offset by an amount smaller than the length of the cylinder, but in the same direction, as in the following diagram (sorry for imprecise drawing).
This square slice you are interested in is the area outlined in solid red, outside the radius of the first circle, and inside the radius of the imaginary second circle (which is just offset from the first circle by whatever length you want the slice).
To draw this area you simply need to draw a path of the outline of each arc and connect the endpoints.
To check if a touch is inside one of these square slices:
Check if the touch point is between angle a from the origin at a.
Check if the touch point is outside the radius of the inside circle.
Check if the touch point is inside the radius of the outside circle. (Note what this means if the circles are more than a radius apart.)
To find a point to display the popover you could average the end points on the slice or find the middle angle between the two edges and offset by half the distance.
Theoretically, doing this in Scene Kit with either SpriteKit or UIKit Popovers is ideal.
However Scene Kit (and Sprite Kit) seem to be in a state of flux wherein nobody from Apple is communicating with users about the raft of issues folks are currently having with both. From relatively stable and performant Sprite Kit in iOS 8.4 to a lot of lost performance in iOS 9 seems common. Scene Kit simply doesn't seem finished, and the documentation and community are both nearly non-existent as a result.
That being said... the theory is this:
Material IDs are what's used in traditional 3D apps to define areas of an object that have different materials. Somehow these Material IDs are called "elements" in SceneKit. I haven't been able to find much more about this.
It should be possible to detect the "element" that's underneath a touch on an object, and respond accordingly. You should even be able to change the state/nature of the material on that element to indicate it's the currently selected.
When wanting a smooth, well rounded cylinder as per your example, start with a cylinder that's made of only enough segments to describe/define the material IDs you need for your "rectangular" sections to be touched.
Later you can add a smoothing operation to the cylinder to make it round, and all the extra smoothing geometry in each quadrant of unique material ID should be responsive, regardless of how you add this extra detail to smooth the presentation of the cylinder.
Idea for the "Simplified" version:
if this representation is okey, you can use a UICollectionView.
Each cell can have a defined size thanks to
collectionView:layout:sizeForItemAtIndexPath:
Then each cell of the collection could be a small rectangle representing a
touchable part of the cylinder.
and using
collectionView:(UICollectionView *)collectionView
didSelectItemAtIndexPath:(NSIndexPath *)indexPath
To get the touch.
This will help you to display the popover at the right place:
CGRect rect = [collectionView layoutAttributesForItemAtIndexPath:indexPath].frame;
Finally, you can choose the appropriate popover (if the app has to work on iPhone) here:
https://www.cocoacontrols.com/search?q=popover
Not perfect, but i think this is efficient!
Yes, SceneKit.
When user perform a touch event, that mean you knew the 2D coordinate on screen, so your only decision is to popover a view or not, even a 3D model is not exist.
First, we can logically split the requirement into two pieces, determine the touching segment, showing right "color" in each segment.
I think the use of 3D model is to determine which piece of data to show in your case if I don't get you wrong. In that case, the SCNView's hit test method will do most of work for you. What you should do is to perform a hit test, take out the hit node and the hit's local 3D coordinate of this node, you can then calculate which segment is hit by this touch and do the decision.
Now how to draw the surface of the cylinder would be the only left question, right? There are various ways to do, for example simply paint each image you need and programmatically and attach it to the cylinder's material or have your image files on disk and use as material for the cylinder ...
I think the problem would be basically solved.

Drawing LOTS of MKPolylines efficiently. Is it possible?

I've an app whose function is to run in the background, collecting location data ('runkeeper' style app). It could potentially be running for hours, and collect thousands of points.
These 'runs' are listed in a tableview and on selection it will redraw that run on the map. I'm also coloring these polylines, so in order to have multiple colors on a seemingly single line, I connect a bunch of different polylines. When I go to add an NSArray containing (say 700) lines, and use
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
lineArray = [self polylinesFromSession];
dispatch_async(dispatch_get_main_queue(), ^{
[map addOverlays:lineArray] // lineArray.count = ~700
});
});
It really, really, bogs the app down for a 10-15 seconds. I can't use addOverlays on any thread other than main, so I don't see many options here. Is it possible to join a bunch of lines into a single overlay, THEN add it to the map? Or, any ideas for a better way to do this?
Thanks!
If your data points are contiguous, instead of adding hundreds of different lines, try to combine them into a single line with many points.
You might try creating raster tiles (with alpha transparency) of your lines on the fly and adding those as an MKTileOverlay to your map. For each point in a line, you can figure out in "tile space" what point this corresponds to, and use Core Graphics to draw in that tile. You can also skip points that would be over or under previous lines' points (unless you are plotting in a different color or want to layer the lines in a specific way).
The math is a little of the scope of an answer here, but Spherical Mercator is relatively easy to grasp as the world is a large square continuously tiled into smaller squares and the projection math is relatively straightforward trigonometry.
But you will likely find higher performance out of rasterizing this way, as long as you don't need to interact with the various line annotations individually in the app, but just show them.

Drawing lines in cocos2d

I'm trying to draw lines in Cocos2d using touches.
I had a system where it would just add a small sprite where you touched, but it's working terribly. So I've been trying to find a way to draw actual lines using a method like ccDrawLine, but every tutorial I find seems to leave out something, and I just can't figure it out.
I've found this tutorial, Drawing line on touches moved in COCOS2D but I don't understand a few things about that.
It seems to reference the same variable from two different files, so I don't understand how it's doing that. (The naughtyTouchArray variable)
I can't find a complete guide on drawing lines, so sorry for the codeless question, but I'm getting frustrated.
Thanks.
The answer you've linked in your question provides good solution to your problem. There is no "two different files". Just two different methods of one layer. One method (ccTouchesMoved:withEvent:) handles touches and fill the array of points to be connected to each other one-by-one with lines. From cocos2d documentation, all drawing must be placed in the draw method of the node. So, another (draw) method just draws lines according to the given array. Cocos2d is based on OpenGL and it fully redraws scene every tick, so you cannot just draw new line. You had to draw all of them.
Or any other node can draw your array in it's draw method, so you can simply pass stored array of points from the layer, that detects touches, to this node.

Animating sprites on top of mapview (iOS)

I am developing an application which will show where trains are at any given time. It will receive gpx-data from the trains to get their location.
Is there any way to animate a sprite moving along a restricted line in a mapview? In my example, a train moving along a trainroute.
I have drawn the trainroutes as polyline in an overlay, and the train is an annotation.
I ended up making a visible overlay, then making a separate array out of the same coordinates.
To simulate the movement, the MKAnnotations uses 'setCoordinate' while iterating the array, which in fact is the same as the overlay.
This made the annotations look like they moved atop of a restricted overlay.

Resources