I have my application designed using Php-Mysql and I should convert it to rails. I have a table called coordinates and a search form for the same.I have two variables called latitude and longitude. the coordinates table cantains city,latitude and longitude. when the user enters the city and if it's found in the table then it should assign that city to the matching city in the coordinates table and the latitude and longitude should be assigned to the respective latitude and longitude in the coordinates table.
my code
$result = mysqli_query($con,"SELECT * FROM coordinates");
while($row = mysqli_fetch_array($result))
{
if(strtolower($location) == strtolower($row['city'])){
$latitude = (float) $row['latitude'];
$longitude = (float) $row['longitude'];
break;
}
This s my php code . location is entered by the user in the search button. I want the same thing t to b implemented using rails . anyone pls help me to implement the rails code . thks in advance`
Assuming you've created an ActiveRecord model for coordinates you can do something like:
coordinate = Coordinate.where(city: city_to_find).take
and the latitude will be in coordinate.latitude and the longitude in coordinate.longitude.
Notice that this doesn't use a loop and will run a database query to retrieve just the required record, and that you could have taken a similar approach with your PHP code.
Related
I got an array of location objects. ( latitude , longitude, location name).
Location is received from users gps from morning to evening.
Challenge:
I want to create trip by filtering the location objects.
For e.g.: trip 1 will contain location from one start point to another and trip 2 will contain...
User can make 2/3 trips per day.
Question:
How can I group that array containing closer(values) latitudes and longitudes in swift iOS ?
EDIT
Get the data from array if the user is traveling else leave them so that from that array i can make a trip
Any ideas highly appreciated.
I think there is no way like magic code in swift for that.
How I actually solve is by traditional solving technique.
I did made a for loop and inside made a checker (which checked if two consecutive elements got time interval > 30 minutes then add the collected array elements as object to main array .
If anyone searching for such then apply you own logic.
var closure:((_ sender:UIButton)->())? = nil
var closure:((_ sender:UIButton)->())? = nil
closure!(sender)
cell.closure = { (sender:UIButton) -> Void in
debugPrint("\(indexPath.row + 1)")
debugPrint(sender.tag)
}
I have latitude and longitude of some cities and I want to input it the geocoder randomly. Can anyone please tell me how do I do that. The values are both negative and float. for an instance mexico lat/long 23.634501,-102.552784
myLatlng = new google.maps.LatLng(1.352083,103.819836);
I want to put values randomly from a set of values in Latlng function. Thanks
You don't specify your programming language, so I'll answer language agnostic.
I'll assume you have a Point type that can take two double values, e.g.
class Point
{
double Latitude;
double Longitude;
}
and that you have a list or set of instances of Point representing the set of latitude/longitude pairs you want to select from.
List<Point> points;
Just generate a random integer between 0 and (set size -1). Use that random integer as an index into the list.
int index = Random(0, points.Length-1);
Point myRandomPoint = points[index];
Now use that Point in your call to Google
myLatlng = new google.maps.LatLng(myRandomPoint.Latitude, myRandomPoint.Longitude);
I am using html5 geolocation api to get my position in latitude and longitude. I want to store them in a table of locations and want to retrieve those locations within a particular distance.
my current latitudes and longitudes are stored in variables "latval", "longval", "distance"
My table is "location"
columns are "location", "lat", "long"
I am using DB2 Express C as database and latitude and longitude columns are set as double type now. What type should I use to store these values and what would be the query to get location names within a distance
Thank you.
It looks like there's an extension for Express C that includes Spatial processing. I've never used it (and can't seem to get access at the moment), so I can't speak to it. I'm assuming that you'd want to use that (find all locations within a radius is a pretty standard query).
If for some reason you can't use the extension, here's what I would do:
Keep your table as-is, or maybe use a float data-type, although please use full attribute names (there's no reason to truncate them). For simple needs, the name of the 'location' can be stored in the table, but you may want to give it a numeric id if more than one thing is at the same location (so actual points are only in there once).
You're also going to want indicies covering latitude and longitude (probably one each way, or one covering each column).
Then, given a starting position and distance, use this query:
SELECT name, latitude, longitude
FROM location
WHERE (latitude >= :currentLatitude - :distance
AND latitude <= :currentLatitude + :distance)
AND (longitude >= :currentLongitude - :distance
AND longitude <= :currentLongitude + :distance)
-- The previous four lines reduce the points selected to a box.
-- This is, or course, not completely correct, but should allow
-- the query to use the indicies to greatly reduce the initial
-- set of points evaluated.
-- You may wish to flip the condition and use ABS(), but
-- I don't think that would use the index...
AND POWER(latitude - :currentLatitude, 2) + POWER(longitude - :currentLongitude, 2)
<= POWER (:distance, 2)
-- This uses the pythagorean theorem to find all points within the specified
-- distance. This works best if the points have been pre-limited in some
-- way, because an index would almost certainly not be used otherwise.
-- Note that, on a spherical surface, this isn't completely accurate
-- - namely, distances between longitude points get shorter the farther
-- from the equator the latitude is -
-- but for your purposes is likely to be fine.
EDIT:
Found this after searching for 2 seconds on google, which also reminded me that :distance would be in the wrong units. The revised query is:
WITH Nearby (name, latitude, longitude, dist) as (
SELECT name, latitdude, longitude,
ACOS(SIN(RADIANS(latitude)) * SIN(RADIANS(:currentLatitude)) +
COS(RADIANS(latitude)) * COS(RADIANS(:currentLatitude)) *
COS(RADIANS(:currentLongitude - longitude))) * :RADIUS_OF_EARTH as dist
FROM location
WHERE (latitude >= :currentLatitude - DEGREES(:distance / :RADIUS_OF_EARTH)
AND latitude <= :currentLatitude + DEGREES(:distance / :RADIUS_OF_EARTH))
AND (longitude >= :currentLongitude -
DEGREES(:distance / :RADIUS_OF_EARTH / COS(RADIANS(:currentLatitude)))
AND longitude <= :currentLongitude +
DEGREES(:distance / :RADIUS_OF_EARTH / COS(RADIANS(:currentLatitude))))
)
SELECT *
FROM Nearby
WHERE dist <= :distance
Please note that wrapping the distance function in a UDF marked DETERMINISTIC would allow it to be placed in both the SELECT and HAVING portions, but only actually be called once, eliminating the need for the CTE.
When I add:
fields: [ "doc['Location'].lon" ]
to my query, the longitude value is different that what is shown in the document source.
Here you can see the results of a query where I fetched the doc['Latitude'].lon and .lat, and the _source.Latitude to compare:
https://gist.github.com/d9533170f1f50fd27e87 (note - these have been passed through json_decode in PHP, but the data is the same before using json_decode.)
I first noticed this when I was using "doc['field_name'].distance(lat, lon)" to try and add the distance as a field to my query. I tried both the "script_fields" and "fields" keys and each had the same result.
UPDATE: I noticed that the "doc['Location'].lon" is returning what I thought should be the doc['Location'].lat (the lat and lon are switched.)
The problem was that when using GeoJSON format (or using lat/long as an array) you have to switch the order of lat/lng to lng/lat.
I am rebuilding my index, but, in order to work around this I have used this query for now:
doc['Location'].distance( lon + 180, lat ) // Temporary bandaid
Once I've rebuilt the index with the correct values I'll switch back to:
doc['field_name'].distance(lat, lon) // The correct method
I have rails application, which deals with some google map related stuff.the problem is , i have a table which contains latitude & longitude columns. the column types are "float". for some occasions i need to generate the query by the following:
clients.find_all_by_lat_and_lng(latvalue,lngvalue).
I gave the correct & existing lat & lng values to fetch the database value. but i can get only Empty.
I tried with difference dynamic finders like find_by_lat,find_by_lng i get only empty values from the query ...but data are exits in the table.
I guess the Float column type is the culprit here, i don't how to overcome this and get the values. Can one suggest me on this.
NOTE : if i work with same query in windows i get the values. my box is ubuntu here i can't get the values
clients.find(:all, :conditions => ["lat = ? and lng = ?",latvalue, lngvalue])
try this your model name is "clients" ???
or Client