Converting a view from MKMapKit to a region - ios

I am displaying an MKMapView with another custom view overlaid on top (the 'radar' seen below). The radar is simply a static view that always stays centered and doesn't resize - it's not a map annotation. The idea is that a user can scroll and zoom the map until the radar covers a region they are interested in.
What's the best way to get the MKCoordinateRegion covered by the radar? I've tried convert:toRegionFromView: but I'm not seeing the results I would expect:
print("map region: \(mainView.map.region)")
// region: MKCoordinateRegion(center: __C.CLLocationCoordinate2D(latitude: 37.132839999999987, longitude: -95.785579999999968), span: __C.MKCoordinateSpan(latitudeDelta: 74.933954323122975, longitudeDelta: 61.276015749875469))
let radarRegion = mainView.map.convert(mainView.radar.frame, toRegionFrom: mainView.map)
print("radar region: \(radarRegion)")
// radar region: MKCoordinateRegion(center: __C.CLLocationCoordinate2D(latitude: 32.559875544717606, longitude: -95.760911716686806), span: __C.MKCoordinateSpan(latitudeDelta: 21.752004541359227, longitudeDelta: 25.901697478812096))
I would expect the radarRegion to have the same center coordinate, and a span roughly half, of those of map.region. Neither are true.
Here's a small project that replicates my setup.

According to the Apple documentation, convert:toRegionFrom: method:
Converts a rectangle in the specified view’s coordinate system to a
map region.
Parameters
rect
The rectangle you want to convert.
view The view that serves as the reference coordinate system for the
rect parameter.
So you really need to specify the view for which you provide the coordinates as the second parameter, not the map view :)
So, I believe if you pass in the frame for the radar as the first parameter, you should pass the radar view's parent as the second parameter and that should (theoretically) work.
Basically, if view is the parent view of which map (the MKMapView instance) and radar (the radar view) are children, I tried the following code:
let frame = radar.frame
let radarRegion = map.convert(frame, toRegionFrom:view)
map.setRegion(radarRegion, animated:true)
Before the above code was executed, this is what the screen looked like:
And after the above code runs:
The basic region is correct though there is a little bit of shifting up.
If this is not what you were expecting with regards to the region selected by the radar view, could you please explain what I might be missing?

Related

react-native-maps animateToRegion has lower deltas (smaller region) than onRegionChangeComplete

I am using react native maps in typescript in an iOS app. My initial region is pretty small, with latitudeDelta = 0.0000922 and longitudeDelta=0.0000922*(screenwidth/screenheight). My code uses useRef to save a reference to the MapView and when I get the first location (first latitude and longitude) from my server, I use mapRef.current.animateToRegion to animate to the region:
const INITIAL_LATITUDE_DELTA = 0.0000922;
const INITIAL_LONGITUDE_DELTA = INITIAL_LATITUDE_DELTA*(screenwidth/screenheight);
const server_callback = (serverResult) => {
...
mapRef.current.animateToRegion({
latitude: serverResult.latitude,
longitude: serverResult.longitude,
latitudeDelta: INITIAL_LATITUDE_DELTA,
longitudeDelta: INITIAL_LONGITUDE_DELTA
});
}
This works fine and shows my little region (about the size of a suburban backyard, a little blurry but fine). But I also want to allow the user to zoom in and out using hand gestures (pinch and expand) so I implement onRegionChangeComplete:
const onRegionChangeComplete = (newRegion: Region) => {
setRegion(newRegion);
}
The problem is that as soon as I do this, the size of the region expands to (what appears to be) the minimum size allowed by setRegion for the map. It seems like as soon as the user uses a gesture to change the region, or even uses a swipe to move it, the region size (the latitudeDelta and longitudeDelta) suddenly increase in value so that what I get in the region newRegion inside onRegionChangeComplete is this larger region. I can't zoom in any closer than this.
My MapView is defined like this:
<MapView mapType="satellite" style={styles.map} initialRegion={region} onRegionChangeComplete={onRegionChangeComplete} ref={mapRef}>
....
</MapView>
So, is it the case that animateToRegion allows me to zoom more than manual region changes allow? I need one of two things. Either I need to find a way to allow the manual gestures to keep this high zoom level (low deltas), or I need to know what the minimum values for the deltas are so that I can uses these as my initial/default values. How would I do either of these?

How To Detect A User Is Within Range Of An Annotation

