i am implementing an app, that show customers and my userLocation on a map.
But now i have a problem, because there are a lot of costumers and the map is very uncomfortable.
How can i only see pins on a map or in a list, that are around 100 kilometers away from
me?
I hope someone could help me?
Greetings Marco
you should cache your current location in a CLLocation* variable.
You have to calculate the distance of every pin (annotation) you add from this location using [myLoc getDistanceFrom:pinLoc] which returns a CLLocationDistance i believe. If this is > 100 kms don't add it to your map.
Related
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.
I have around 800 geo co-ordinates in my iPhone app as a flat file. I am searching for an effective way to find an algorithm which will take the current user location, loop through all these 800 coordinates and pull only the coordinates which are in 10 miles vicinity. How effectively this can be done? Also please share links which will get me the basic understanding on the maths behind this.
Here is a link for a question where the final code of OP may help you understand the how to create locations from coordinates and how to compute the distance between them.
Here is how to create a location:
CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
And here is how to find the distance between two locations:
CLLocationDistance distance = [locationA distanceFromLocation:locationB]; //CLLocationDistance is a double
However you don't have to sort the locations. Just loop through them and add the near locations to an array.
First, I think everyone agrees that to compute distance, you need to use the Haversine function.
Finding the closest point to a given point
If the search time is an issue (iterating over the 800 data points you mentioned) then how about a 2D hash? Simply load the dataset into buckets or regions based on lat/long - then, you won't have to search through the whole data set - only the possible buckets that may contain matches.
Good hash function for a 2d index
I'm starting with geocoding. And I have a lot of doubts.
I'm able to do forward and reverse geocoding (I guess, its not perfect).
And now, I'm trying to detect if user (device) enters or leaves a region. For that, I picked up apple's sample code "Regions". The sample uses regionMonitoring. I already try it in a device, but its not working well. I set a region with 25 meters radius, and when I left the region (walking) doesn't happen anything.
My question is: there is another and better way of doing this, detect if user enters or leaves a region, than regionMonitoring?
Can someone help me here??
Thanks a lot.
you could keep the user-location tracking running in the background (here is a good tutorial) but keep in mind this can be heavier on battery use than regionMonitoring.
I found a solution to calculate the distance between two CLLocationCoordinate2D it is easier than I though:
- (CLLocationDistance) DistanceBetweenCoordinate:(CLLocationCoordinate2D)originCoordinate andCoordinate:(CLLocationCoordinate2D)destinationCoordinate {
CLLocation *originLocation = [[CLLocation alloc] initWithLatitude:originCoordinate.latitude longitude:originCoordinate.longitude];
CLLocation *destinationLocation = [[CLLocation alloc] initWithLatitude:destinationCoordinate.latitude longitude:destinationCoordinate.longitude];
CLLocationDistance distance = [originLocation distanceFromLocation:destinationLocation];
[originLocation release];
[destinationLocation release];
return distance;
}
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.
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.