Create a textfield over MKAnnotation Callout Swift - ios

I'd like to make an editable callout, and from a quick look it seems like the best way is to create a textfield over it in the view.
So, I was wondering how to get the CGPoint of an annotation so that I can use it in placing my CGRect of a textfield. This is the code I'm using to display an annotation at a tapped point, how would one do the reverse?
If anyone suggests better ways to create an editable callout, I'm welcome to suggestions
var point:CGPoint = gestureRecognizer.locationInView(self.Map)
EventLoc = self.Map.convertPoint(point, toCoordinateFromView: self.Map)

I'm not too familiar with Swift yet, but independent from the language, MKMapView has a method convertCoordinate:toPointToView: which you can use to convert a CLLocationCoordinate2D to a CGPoint.
EDIT
From the MKMapView reference:
Converts a map coordinate to a point in the specified view.
So in your case the last argument should be your map view:
The view in whose coordinate system you want to locate the specified map coordinate. If this parameter is nil, the returned point is specified in the window’s coordinate system. If view is not nil, it must belong to the same window as the map view.

Related

Position update in SwiftUI with MKMapKit

I'm using for a way to update an annotation programmatically. I'm playing with SwiftUI and UIViewRepresentable. The map is shown. It is also not a problem to display an annotation on the map.
What I need now is this: I'm having a location provider, which delivers CLLocation2D coordinates. I would like to display the changing position on a map, but can't figure out how.
The location update is provided via an ObservableObject. That works. If I display the updates in Text element, I can see them:
#ObservedObject var pushedPoseClient = PushedPosesClient()
...
Text(String(format: "%.10f %.10f", self.pushedPoseClient.position.latitude ?? 0, self.pushedPoseClient.position.longitude ?? 0))
The same mechanism doesn't work for a map. I thought I could see updateUIView calls with every position update, but there are no.
How can an ObservedObject, which changes its coordinates, be displayed on a map?
The key is to have a CLLocation2D as observed object. Then it works.

Moving Map Under the Marker in Mapbox iOS SDK

I am new in MapBox iOS SDK and I need to add a marker in the center of MGLMapView so that user would be able to move map view under the marker and the marker would be fix on the screen. I also need to get the coordinate of the point in the map that is under the marker. I could not find any method in Mapbox SDK and I have no idea how to do that.
I believe this is quite easy. Add an image of a marker on top of the map to give the visual effect so the user can still scroll around without moving the marker. Then you can get the center coordinates usually quite easily using mapView.centerCoordinate when the user stops scrolling.
Here is the API documentation link for reference
I had done something like this in one of my apps
1) Add map to UIViewController
2) Add a Transparent view on top of the map in UIViewController. (May need to set userInteractionEnabled to false. Not sure though!)
3) Add marker image to the Transparent view so that its bottom tip is at the center of the view it is added to.
4) Get center coordinates by using mapView.centerCoordinate
For other people who have this issue too:
let markerImageView = UIImageView(image: UIImage(named: "Orange"))
markerImageView.center = mapView.center
mapView.addSubview(markerImageView)

How do I retrieve the coordinates used to create an iOS MapKit MKPolygonView?

I am using apple mapkit to create 3 mkpolygonviews on a map using an nsarray.
What i need is to have a tap gesture recognizer to get the coordinates from each polygon.
Is this possible? if so, some sample code would be appreciated.
You have reference to the MKPolygon objects (either your own reference or the overlays collection of the MKMapView object). And MKPolygon is MKMultiPoint subclass, which provides points, pointsCount and getCoordinates methods.
For example, if you use getCoordinates, you get and array of CLLocationCoordinate2D, which you can then use convertCoordinate:toPointToView: to convert that to a CGPoint that you can reference in gesture recognizer.

MapKit Looking For More Annotations Than I've Specified

I have an app that calls addAnnotation exactly 3 times (after verifying that the coordinates are good using CLLocationCoordinate2DIsValid, passing a model that responds to id, title latitude, longitude and coordinate. In the model, I watch the callbacks by doing an NSLog in my coordinate method.
Note that coordinate and name are implemented as methods, although this should make no difference, right?
What I expect:
MapKit will access the coordinate method 3 times
What I get:
MapKit access the coordinate 3 times per coordinate, then tries a 4th time, even though there is no 4th coordinate and the app crashes on a memory exception as the result is nil.
I am pretty naive about MapKit, but there has to be some explanation for this that I'm missing.
Any help appreciated!
If the user location is shown on your map (the blue dot) you'll have four annotations, as the blue dot also is an annotation.
Your app probably crashes as the annotation used to show the user location has no method called coordinate
To prevent your code to call the method coordinate you can either check if the retrieved annotation is an instance of your custom annotation class:
if ([annotion isKindOfClass:[YourCustomeAnnotation class]])
// it's one of your annotations
else
// it's the current location annotation
or check if the current annotation is the current location anotation
if ([annotion isKindOfClass:[MKUserLocation class]])
// it's the current location annotation
else
// it's one of your annotations

how to create a variable radius in map view

I have a map view and I want to add a variable radius like in the find friends app, allowing the user to drag to make the radius smaller or bigger. How do I do this? Is there any prebuilt functionality that allows you to do this easily?
MKCircle is the class you are looking for. It conforms to the MKAnnotation protocol, so you can just use it like any other annotation. Use the MKCircleView class for the actual drawing of the circle (also check the available sample code).
It can be possible by just using a custom MKAnnotationView that shows a circle? That will maintain size as you zoom around.

Resources