So I am working on a project that includes many uses placing annotations all around a map. The annotation, (which is a custom image with a much larger circular range) appears on the screen and, ideally, I would like for a user to be:
Notified if they are within the range of a annotation
and
Not be allowed to place another annotation within the range of another one if the circular pins overlap by, say, more than 25%
I think this is a pretty unique question and should be fun for somebody to help out with, so have fun! Thanks everybody!
You can check the distance from each annotation using
- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location
This method measures the distance between the two locations by tracing
a line between them that follows the curvature of the Earth. The
resulting arc is a smooth curve and does not take into account
specific altitude changes between the two locations.
For more details refer Link
Try this:
let location = CLLocation(latitude: 1, longitude: 1)//Or user's location
let distance = location.distance(from: anotherLocation)
Edit:
As mentioned in the comments, you wanted to create an equidistant point. I suggest manually doing that:
Subtract the annotation's location from he user's location. Then add your distance back to the original one. For example:
The user's location = (1, 1)
The annotation's location = (3, 2)
Vertical difference would be 2
Horizontal difference would be 1
Then:
(3 + 2, 2 + 1)
Your result: (5, 3)
Now you would have two points (the one you just created and the user's location) at each end with a center point (original annotation)

(Swift 2.1) Load annotations for visible region in MKMapView

I am calling API and getting latitude, longitude, title and many other data and storing them into custom data structure. I get 20 results per request.
So i need to show only some of those 20 results into visible area of MKMapView. When user change the region of map, API call should be done and my custom data structure will be filled and again only in visible area annotation should be drawn.
I am calling API again into mapView:regionDidChangeAnimated method but i am not able to show annotation into visible Map area only.
Can anyone help me to figure out how I can show annotation into visible are only?
Note: This is a same question but for Objective-C and I don't understand the answer of #Shmidt, and how I can use that code.
MKMapView has attribute visibleMapRect, then you can use function MKMapRectContainsPoint
let point = CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0)
let mapContainsPoint = MKMapRectContainsPoint(map.visibleMapRect, MKMapPointForCoordinate(point));`

Check if coordinate rectangle contains CLLocationCoordinate2D

I am using a special Map SDK for iOS and I am adding a custom shape to the map. The shape is always a different size and it could be a circle, square, star etc. the point being it is always dynamic whenever the app is run.
After adding this shape to the map, I can access it's property called overlayBounds which is described as: This property contains the smallest rectangle that completely encompasses the overlay.
The overlay is my shape that I'm adding to the map.
Whenever a location update is generated by CLLocationManager, I want to check and see if the most recent coordinate is inside of that overlayBounds property of the shape.
When accessing overlayBounds, it has an ne property and a sw property. Both of these are just CLLocationCoordinate2D's
So, if the overlayBounds is made up of two CLLocationCoordinate2D's and the CLLocationManager is always updating the user's location and giving me the most recent coordinate(CLLocationCoordinate2D), how can I check if that most recent coordinate is within the overlayBounds?
After doing a lot of research I have only found one potential solution to go off of which is this: https://stackoverflow.com/a/30434618/3344977
But that answer assumes that my overlayBounds property has 4 coordinates(CLLocationCoordinate2D's), when I only have 2.
Your description seems much harder then the actual question. So if I am getting this correctly your question is only to check if the point is inside the rectangle described in overlayBounds.
You have only 2 points as it is enough to define a rectangle. So NE and SW are the two points where the other two are received as (NE.x, SE.y) and (SE.x, NE.y). With this you may use the answer you linked or you may simply construct a MKMapRect where origin is NE and size is SE-NE. So in this case you may simply use MKMapRectMake and then use MKMapRectContainsPoint. BUT watch out when computing size as SE-NE might produce negative results in which cases you need to add degrees to the size. That is 180 to x (latitude) and 360 to y (longitude)...
MKMapRect rect = MKMapRectMake(NE.latitude, NE.longitude, SE.latitude-NE.latitude, SE.longitude-NE.longitude);
if(rect.width < .0) rect.width += 180.0;
if(rect.height < .0) rect.height += 360.0;
BOOL pointInside = MKMapRectContainsPoint(rect, pointOnMap);
Something like this should do the trick.
Now if you are trying to check if the point is inside the shape itself it really depends on how your shape is defined. If this is some form of analytic representation you might find some method already made for you to return the value but if not then your best shot would most likely be drawing the shape to some canvas and checking the color of canvas at the location you need to check. In any case the bigger problem here is converting the point and the rect to a Cartesian coordinate system. If that is the case then just add a comment and I will try to help you on that...

iOS MapKit get actual visible area of MKMapView when mapView is 3D

I've tried the following to get the actual visible area of my MKMapView after a region change. None produce the desired result after the user rotates the map.
use mapView.bounds and mapView.convertPoint to get NE and SW CLLocationCoordinate2D.
use mapView.visibleRect to create NE and SW MKMapPoints and convert those points to NE and SW CLLocationCoordinate2D.
use mapView.centerCoodinate and mapView.region.span latitude and longitude delta to calculate NE and SW latitude and longitude, which are then used for new NE and SW CLLocationCoordinate2D.
#1 and #2 come from this post, and all 3 work well enough until the user rotates the map, which brings the mapView.camera into play by changing its heading. Once this happens, the mapView.visibleRect does not match the actual visible area. I'm sure changing altitude and pitch will have similar issues. I understand why properties on MKMapView don't make sense once it goes 3D, but I don't know how to account for the mapView.camera. There is mention of this in a comment on one of the proposed answers in this post, but no solution provided.
My question is, how can I get the area that's actually visible to the user, through the mapView.camera, accounting for heading, altitude and pitch?
I was looking for an answer to the similar situation, and found this. For my project I resolved like the following. Hope this helps.
let northWestCoordinate = self.mapView.convert(CGPoint(x: 0, y: 0), toCoordinateFrom: self.mapView)
let southEastCoordinate = self.mapView.convert(CGPoint(x: self.mapView.frame.size.width, y: self.mapView.frame.size.height), toCoordinateFrom: self.mapView)

Resources