Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Tam committed Oct 30, 2017
2 parents 8cfe0e6 + c10432c commit 9cb1b37
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 12 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ var map = new mapboxgl.Map({

## Changelog

### 1.7.0
- Improved the efficiency of the location search query.

### 1.6.3
- The field is now previewable in the entries table (via @joshuabaker)
- Fixed a bug where `SimpleMap_MapModel` `__toString()` would't always return a string.
Expand Down
8 changes: 8 additions & 0 deletions releases.json
Original file line number Diff line number Diff line change
Expand Up @@ -166,5 +166,13 @@
"[Improved] The field is now previewable in the entries table (via @joshuabaker)",
"[Fixed] Fixed a bug where `SimpleMap_MapModel` `__toString()` would't always return a string"
]
},
{
"version": "1.7.0",
"downloadUrl": "https://github.com/ethercreative/simplemap/archive/v1.7.0.zip",
"date": "2017-10-30T13:21:00-08:00",
"notes": [
"[Improved] Improved the efficiency of the location search query."
]
}
]
4 changes: 2 additions & 2 deletions simplemap/SimpleMapPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public function getDescription()

public function getVersion()
{
return '1.6.3';
return '1.7.0';
}

public function getSchemaVersion()
{
return '0.0.6';
return '0.0.7';
}

public function getDeveloper()
Expand Down
22 changes: 22 additions & 0 deletions simplemap/migrations/m171027_173900_simpleMap_addLatLngIndexes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Craft;

class m171027_173900_simpleMap_addLatLngIndexes extends BaseMigration {

public function safeUp ()
{
$this->createIndex(
SimpleMap_MapRecord::TABLE_NAME,
'lat'
);

$this->createIndex(
SimpleMap_MapRecord::TABLE_NAME,
'lng'
);

return true;
}

}
4 changes: 3 additions & 1 deletion simplemap/records/SimpleMap_MapRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ public function defineIndexes()
{
return array(
array('columns' => array('ownerId')),
array('columns' => array('fieldId'))
array('columns' => array('fieldId')),
array('columns' => array('lat')),
array('columns' => array('lng')),
);
}

Expand Down
4 changes: 2 additions & 2 deletions simplemap/resources/SimpleMap_Map.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@
this.settings.lat,
this.settings.lng
),
mapTypeId: google.maps.MapTypeId.ROADMAP
mapTypeId: google.maps.MapTypeId.ROADMAP,
});

this.setupAutoComplete();
Expand Down Expand Up @@ -546,4 +546,4 @@
*/

window.SimpleMap = SimpleMap;
})(window);
})(window);
55 changes: 48 additions & 7 deletions simplemap/services/SimpleMapService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class SimpleMapService extends BaseApplicationComponent {

public $searchLatLng;
public $searchEarthRad;
public $searchDistanceUnit;

// Public
// =========================================================================
Expand Down Expand Up @@ -149,11 +150,11 @@ public function modifyQuery (DbCommand &$query, $params = array())
private function _searchLocation (DbCommand &$query, $params)
{
$location = $params['location'];
$radius = array_key_exists('radius', $params) ? $params['radius'] : 50;
$radius = array_key_exists('radius', $params) ? $params['radius'] : 50.0;
$unit = array_key_exists('unit', $params) ? $params['unit'] : 'kilometers';

if (!is_numeric($radius)) $radius = (float)$radius;
if (!is_numeric($radius)) $radius = 50;
if (!is_numeric($radius)) $radius = 50.0;

if (!in_array($unit, array('km', 'mi'))) $unit = 'km';

Expand All @@ -172,18 +173,46 @@ private function _searchLocation (DbCommand &$query, $params)
return;
}

if ($unit == 'km') $earthRad = 6371;
else $earthRad = 3959;
if ($unit == 'km') $distanceUnit = 111.045;
else $distanceUnit = 69.0;

$this->searchLatLng = $location;
$this->searchEarthRad = $earthRad;
$this->searchDistanceUnit = $distanceUnit;

$table = craft()->db->tablePrefix . SimpleMap_MapRecord::TABLE_NAME;

$haversine = "($earthRad * acos(cos(radians($location[lat])) * cos(radians($table.lat)) * cos(radians($table.lng) - radians($location[lng])) + sin(radians($location[lat])) * sin(radians($table.lat))))";
$haversine = "
(
$distanceUnit
* DEGREES(
ACOS(
COS(RADIANS($location[lat]))
* COS(RADIANS($table.lat))
* COS(RADIANS($location[lng]) - RADIANS($table.lng))
+ SIN(RADIANS($location[lat]))
* SIN(RADIANS($table.lat))
)
)
)
";

$restrict = [
'and',
[
'and',
"$table.lat >= $location[lat] - ($radius / $distanceUnit)",
"$table.lat <= $location[lat] + ($radius / $distanceUnit)",
],
[
'and',
"$table.lng >= $location[lng] - ($radius / ($distanceUnit * COS(RADIANS($location[lat]))))",
"$table.lng <= $location[lng] + ($radius / ($distanceUnit * COS(RADIANS($location[lat]))))",
]
];

$query
->addSelect($haversine . ' AS distance')
->andWhere($restrict)
->having('distance <= ' . $radius);
}

Expand Down Expand Up @@ -295,7 +324,19 @@ private function _calculateDistance (SimpleMap_MapModel $model)
$lt2 = $model->lat;
$ln2 = $model->lng;

return ($this->searchEarthRad * acos(cos(deg2rad($lt1)) * cos(deg2rad($lt2)) * cos(deg2rad($ln2) - deg2rad($ln1)) + sin(deg2rad($lt1)) * sin(deg2rad($lt2))));
return (
$this->searchDistanceUnit
* rad2deg(
acos(
cos(deg2rad($lt1))
* cos(deg2rad($lt2))
* cos(deg2rad($ln1) - deg2rad($ln2))
+ sin(deg2rad($lt1))
* sin(deg2rad($lt2))
)
)
);
// return ($this->searchEarthRad * acos(cos(deg2rad($lt1)) * cos(deg2rad($lt2)) * cos(deg2rad($ln2) - deg2rad($ln1)) + sin(deg2rad($lt1)) * sin(deg2rad($lt2))));
}

private function _formatLocaleForMap ($locale) {
Expand Down

0 comments on commit 9cb1b37

Please sign in to comment.