I need to be able to map out in coordinates real world cities and then have a method that calculated the distance between two cities that I give it... What would be the best way to do this? just citie object that has coordinates and then ill figure out the fomula for calculating the distance my self or a hash map or something... another thing is how am i gonna figure out the coordinates of the cities? Im talking about real world cities so yeah...
i had an idea of getting the latitude and longitude of the cities and then somehow when calculating distance turn it into kilo meters... If anyone can help my if this I would be glad!
Thank you very much!
If you need more info just tell me and ill add it.
What I mainly need: A suggestion to based on what map the cities and a lead on implementing distance calculations.. I would not like to check out the distance between each city i make to all the rest online then put it in because it seams to be a lot of work and if i manage to figure out a algorithm then it would be much faster and easier to add cities to the game...
First you need to translate city name to geographical coordinates. GeoNames is a big database of geographical locations including coordinates out of many others available.
When having geographical coordinates of two cities, you can use simple equation to calculate the distance, see How to find distance from the latitude and longitude of two locations? and Great-circle distance.
one part is GREAT CIRCLE DISTANCE - there are lots of examples.
finding a data set of city coordinates may be trickier.
Related
I am having a predefined route as a set of locations with latitude and longitude. I want to create a polygon by using these co-ordinates and want to know when the user deviates from it.
Anyone have any tips or sample code to draw a virtual fence across the points A, B and C ?
I don't know if polygon is the way to do it...
I can suggest a more basic approach, where you compute the distance of point to a line segment and check for distance < fence radius
you can compute line segments of your route, in your case the segments are (A,B) (B,C)
when you got a new position and want to know if it reside within the fence, you just compute the distance of that position to each line segments
the calculation of this is explained (very clearly, with code examples) in here
the math of geo position is pretty straight forward when dealing with small areas (don't need to take the earth curvature into consideration) but even if you do, it's a small change and there a lot of code examples for that either
I am currently making a map generator using Java. I am using arrays to store coordinates, which are then used to draw land. So I also have cities, and I want to draw the most direct path from city a to city b without crossing the water, AKA, only using the coordinates that are stored as land. How can I do this? I have tried many algorithms a;ready, like solving systems to make sure things do not overlap (which takes too long, as i have 75,000 points) and also finding the nearest city that does not have any water in between the two, found by drawing a best fit line and solving just one systems (extremely inaccurate and probably just wrong). Thanks for all the help in advance!
I have multiple GPS tracks(vector of 2d latitude, longitude coordinates) that I have created using my mobile device. They have different lengths and directions. I want to average this tracks and create just a single one. As a first step I would like to select only the points that are in a certain area. For example in the image bellow I want to select only the points that are between the grey lines.
Given the fact that the tracks might have different shapes and positioning would a bounding rectangle approach make sense? Are there better algorithms to do this?
I would suggest taking a look into these classes for practical use :
https://commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math3/stat/clustering/DBSCANClusterer.html
http://commons.apache.org/proper/commons-math/userguide/filter.html
I would try clustering points from multiple tracks. After that, I would use the center point of each cluster to get my average path.
For clustering, you can use kNN or any other principle where you group points that are near each other.
After getting your average path, you can apply bounds to it (you could also filter your points before clustering).
I thought that this one would be easy to find on net but seems its not.
I want to know the distance between 2 consecutive longitudes at a given latitude value. That is to measure with a rope keeping it parallel to equator. I can calculate it by simple geometry assuming earth to be a proper sphere. But will that assumption be acceptable?Please give an idea about the error margin in that assumption. Or,
Please let me know a mathematical formula or a Java/Android code to calculate it.
Thanks.
EDIT: Thanks for your responses, it seems to have erupted a mini storm . Please read my comment to David below for clarity
EDIT2: The debate is will the great circle distance (found by haversine formula) and the "rope" distance be the same? According to me it should be. As the great circle distance is the shortest distance between to 2 points, and in the case of rope distance i restrict myself to move along the same latitude from one point to another, which I think is the shortest distance. Isnt it?
EDIT3: I was wrong. After visualizing it a bit I realize that the great circle distance will not be the same as the longitudinal separation at a latitude ("rope" distance"). Both would be same only if the latitude happens to be the equator. What David has been saying. So yes no correct answers yet. For my case I would assume the earth to be a sphere, I would accept Laune's answer in some time as he/she has partially answered my question. Still would be really keen to know if there is way to get a correct calculation.Also, please go through the link given by Byzantine Failure (wats with the name??) It talks about how to create a stored procedure and and query for such problems, really helpful!! Thanks for all your responses!
Have a look at this: http://en.wikipedia.org/wiki/Geoid
For everyday purposes, you can use the spheric model. Geoid deformation starts in the higher latitudes, so unless you are into something very accurate, scientific or polar travel, you should be fine.
quite simple with android actually:
Location locationA = new Location();
locationA.setLatitude(latA);
locationA.setLongitude(lngA);
Location locationB = new Location();
locationB.setLatitude(latB);
locationB.setLongitude(lngB);
float distance = locationA.distanceTo(locationB);
The distance usually used in situation of geolocations is haversine distance. This is basically an approach of the real distance in earth's surface since it is the distance of two points lying on a sphere having the average radius of earth.
Anyway there are many implementations in Java if you search for them.
Edit:
In this case there isn't any real difference if the two points have some of their coordinates the same or not (if they have them both different).
You can check the following link for a detailed algorithm
http://www.arubin.org/files/geo_search.pdf
Although this is based on JavaScript, but there is actually a function of the API to calculate the distance between the two given lat long points: https://developers.google.com/maps/documentation/javascript/reference#spherical
I am writing a server side java application related to Geo-fencing.
I have a store with their latitude,longitude and radius
I also have Person's current position(in terms of latitude & longitude).
If he arrives within the geo fence i.e., within the store's radius i have to give an alert. I googled for the solution & there are some methods to identify person is inside the fence(circle) or outside the fence.
Calculate distance between store latitude,longitude and person's current latitude & longitude and if Distance < radius then person is inside the fence else outside the fence.
Using Polygon Geofencing
Can anyone suggest me which method is best from the above.
Thanks in advance
It depends on your needs. If you want to know whether a Person is inside 1km radius of store, then you need a circle search. Here, location of the store is the pivot. You might change the radius based on your need.
Polygon search is used when you need to search people in a particular area regardless of how much distance it is away from anywhere. People in Paris, New York or Istanbul area, not people inside 1km radius of a store.
In your case, where only radius (i.e. distance to the shop) is needed, I would definitely choose the radius search (your first proposition).
The Polygon Geofencing, I think, is using the Ray-Casting Algrithm which iterates over each segment of the polygon. This will take more time and could lead to some approximation in the case of a circle (depending on how the algorithm is applied).
I would have thought that the best method to use here would be a radius distance calculation, presuming you need to know the distance 'as the crow flies' so to speak. So geographical distance rather than travelling distance. This will be the most performant and simplest to implement. Examples available here and here.
If the latter is the case (which I hope for your sake it isn't), then I would imagine you would need to use polygon geofencing or perhaps Google's distance Matrix API.
Thanks for all your answers. In my project i have integrated both methods(Polygon & Distance). If a user enters into the polygon he ll get a notification and same like radius. I have stored the polygon co-ordinates and radius into my database.