I would like to get the meters of a MKPolyline path so I can tell the user what are the meters left to finish the path.
I've been searching for something to do it but I couldn't get anything.
Thanks.
As tedious as it sounds, unless you want to do the maths yourself you're probably going to have to iterate thought the line's points, convert each to a CLLocation and use its -distanceFromLocation: between each, summing up to the appropriate total.
Related
Bit of a weird one I hope someone can help out with.
In leaflet, once the user has entered a lat/lng and added a point to the map I want to be able to also add a 10km square around that point.
I've tried looking around for a calculation to find the corners of the square x Km away but haven't dug anything up. But surely there's an easier way!
Does anyone have any thoughts? It'd be lovely to just say L.polygon then pass in a centre point and a square size.
Thanks,
Tayler
Initialize a L.Circle on your desired latitude/longitude with a radius of 5000 meters, grab the boundaries and use them to initialize a L.Rectangle:
new L.Rectangle(new L.Circle([0, 0], 5000).getBounds())
May I know how I should calculate the bearing of one point relative to another? All the formulas I'm seeing on the Internet are for lat/lon coordinates. I'm working with the Cartesian coordinate system here and am unable to find a solution. Please help!
Use atan2 function (IIRC your objective-c should have it).
It gives you a result between -PI and PI. You have to map it to 0-360 if you need it.
Not really an answer to your question, but if you're getting your points using CLLocationManager, each CLLocation has a course property, which gives you the bearing of your journey for the given point. For the actual math, see Axeman's answer.
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
In my application i am having 4 places with its longitude and altitude(Given).Now i want to find the nearest place from user.
So anyone can tell me the solution for it or provide me some source code or demo for this.
Thanks to all
Create an CLLocation of the points:
CLLocation* locationx = [[CLLocation alloc] initWithLatitude:lat longitude:lon];
And the compare the points with:
double distance = [userposition distanceFromLocation:locationx];
Keep the nearest location.
Edit:
If you really mean "longitude and altitude" then I dont know how.
just took 10 seconds to ask google and find this, wich describes how to calculate the distance beween two points given latitude and logitude:
http://www.movable-type.co.uk/scripts/latlong.html
all you have to do is calculate the distance to all 4 points and choose the lowest one.
ETDI: i'm sure you're also given the latitude, otherwise you won't even have specific coordinates. if this wasn't a type and you really have to take the altitude into account, take a look at this question at google answers
picknick describes a native class method.
This is probably going to use the Haversine formula (search the web or stackoverflow for discussions) to measure distances.
In my experience, writing your own Haversine is usually faster than using a system/app-provided one, although this is on Windows where COM overhead can be "problematic" as it were!
The Haversine (and many similar geometry-on-the-Earth) formulae can be found here:
http://williams.best.vwh.net/avform.htm
(this is one of my few bookmarks I go back to for reference on a regular basis!)
for example user is at x=0,y=0
Calculate the distance by doing : SquareRoot(x2^2 & y2^2)
You will get a distance. Look through all points and compare which distance is the shortest
I have a series of nature reserves that need to be plotted, as polygon overlays, on a map using the coordinates contained within KML data. I’ve found a tutorial on the Apple website for displaying KML overlays on map instances.
The problem is that the reserves vary in size greatly - from a small pond right up to several hundred kilometers in size. As a result I can’t use the coordinates of the center point to find the nearest reserves. Instead I need to calculate the nearest point of the reserves polygon to find the nearest one. With the data in KML - how would I go about trying to achieve this?
I've only managed to find one other person ask this and no one had replied :(
Well, there are a couple different solutions depending on your needs. The higher the accuracy required, the more work required. I like Phil's meanRadius parameter idea. That would give you a rough idea of which polygon is closest and would be pretty easy to calculate. This idea works best if the polygons are "circlish". If the polygon are very irregular in shape, this idea loses it's accuracy.
From a math standpoint, here is what you want to do. Loop through all points of all polygons. Calculate the distance from those points to your current coordinate. Then just keep track of which one is closest. There is one final wrinkle. Imagine a two points making a line segment that is very long. You are located one meter away from the midpoint of the line. Well, the distance to these two points is very large, while, in fact you are very close to the polygon. You will need to calculate the distance from your coordinate to every possible line segment which you can do in a variety of manners which are outlined here:
http://www.worsleyschool.net/science/files/linepoint/distance.html
Finally, you need to ask yourself, am I in any polygons? If you're 10 meters away from a point on a polygon, but are, in fact, inside the polygon, obviously, you need to consider that. The best way to do that is to use a ray casting algorithm:
http://en.wikipedia.org/wiki/Point_in_polygon#Ray_casting_algorithm