iOS MapKit - drawing shapes on map - ios

I'd like to allow users to mark some area on map. This area should be any closed shape. For instance user can draw something like this:
In next step I'd like to calculate region of this shape. How can I achive this?

You can use MKPolygon.
First of all you'll need to disable user interaction on the map view so that it won't move around while you're trying to draw on it. Next up you can use the UIResponder functions touchesBegan, touchesMoved and touchesEnded. As you move through these three functions you can record the points the user has pressed. Lastly you can then create an MKPolygon from this array of points that you have recorded.

Related

How to split an image into polygons

Similar to some coloring apps that fill a certain region with selected color when tapped, I would like to convert a png image to polygons which could be tapped in order to perform a certain action. An example picture is posted below.
For this example, I would like to implement the logic to divide the image into regions 1, 2, 3, and 4 (not necessarily in this order) so when the user taps on the upper-left rectangle, action1, for upper-right rectangle action2, for the ellipse actions, and for the rest action3 is executed.
Does anyone know how to do it by using SpriteKit?
You don't need to split the image into regions. Attach a tap gesture recognizer to your image view.
In the action of the tap gesture recognizer, take the coordinates of the tap and figure out which region it falls into.
Rectangular regions are really easy. You just see if the coordinates fall within the x/y bounds of the rectangle.
For more complex shapes, you can create UIBezierPath shapes and use the UIBezierPath contains(_:) method to see if the tap point falls in a particular path.
The simplest way to structure your code would be an array of structs each of which contains a UIBezierPath and a closure to invoke if a tap lands in that path. You can then invoke the closure when a tap lands in one of those paths.

Increase tap area of GMSPolyline

,In our app, users can create GMS- features on a map. Those features are then editable, with the editing process starting from a tap. In some cases, if the style applied to a polyline is a 1 or 2 point line, they are difficult for a user to tap. I have researched this and found nothing on how to increase the tap area.
I would like to do something like adding a buffer around the line, such as in this example, but the buffer would not be visible to the user as it would only provide an increased tap area:
Does anyone know if this is possible? Any good resource on how to do this? Thanks!
If you want to know if the user tapped a location that is within a certain distance from a GMSPath you can use GMSGeometryIsLocationOnPathTolerance .
Using this combined with the mapView:didTapAtCoordinate GMSMapViewDelegate method you're all set.

Get array of CGPoint while user wants to draw a line

I am using google map for my ios app, what i want to achieve is like when user touches the map and drags the finger, then identify the coordinates and draw the line on the map and then want to get all the marker inside that polyline(circular). So for that i had to disable the movement of the map which i did as below:
_googleMapView.settings.scrollGestures = NO;
_googleMapView.settings.zoomGestures = NO;
But then now i want to draw a line for which i got this link as below:
Free hand drawing in google maps-iOS
This link says that i have to do as follows:
To draw a line use polyline. Refer to Google Maps Shapes.
To use polyline you need to give locatoin coordinates. To convert a point on screen to coordinate use coordinateForPoint:(CGPoint)point method of GMSProjection class.
But my problem is i want to get the CGPloint of array when user drags and the map(which inturn will make a polyline), but i am really unaware of how will i get the CGPoints.
https://github.com/saru2020/SARMapDrawView
This project did exactly what i wanted, helpful link.

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.

Show circle around selected location

As we have seen there is a circle around current location in iOS mkmapview. Is there any way to draw a similar circle in mkmapview for location other then current location?
If you look at the documentations for MKCircle: http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKCircle_class/Reference/Reference.html and MKCircleView: http://developer.apple.com/library/iOS/documentation/MapKit/Reference/MKCircleView_class/Reference/Reference.html both of which are part of the MapKit API,
you'll find tools for drawing circles!
As mentioned at this link Circle around a point on an MKMapView
there are no easy way to create animations for these circles, however the advantage is that they will provide you with a degree of accuracy for your coordinates on your map!
The steps to creating the circles, one way, is here: Drawing a circle on mapkit
Basically, create an MKCircle object using circleWithCenterCoordinate:radius: method and add it to your mapView (using addOverlay: method)
Next, in the mapView delegate, implement mapView:viewForOverlay: method, create and setup MKCircleView instance there and return it.

Resources