Swift PDFKit add annotation to beginning of page annotation array? - swift

In Swift PDFKit is there a way to add an annotation to to the beginning of the page annotations array. I want to add my annotation to the beginning of the array because it is an image and I have other annotations I want to be able to edit that are ontop of it.
The only way I can find to add an annotation is with the following, but it adds it to the end of the array overlaying all other annotations that need to be editable.
page.addAnnotation(myannotation)
Thanks for any help!!

Here is a terrible solution, but it works . . .
I made a tmp list of all the annotations, remove all annotations on the page, and then added them all back with the image added first.
let list = page.annotations
for i in page.annotations{
page.removeAnnotation(i)
}
// add myannotation first
page.addAnnotation(myannotation)
// add other annotations back
for i in list {
page.addAnnotation(i)
}

Related

How to access callout view on clicking on map annotation using swift in xctest

I want to access callout views and do some UIAutomation on those views. I'm able to click on map markers/annotations but not able to access the callout view.
The following code used to tap on the marker:
let marker = app.otherElements.matching(identifier: "mapMarker").element(boundby: 0)
marker.tap();
After this, I'm getting the callout view of the respected marker/annotation.
I need to access that callout.
Please suggest me on this.
You should create a breakpoint after the callout is snown,
then type po print(app.debugDescription) (or simply po app in XCode 11) in lldb in order to view the whole hierarchy of UI elements.
Locate the needed element and access it further in code.
Also, consider rewriting your marker code in a shorter way:
let marker = app.otherElements["mapMarker"].firstMatch
Please notice firstMatch aborts search of elements after it found the first one.
Drop firstMatch, if you want to check that the element is unique
let marker = app.otherElements["mapMarker"]
Same as Smart Monkey said, but to add more code based off the comment from ablarg:
Ex: "mapMarker" being the accessibility ID for the element
let mapMarker = app.maps.otherElements["mapMarker"].firstMatch
let mapMarkerExists = mapMarker.waitForExistence(timeout: 3)
if mapMarkerExists {
mapMarker.tap()
}
waitForExistence(timeout:) returns a bool, so if the element appears before the timeout expires (it finds the element) take action (tap) on the element.
Make sure the element is enabled for accessibility and has the accessibility ID set.

Is it possible to make MGLPolyLines selectable? - Swift, MapBox

Is it possible to make an MGLPolyLine touchable/selectable/have user interaction? In my project, the user needs to touch the polyline. There was this question asked before but it is outdated by about 2 years. Have they (MapBox) updated this?
I've just checked back and it looks like this has been implemented though I'm not sure which Mapbox release rolled this out.
If you take a look at the simple Mapbox example, Annotation Models, that demos an MGLPolyline and interspaced circular annotations, you can make a simple mod to the supplied code and see for yourself. The demo looks like this:
If you look into the viewController code, add a couple of lines below the polyline creation:
let polyline = CustomPolyline(coordinates: &coordinates, count: UInt(coordinates.count))
polyline.title = "Polyline" // New line
polyline.subtitle = "Pretty Poly". // New line
// Set the custom `color` property, later used in the `mapView:strokeColorForShapeAnnotation:` delegate method.
polyline.color = .darkGray
Now you can tap and see a basic callout:
This example subclasses MGLPolyline (CustomPolyline) so that its appearance can be altered slightly but that doesn't change anything with regards to the tappability.

iOS Moving annotations on MapBox map

Is it possible to move an annotation without removing and adding a new annotation?
I'd like to stick with MapBox because the future support of offline maps.
Thank you in advance!!
As of Mapbox iOS SDK v3.2.0, it is not possible to update the coordinates of an annotation that has been added to a map. Here is the relevant ticket on Github.
You can do a custom development for moving marker. I have done it and it's work as expected.
If you want to move a marker along your polyline, I would start by holding a reference to your MGLPointAnnotation and update its coordinate property on a timer or as the user moves along with it.
You can do this by keeping a reference to the annotation instance and then updating the coordinates of that instance.
If I assume you're adding your map (MGLMapView) to a View Controller, then a basic approach could be to add a property to that view controller so that you can keep track of the reference. For this example, I'll use an MGLPointAnnotation:
class ViewControllerWithAMap: UIViewController {
var movingPointAnnotation: MGLPointAnnotation?
...
}
Make this reference an optional so that you know when to initiate it and then just update the coordinates. The last part may seem counterintuitive, but all you really need to do now is add the annotation to the map again. For example:
if self.movingPointAnnotation == nil {
self.movingPointAnnotation = MGLPointAnnotation()
}
guard self.movingPointAnnotation != nil else {
print("What?! Could not initiate moving annotation.")
return // something weird, give up
}
self.movingPointAnnotation!.coordinate = myNewCLLocationCoordinate2D
self.mapView.addAnnotation(self.movingPointAnnotation!)
Regarding that last part where you add the annotation over and over again, I tried only adding the annotation once, and then just updating its coordinates afterward, but the annotation did not move.

multiple annotation have issue to get selected index annotation

I have displayed multiple annotation successfully in mapView in my iphone application, but I have problem too .. In top of the screen I have two Tab Map and List . map display all annotation in map and list display those data in Tableview which is display in map. when I click on particular cell i get all the particular detail of that cell.but when I try to get id from Tap annotation my array getting lots of id after comparison of name because we have same name in my array list so how can I differentiate from annotation tag. How to set tag of annotations ?
In the calloutAccessoryControlTapped delegate method, use view.annotation to access the annotation that was tapped.
If you have a custom annotation class, you can cast it to easily access the properties (you may also want to first check if the annotation tapped is an instance of the class you're interested in--important if you're using multiple annotation classes):
if ([view.annotation isKindOfClass:[TagMark class]]) {
TagMark *tm = (TagMark *)view.annotation;
NSLog(#"tm.someProperty = %#", tm.someProperty);
}

Drop annotation on map

Can any one
My trying different-different View for droping annotation on the map But not getting success.This is my last try .
Can any one help me out from this condition .
How can I Show annotation on the map .
By retrieving the annotation data(long,latit) from Sqlite data base .
I have 10 to 15 data in Sqlite table .I To want display annotation to map on this data(long,latit).
My data is like that
42.338326,-71.131411
42.338347,-71.131647
42.33838,-71.131861
42.338551,-71.132934
42.338573,-71.13302
42.338594,-71.133213
42.338626,-71.133406
42.338809,-71.134393
You need to do few more things like, 1) Adding annotations to map view and 2) Implementing viewForAnnotation: method. Follow this tutorial.
I was able to set up my map view by following this tutorial: http://www.edumobile.org/iphone/iphone-programming-tutorials/mapkit-example-in-iphone/
I think it will help you a lot.