Reputation: 442
I have an issue which i can't wrap my head around :)
Basically i have a php / mysql based system and a table which contains a set of points in lat/lng.
On the other end i have an iOS app with Google Maps and when the camera position changes i submit the top/left, bottom/right points of the current area the user is observing (this is what i need, i don't need the visual points of the map).
I need to be able to find a specific subset of those points by given a bounding 'box' - a rectangular area on earth - with a top/left, bottom/right point.
This works - no issues with it.
BUT when the map is rotated freely - things go completely wrong as the orientation of the top/left, bottom/right points is changed and ofcourse .. the mysql query finds nothing.
Now .. it came to my mind that the fix would be to rotate the rectangular area as well - but the problem is the user can freely rotate the map and at one moment the rectangular area will no longer be a rectangular and i am not even sure how to fetch the points at this stage :)
Has anyone had this kind of issue and how did you solve it?
PS: Yes the easiest way is to use a circular area and a given center point but .. my client is calling it... :/
Upvotes: 2
Views: 2110
Reputation: 7228
First create bounding box containing rotated rectangle and send the coordinates of bounding box to php/MySQL, Then use pointInPolygon() to eliminate points outside rotated rectangle.
javascript code for sending coordinates of bounding box to php/MySQL file
function searchLocations(){
var bounds = new google.maps.LatLngBounds();
var url = "dbtoJSON.php";
url +="?maxLat="+maxLat +"&minLat="+minLat +"&maxLng="+maxLng +"&minLng="+minLng;
$.getJSON(url,function(data) {
$.each(data.marker,function(i,dat){
if (pointInPolygon(polySides,polyLat,polyLng,dat.lat,dat.lng)){
var latlng = new google.maps.LatLng(dat.lat,dat.lng);
addMarker(latlng,dat.name);
bounds.extend(latlng);
}//
});
map.fitBounds(bounds);
});
}
function pointInPolygon(polySides,polyX,polyY,x,y) {
var j = polySides-1 ;
oddNodes = 0;
for (i=0; i<polySides; i++) {
if (polyY[i]<y && polyY[j]>=y || polyY[j]<y && polyY[i]>=y) {
if (polyX[i]+(y-polyY[i])/(polyY[j]-polyY[i])*(polyX[j]-polyX[i])<x) {
oddNodes=!oddNodes;
}
}
j=i; }
return oddNodes;
}
php/MySQL file dbtoJSON.php
// Get parameters from URL
$maxLat = $_GET["maxLat"];
$minLat = $_GET["minLat"];
$maxLng = $_GET["maxLng"];
$minLng = $_GET["minLng"];
//array to hold location array
$arr = array();
// Prepare statement
$stmt = $dbh->prepare("SELECT name, lat, lng FROM gbstn WHERE (lat BETWEEN ? AND ? )AND (lng BETWEEN ? AND ?)");
// Assign parameters
$stmt->bindParam(1,$minLat);
$stmt->bindParam(2,$maxLat);
$stmt->bindParam(3,$minLng);
$stmt->bindParam(4,$maxLng);
//Execute query
$stmt->setFetchMode(PDO::FETCH_OBJ);
$stmt->execute();
//Show the results
while($obj = $stmt->fetch()) {
$arr[] = $obj;
}
if (count($arr) >= 1)
{
echo '{"marker":'.json_encode($arr).'}';
}
Upvotes: 2
Reputation: 1933
Step 1. You have rotated rect with 4 points. Choose max and min coordinates from them, it will be min_latitude, max_latitude, min_longitude, max_longitude. With these values you can draw normal rectangular area and get needed results from you base.
Step 2. In received results you will have points that are placed in calculated rect and in rotated rect, but some points will be outside rotated rect but still in normal rect. So exclude wrong points and you will have points that fit to your rotated rect.
Upvotes: 1
Reputation: 93
if you the angle of rotation you should be able to define the rotation matrix and apply that to the original 4 points that define the rectangle.
Upvotes: -1