Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Michael R Lundquist
zipAPI
Commits
be8254ef
Commit
be8254ef
authored
Nov 13, 2018
by
michael lundquist
Browse files
The API does what it's supposed to do, it would be smart to add error checking.
parent
b32bce2e
Changes
1
Hide whitespace changes
Inline
Side-by-side
API.php
View file @
be8254ef
...
...
@@ -11,35 +11,46 @@ function longLatZip($zip){
$param
=
Array
(
0
=>
$zip
);
$result
=
resultQuery
(
$sql
,
$param
);
$retVal
=
False
;
$retVal
=
False
;
//delete this line
if
(
$result
->
num_rows
>
0
){
$retVal
=
$result
->
fetch_assoc
();
$result
->
close
();
//return here
}
else
{
//throw an error here
}
return
$retVal
;
}
/*
calculating
Finds all the zip codes within maxDistance miles of zip.
https://stackoverflow.com/questions/24370975/find-distance-between-two-points-using-latitude-and-longitude-in-mysql/24372831#24372831
*/
function
nearbyZips
(
$zip
,
$maxDistance
){
$zipArr
=
longLatZip
(
$zip
);
if
(
!
$zipArr
)
return
false
;
$sql
=
"SELECT a.city AS from_city, b.city AS to_city,
69.0 *
DEGREES(ACOS(COS(RADIANS(a.Latitude))
* COS(RADIANS(b.Latitude))
* COS(RADIANS(a.Longitude - b.Longitude))
+ SIN(RADIANS(a.Latitude))
* SIN(RADIANS(b.Latitude)))) AS distance_in_mi
FROM city AS a
JOIN city AS b ON a.id <> b.id
WHERE a.city = 3 AND b.city = 7"
;
$params
=
[];
$sql
=
"SELECT P AS zipCode,
ROUND(
69.0 *
DEGREES(ACOS(COS(RADIANS(LAT))
* COS(RADIANS(?))
* COS(RADIANS(LNG - ?))
+ SIN(RADIANS(LAT))
* SIN(RADIANS(?))))
,2) AS distance_in_mi
FROM zip
HAVING distance_in_mi <= ?"
;
$params
=
[
$zipArr
[
"LAT"
],
$zipArr
[
"LNG"
],
$zipArr
[
"LAT"
],
$maxDistance
];
$result
=
resultQuery
(
$sql
,
$params
);
$retVal
=
[];
while
(
$row
=
$result
->
fetch_assoc
())
{
$retVal
[]
=
$row
;
}
$result
->
close
();
return
$retVal
;
}
print_r
(
longLatZip
(
20194
));
?>
HELLO WORLD!
\ No newline at end of file
if
(
isset
(
$_GET
[
"zip"
])
&&
isset
(
$_GET
[
"dist"
])){
echo
json_encode
(
nearbyZips
(
$_GET
[
'zip'
],
$_GET
[
"dist"
]));
}
?>
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment