I'm working on an app that lets a user select locations on a map. The entire map is subdivided into irregular regions (administrative boundaries), and when a user touches a point on a map, I need to be able to figure out which region the point belongs to. Just to clarify, there is no finite set of points for a user to choose from, they just tap anywhere on the map.
What is the best way to achieve this? I have been looking at MKPolygon class but cannot really figure out if this is the way to go. If it is, would I be using intersectsMapRect: method of the MKOverlay protocol to check for a match? Are there any good tutorials on this kind of map operations?
A good approach here might be the MapBox iOS SDK and it's RMInteractiveSource, which is designed for this. Check out this sample app which shows interactive regions.
This is done by a space-optimized, offline-capable key-value store of sorts that keys pixels at varying zoom levels to arbitrary content values (region name, data, imagery, etc.)
In MapKit proper, you'll need some sort of spatial analysis (maybe Spatialite?) to determine intersections between points touched and irregularly-shaped regions.
Related
I am new to iOS programming (obj c) and would like to create a feature for my app. I have divided (in photoshop) the world's coastlines into 204 separate regions. I would like to set up these regions in Apple's MapKit so that when a user is in any of these regions, my app will know which region they are in.
I realize this is a longer exercise, but I am only hoping that someone can point me in the right direction with either some basic code or what to search for.
To be clear, for now, just want to be able to draw (or define) the regions in MapKit
Thanks
Have a look at this tutorial:
http://www.raywenderlich.com/30001/overlay-images-and-overlay-views-with-mapkit-tutorial
You'll find an example on how to store your regions in the app and how to show them on the map.
I am trying to find a way to create several polygon geofences with iOS. I need to draw multiple zones in a city to represent areas, streets, etc. From what I've read so far, iOS only allows circular zone from a geolocated device.
Is it feasible with iOS?
Is there a web app somewhere to draw polygons on a map and generate the coordinates in an array?
1) iOS only allows to create circular geofences indeed however what you are trying to achieve is possible with some extra logic.
I have developed similar features so I suggest you to do the following:
create a circular geofence that embeds your polygon
when the device gets notified as being within the circular geofence,
start the GPS
every time you get a location update, check if its coordinates are
within the polygon
turn off the GPS as soon as the device's location is found within the
polygon, unless you need to be notified when exiting the polygon as
well
turn off the GPS when the device gets notified as outside the
circular geofence
As you need polygon geofences I guess you expect a good level of accuracy, so you would need to use an extra layer of GPS on top of the geofencing anyways, as geofencing is not accurate at all.
2) Have a look at those links:
https://github.com/thedilab/v3-polygon-shape-creator
https://github.com/tparkin/Google-Maps-Point-in-Polygon
When a user taps a point on an MKMapView in my application, I want to determine what country they tapped on. Speed is my priority, since a user will definitely notice the lag between a map touch and an annotation callout begin presented with the name of the country.
I have the polygon information for all the countries and I can parse/store them in any kind of data structure necessary. Currently, they are in GeoJSON format and have thousands of verticies. I also have bounding boxes computed and stored for each country.
One suggestion was to store an array of CGRects for each polygon's bounding box and first doing a CGRectContainsPoint search on all the bounding boxes to quickly narrow down the search. If that search returns multiple bounding boxes (common on country borders where the bounding boxes can overlap) then I can check the filtered country's full polygon for the point in question. Even on very congested overlapping areas, this full search would be 5 or less full country polygons to check.
To accomplish this, I'll need to store both a CGRect bounding box and a full, complex CGPath for each country. (and a country can have multiple polygons if it has territories) I don't know if storing these in-memory is a good idea, since there are potentially many thousands of polygons.
Compiling SpatiaLite is not an option because of the licensing requirements for GEOS. Compiling SQLite with the R-Tree extension is a possibility. The polygons do not change, so I'm able to precompute and store them on disk in any way that is suggested.
If I follow this suggestion, what is the best way to store and access all these CGRect and CGPaths for quick searching and access?
I'm open to any other suggestions people have.
I would recommend either using the Mapbox iOS SDK or at least using the RMInteractiveSource and RMMBTilesSource parts of it. You can use TileMill to turn your source data into raster tiles with encoded interactivity info on a per-pixel basis. Here's an example: https://github.com/mapbox/mapbox-ios-example The interactivity data is basically a highly efficient key-value store on a per-pixel basis. You can then convert a CGPoint at a given map zoom level and panning offset into the value.
Technology background: https://www.mapbox.com/developers/utfgrid/
It seems to be a known fact that MKMapView (and Google's maps in general) have a varying offset on 100-600m which makes annotations display incorrectly on the map.
According to this thread, Google has a private method called _applyChinaLocationShift, and it works, but apparently only for CLLocations that are given by CLLocationManager. For arbitrary CLLocations, it returns nil. The app I'm writing only needs to work in one city, so I've thought of pre-sampling the area using _applyChinaLocationShift and store the inverse transforms in the shipped app if that was possible.
So basically, is there any way to convert a coordinate to a coordinate that corresponds to the transformed China maps?
How about using location simulation in the Simulator and feeding it a bunch of coordinates in that particular city.
I'm building an iPad app that will present a screen-by-screen walkthrough of directions sourced from Google's Directions API. I'd like to track the user's progress through physical space using CoreLocation and update the screens to follow the user, similar to most directions applications.
My initial idea is something along these lines:
For each step in the directions, grab the corresponding polyline
When CoreLocation updates, check whether the lat/long pair are within some delta of some point on the polyline (ie, iterate over all the points on the polyline).
If the location is within the polyline, stay on the same screen
If not on the polyline, check whether the user is within the same delta of some subset of the polyline for the next step (say 10 points) and, if so, advance to the next screen.
If not on the next polyline, alert the user that they've left the route.
This seems inefficient and not particularly accurate... Are there better ways to do this?