youkaihiei's avatar

Calculating sectors of circles around a latitude & longitude w/ PHP

Hey everybody! Figured I'd try asking this here as a couple of days of wandering around stackoverflow hasn't proven very successful.

I'm basically building an angularjs front end/laravel back end application and want to know if what I'm currently using the Google Maps API for can be done with a PHP package or something.

I'm using it to draw sectors around a latitude and longitude (for example, two sectors would be two halves of a circle) and then determine whether or not another latitude and longitude falls inside of which sector.

0 likes
2 replies
noeldiaz's avatar

@youkaihiei I'm not sure I am following what you are doing (so this might be way off) but I am using this code below to figure out locations (using lat/long) that are within a certain distance of a point. This is being used to show properties within a certain number of miles of a lat/long combo. I'm pulling the values from a database table with coordinates:

select id, latitude, longitude,
((ACOS(SIN(" . $lat . " * PI() / 180) *
SIN(latitude * PI() / 180) + COS(" . $lat . " * PI() / 180) *
COS(latitude * PI() / 180) * COS((" . $long . " - longitude) *
PI() / 180)) * 180 / PI()) * 60 * 1.1515) AS distance
from coordinates
having distance <= " . $distance . "
order by distance asc

Distance variable is by miles. You can use a percentage less than 1 for fraction of a mile. So far it is working for my use case.

youkaihiei's avatar

@noeldiaz Thanks for the response! Its related but not necessarily what I'm after.

Forgive the photoshop skills haha but http://www.nathanielthomas.net/images/ltlngexample.jpg

The white dot in the middle is a latitude/longitude. A and B are each sectors of the circle made up of the radius around the circle (lets say 5 miles), so A is 0 degrees to 180 degrees and B is 180 degrees to 360 degrees, 5 miles away from the middle. The yellow dot is another latitude/longitude.

Right now I have this generated using the google maps api using the polygon plugin and the geometry plugin to find out if a given latitude/longitude falls inside of a polygon.

I'm trying to find out if theres an equivalent way to do that sort of thing in PHP.

Please or to participate in this conversation.