plot pins on MKMapView around 10 kms from current location - iphone

I have an MKMapView & I am using PinAnnotation for plotting the pins on MKMapView.
I have a local database which contains the latitude & longitude of thousands of locations. I want to plot the pins around 10 kms from current location on MKMapView, when the application is launched.

It depends what your database. A simplistic way would be to work out the longitude difference for 10km (it is different at the equator and near the north pole) and the latitude difference for 10km, then search your database for everything between maxLat and minLat and maxLong and minLong. That gives you items in a rectangle but it's a start. From there you could cycle through every result, make it a CLLocation and check it is within 10km using distanceFromLocation.

I done it using the following way -
1) First found the current latitude & longitude using the delegate method of MKMapView(didUpdateUserLocation).
2) Found the distance from the current location with the location stored in my local DB in KM.
3) If distance <=10 Kms, then plot pin, else do nothing.

Related

Hey are coordinates from Apple Maps incorrect when used as a CLLocation in mapkit?

I’m working on an app that shows various places on a map (SwiftUI using mkmapkit in a uiViewRepresentable) and have found that coordinates are being placed off target for some reason.
I have the data listed in Firebase as a map - Latitude value and Longitude value that is then transferred into a CLLocation in app.
Example of the problem is that Big Ben in London should be (according to Apple Maps) be at 51.50070N and 0.12455W doesn’t appear there in app (see image) but the app reports coordinates of 51.500702, 0.124610 - when tapped just below the annotation.reported coordinates
The sign on the longitude is incorrect. According to my MKLocalSearch (and confirmed by dropping an annotation there), Big Ben is at a latitude of 51.5006854 and a longitude of -0.1245698. Note, that's -0.1245698 (aka, 0.1245698W) and not 0.1245698 (aka 0.1245698E).
A longitude is defined “relative to the zero meridian, with positive values extending east of the meridian and negative values extending west of the meridian.”
But your map is showing the coordinate east of the meridian, not the one west of the meridian. Here are the two coordinates:
And zooming in on that incorrect coordinate to the east, that's obviously where your map is pointing:
But Big Ben is to the west of the meridian:

NSPredicate to limit latitude longitude range according to MapView bounds

So I have an app with a MapView and I use CoreData to load annotations associated with locations nearby the center of the map. Currently, I define the lat/long range statically like this:
var fetchDataPredicates = [NSPredicate]()
fetchDataPredicates.append(NSPredicate(format: "itemLatitude BETWEEN {%f,%f} AND itemLongitude BETWEEN {%f,%f}", (latitude-0.10), (latitude+0.10), (longitude-0.10), (longitude+0.10)))
However, this is a pretty poor solution. If the user has zoomed out far in the map then the data would only cover a small portion of it.
Does anyone have any good ideas on how to dynamically adjust the lat/long range according to the MapView bounds?
Thanks!
A likely solution would be to ask the map view for its region. That's an MKCoordinateRegion which includes
CLLocationCoordinate2D center
MKCoordinateSpan span
The span includes longitude and latitude deltas, which look like exactly what you need.

GPS how to combine latitude/longitude with horizontalAccuracy to create "buckets" of data?

I'm processing user photos on an iPhone and some of them are geotagged (have latitude and longitude). For example, my longitude looks like -73.123456. Each GPS tag has a horizontalAccuracy property, expressed in meters.
How can I combine the horizontal accuracy with latitude/longitude to create groups(buckets) of GPS coordinates?
For example, I want to have all photos that are taken around the user's house to fall into one bucket, while all photos taken around work fall into another bucket.
I originally was thinking about multiplying the latitude/longitude by 1,000,000 , then cutting off fractional components:
CLLocation *assetLocation = [asset valueForProperty:ALAssetPropertyLocation];
CLLocationCoordinate2D coordinate = [assetLocation coordinate];
NSInteger latitudeInt = coordinate.latitude * 1000000;
NSInteger longitudeInt = coordinate.longitude * 1000000;
latitudeInt = latitudeInt-(latitudeInt % 100000);
longitudeInt = longitudeInt-(longitudeInt % 100000);
NSLog(#"trimmed:(lat %f, lon %f) real: (lat %f, lon %f) ", latitudeInt/1000000.0, longitudeInt/1000000.0,coordinate.latitude,coordinate.longitude);
This will result in GPS reading of -73.1
The problem with this approach is that for one photo I get GPS location that is 1km off, but if I subtract 0.099 from the latitude/longitude reading, the error can be up to 5kilometers. Ideally, my buckets would each be 1-2 kilometers across, centered around some GPS coordinate.
This poses a question - how can I combine information about latitude/longitue, together with horizontal accuracy to create groups/buckets of GPS coordinates? They don't have to be accurate or centered directly onthe user's house, but I would like all photos taken around the users's house to fall into the "user's home" bucket.
You should look into the k-means clustering Algorithm.
Your task is a quite difficult, this will cost a lot of time and needs algoritmic (geo-spatial) knowledge. Think again if you need that.

How to know distance between places

Hello I am creating travel app. I want to find current location and find the distance between selected hotel or place from this location.. I searched core location but it is not returning longitude and latitude of current location in simulator .please help me and how can I calculate distance ?
To calculate distances you can use the distanceFromLocation: method of the CLLocation class.

Calculate distance from Location - iPhone

I'm trying to calculate the distance between two places user Core Location. I've found a few posts that state to use
-(CLLocationDistance)distanceFromLocation:(const CLLocation *)location
Found some other test code in the thread below:
CLLocationDistance NaN
I'm not sure how to put it all together, to get the result I want ?
Anyone any thoughts ?
Regards,
Stephen
If you have a location named myLocation and want to find the distance from another location, say, restaurantLocation, then it seems you would do something like
CLLocationDistance distance = [myLocation distanceFromLocation:restaurantLocation]
This will give you the distance, in meters, between the two.