Add nearby merchant feature

master
Skylar Ittner 5 years ago
parent 501d9cad07
commit 33e375299c

@ -0,0 +1,105 @@
<?php
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
use AnthonyMartin\GeoLocation\GeoLocation as GeoLocation;
$userlocation = GeoLocation::fromDegrees($VARS["latitude"], $VARS["longitude"]);
$radius = 2;
if (!empty($VARS["radius"])) {
$radius = min(10.0, $VARS["radius"] * 1.0);
}
$searchbounds = $userlocation->boundingCoordinates($radius, "miles");
ob_flush();
$people = $database->debug()->select("merchants", [
"merchantid",
"name",
"address",
"latitude",
"longitude"
], [
"AND" => [
'latitude[<>]' => [$searchbounds[0]->getLatitudeInDegrees(), $searchbounds[1]->getLatitudeInDegrees()],
'longitude[<>]' => [$searchbounds[0]->getLongitudeInDegrees(), $searchbounds[1]->getLongitudeInDegrees()]
],
"LIMIT" => 100
]
);
$query = ob_get_contents();
ob_clean();
$merchants = $database->query($query)->fetchAll();
// Get a list of the merchant types
for ($i = 0; $i < count($merchants); $i++) {
$types = $database->select(
"merchant_type",
["[>]merchant_types" => "typeid"],
["merchant_type.typeid (id)", "type (name)", "icon"],
["merchantid" => $merchants[$i]["merchantid"]]
);
$merchants[$i]["types"] = $types;
}
if (!empty($VARS["format"]) && $VARS["format"] == "geojson") {
$geojson = [
"name" => "Nearby Merchants",
"type" => "FeatureCollection",
"features" => []
];
foreach ($merchants as $merchant) {
$geojson["features"][] = [
"type" => "Feature",
"geometry" => [
"type" => "Point",
"coordinates" => [
$merchant["longitude"] * 1.0,
$merchant["latitude"] * 1.0
]
],
"properties" => [
"id" => $merchant["merchantid"],
"name" => utf8_encode($merchant["name"]),
"address" => utf8_encode($merchant["address"]),
"types" => $merchant["types"]
]
];
}
exitWithJson($geojson);
}
$nearby = [];
foreach ($merchants as $merchant) {
$nearby[] = [
"name" => utf8_encode($merchant["name"]),
"merchantid" => $merchant["merchantid"],
"address" => utf8_encode($merchant["address"]),
"types" => $merchant["types"],
"latitude" => $merchant["latitude"] * 1.0,
"longitude" => $merchant["longitude"] * 1.0
];
}
exitWithJson([
"status" => "OK",
"radius" => $radius,
"bounds" => [
0 => [
"latitude" => $searchbounds[0]->getLatitudeInDegrees(),
"longitude" => $searchbounds[0]->getLongitudeInDegrees()
],
1 => [
"latitude" => $searchbounds[1]->getLatitudeInDegrees(),
"longitude" => $searchbounds[1]->getLongitudeInDegrees()
],
],
"nearby" => $nearby
]);

@ -84,6 +84,16 @@ $APIS = [
"format (optional)" => "/(geojson)/"
]
],
"getmerchants" => [
"load" => "merchants.php",
"vars" => [
"key" => $keyregex,
"latitude" => "/-?[0-9]{2}\.[0-9]+/",
"longitude" => "/-?[0-9]{2,3}\.[0-9]+/",
"radius (optional)" => "/[0-9]*\.?[0-9]+/",
"format (optional)" => "/(geojson)/"
]
],
"broadcast" => [
"load" => "broadcast.php",
"vars" => [

Binary file not shown.
Loading…
Cancel
Save