Searching record within latitude and longitude range - php

hi i am using this map
https://google-developers.appspot.com/maps/articles/mvcfun/step6
i can get the latitude and longitude of the required location also i can get the distance (radius). so i have a database in which my each record have latitude and longitude. i want to search the record within the range selected on the map. how the sql query will work ?
for better explanation my each record have a latitude and longitude. user will select the map and i want to search the record within selected range.

The way I have always done this is to use the maps api to draw a circle with the required radius, find the lat long bounds of this circle and then query the database:
SELECT * FROM yourtable WHERE lat BETWEEN a AND c AND lng between b AND d
Where a and b are you top left coordinates and c and d are your bottom right. (more information available here).
Now that you have all objects within your bounding box you will need to pass them back to your front-end and determine whether or not they are within your radius. See this answer for more information.

Related

How to find Specific area from database by latitude and longitude in php laravel?

I want to return area name from database by given latitude and longitude ( not nearby places ) if given latitude and longitude in area A then return area A.
This is the area table structure:
I found some codes but its return nearby places
If you want the exact coordinates (no radius or nearby places), just do:
$village = Village::where('latitude', 15.501501)->andWhere('longitude', 32.4325101)->first();
Otherwise you can use the code you found for nearby places but reduce the radius to 100 merters (or less) distance, so it will only get one location.

Compare latitude and longitude in a rectangle area (no distance)

I have a database with users, defined with a latitude and longitude in a database (mysql) and I extract the infos with a php script.
An app on mobile allow to draw a rectangle, and send me the coordinates of the top left point, and the coordinates of the bottom right point.
How can I compare the latitudes and longitudes to find wich user is in the rectangle? I found many solutions with a circle and distances, but it doesn't fit my needs...
My current query, wrong! :
SELECT * FROM users WHERE latitude BETWEEN ".$latitudeTopLeft." AND ".$latitudeBottomRight." AND longitude BETWEEN ".$longitudeTopLeft." AND ".$longitudeBottomRight.";
Does anyone got an idea? Thanks...
MySQL has some basic spatial extensions (see spatial extensions). This allows you to store lat/long points as point records. Using this, you could create selections using the 'minimum bounding rectangle'.
But since you already have an existing structure, you'll might need to work with between.
$LAT1 = min($latitudeTopLeft, $latitudeBottomRight);
$LAT2 = max($latitudeTopLeft, $latitudeBottomRight);
$LON1 = min($longitudeTopLeft, $longitudeBottomRight);
$LON2 = max($longitudeTopLeft, $longitudeBottomRight);
$sql = 'SELECT * FROM users WHERE latitude BETWEEN '.$LAT1.' AND '.$LAT2.' AND longitude BETWEEN '.$LON1.' AND '.$LON2.';

MySQL Calculate Geolocation Distance with variable radiuses

I'm building a geolocation advertising platform and I've run into a snag. I can successfully calculate all of the businesses advertising within a given radius to a user using this (page 8). Now, the client wants to change the radiuses of the advertisers which you can think of as "service areas". A basic advertiser will have a service radius of 100 miles, but other companies who are larger or spend more on advertising might have a service area of 250 or 500 miles, for example. With this change, the previous calculation does not work.
How can I take all of these variable radiuses and distances in order to return all of the advertisers that would be valid for a visitor?
To better illustrate my problem, take a look at:
Currently we calculate and return all of the advertisers within a 100 mile radius of a user (companies A, B, and C). With the new change, we need to return all of the companies that have a service area that covers the user, which includes company D. Company E has a smaller service area/radius which doesn't cover the user, so that record should not be returned.
The advertiser's table currently looks similar to:
id
name
lat
lng
radius
Given lat/lng of two points distance in miles can be calculated, see:
Find distance between two points using latitude and longitude in mysql
So if the visitor position is given (let's define it as visitor_latitude and visitor_longitude) the query that returns all the advertisers in range is:
SELECT *,
69.0412 * DEGREES(ACOS(COS(RADIANS(lat))
* COS(RADIANS(visitor_latitude))
* COS(RADIANS(lng - visitor_longitude))
+ SIN(RADIANS(lat))
* SIN(RADIANS(visitor_latitude)))) AS distance
FROM advertisers
WHERE distance <= radius

Select locations based on distance between geocodes using SQL query

I have an SQL database containing hotel information, some of which is the geocoded lat/lng generated by Googles geocoder.
I want to be able to select (directly using an SQL query) all the hotels within a certain range. This range will never be more than 50km so I dont need to go as detailed as alot of answers on here are suggesting (taking into account earth curvature and the fact its not a perfect sphere isnt an issue over the distances im searching).
Im thinking a simple Pythagorian formula would suffice, but I dont know what the latitude and longitude figures represent (and therefore how to convert to metres) and also ive read on a couple of 'simple' solutions to my problem that there are issues with their formulas and calculating distances between two locations either side of the meridian line (as I am based in London this will be a big issue for me!!)
Any help would be great, thankyou!
----Helpful Information-----
My database stores the geocoded data in the following format:
geo_lat: 51.5033630,
geo_lon; -0.1276250
This is a select clause that will get your distance into kilometers. From there you can use a where clause to filter it down to less than 25 kilometers or whatever you want. If you want it in miles just take off the * 1.609344 conversion.
$latitude = [current_latitude];
$longitude = [current_longitude];
SELECT
((((acos(sin((".$latitude."*pi()/180)) * sin((`geo_lat`*pi()/180))+cos((".$latitude."*pi()/180)) * cos((`geo_lat`*pi()/180)) * cos(((".$longitude."- `geo_lon`)* pi()/180))))*180/pi())*60*1.1515) * 1.609344) as distance
FROM
[table_name]
WHERE distance
You can use a simple map projection and straight distances for example equirectangular projection. In the formula on this website you can also use a simplier formula without the square root:http://www.movable-type.co.uk/scripts/latlong.html. Of course you can use a bounding box to filter the query:How to calculate the bounding box for a given lat/lng location?, https://gis.stackexchange.com/questions/19760/how-do-i-calculate-the-bounding-box-for-given-a-distance-and-latitude-longitude.

inside lat,lng bounds where query

I am getting lat/lng bounds from google map in client side through xhr to my php script. which is making an sql query to search location within that bounds. My table stores lat, lng as different columns. now how can I arrange my where queries such that only the points within the specified NE, SW bounds are returned.
In database its stored as number and I don't know if there already is any postgresql specific aggregate function to do the same.
Am I missing something? It's just a rectangular bounds check. It's not like you need to calculate the distance between the points.
WHERE lat BETWEEN lat_of_corner_a AND lat_of_corner_b
AND lon BETWEEN lon_of_corner_a AND lon_of_corner_b
I posted the working solution here:
Get all records from MySQL database that are within Google Maps .getBounds?
Alex's solution only works in USA, specifically in the north hemisphere, west region (were decreasing values fit the order in which Google provides the bouds values.
This one works for the other 3/4 of the world.
SELECT * FROM tilistings WHERE
(CASE WHEN a < c
THEN lat BETWEEN a AND c
ELSE lat BETWEEN c AND a
END)
AND
(CASE WHEN b < d
THEN lng BETWEEN b AND d
ELSE lng BETWEEN d AND b
END)
(See the linked post for more details)

Categories