Skip to content

Commit

Permalink
Merge branch 'v3-dev' of https://github.com/ethercreative/simplemap i…
Browse files Browse the repository at this point in the history
…nto v3-dev
  • Loading branch information
Tam committed Mar 5, 2018
2 parents afe50fe + bcbe896 commit 9bda959
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions src/services/MapService.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,22 +257,23 @@ public function modifyElementsQuery (ElementQueryInterface $query, $value)
/** @var ElementQuery $query */

$tableName = MapRecord::$tableName;
$tableAlias = unqid('simplemap');

$on = [
'and',
'[[elements.id]] = [[simplemap.ownerId]]',
'[[elements_sites.siteId]] = [[simplemap.ownerSiteId]]',
'[[elements.id]] = [['.$tableAlias.'.ownerId]]',
'[[elements_sites.siteId]] = [['.$tableAlias.'.ownerSiteId]]',
];

$query->query->join(
'JOIN',
"{$tableName} simplemap",
"{$tableName} {$tableAlias}",
$on
);

$query->subQuery->join(
'JOIN',
"{$tableName} simplemap",
"{$tableName} {$tableAlias}",
$on
);

Expand All @@ -282,7 +283,7 @@ public function modifyElementsQuery (ElementQueryInterface $query, $value)
}

if (array_key_exists('location', $value)) {
$this->_searchLocation($query, $value);
$this->_searchLocation($query, $value, $tableAlias);
} else if (array_key_exists('distance', $query->orderBy)) {
$this->_replaceOrderBy($query);
}
Expand Down Expand Up @@ -501,7 +502,7 @@ private function _padParts (Map $model)
*
* @throws Exception
*/
private function _searchLocation (ElementQuery $query, $value)
private function _searchLocation (ElementQuery $query, $value, $tableAlias)
{
$location = $value['location'];
$country = array_key_exists('country', $value)
Expand Down Expand Up @@ -552,7 +553,7 @@ private function _searchLocation (ElementQuery $query, $value)
ACOS(
COS(RADIANS($location[lat]))
* COS(RADIANS([[simplemap.lat]]))
* COS(RADIANS($location[lng]) - RADIANS([[simplemap.lng]]))
* COS(RADIANS($location[lng]) - RADIANS([[$tableAlias.lng]]))
+ SIN(RADIANS($location[lat]))
* SIN(RADIANS([[simplemap.lat]]))
)
Expand All @@ -565,13 +566,13 @@ private function _searchLocation (ElementQuery $query, $value)
'and',
[
'and',
"[[simplemap.lat]] >= $location[lat] - ($radius / $distanceUnit)",
"[[simplemap.lat]] <= $location[lat] + ($radius / $distanceUnit)",
"[[$tableAlias.lat]] >= $location[lat] - ($radius / $distanceUnit)",
"[[$tableAlias.lat]] <= $location[lat] + ($radius / $distanceUnit)",
],
[
'and',
"[[simplemap.lng]] >= $location[lng] - ($radius / ($distanceUnit * COS(RADIANS($location[lat]))))",
"[[simplemap.lng]] <= $location[lng] + ($radius / ($distanceUnit * COS(RADIANS($location[lat]))))",
"[[$tableAlias.lng]] >= $location[lng] - ($radius / ($distanceUnit * COS(RADIANS($location[lat]))))",
"[[$tableAlias.lng]] <= $location[lng] + ($radius / ($distanceUnit * COS(RADIANS($location[lat]))))",
]
];

Expand Down Expand Up @@ -603,4 +604,4 @@ private function _replaceOrderBy (ElementQuery $query, $distanceSearch = false)
$query->orderBy($nextOrder);
}

}
}

0 comments on commit 9bda959

Please sign in to comment.