Created
April 24, 2017 08:15
-
-
Save mohammadmursaleen/adad09ceb597888130f5aa5d741bbee1 to your computer and use it in GitHub Desktop.
PHP function to get markers within certain radius
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @author Mohammad Mursaleen | |
* @usange to get array of all markers within certain radius | |
* @param $coordinateArray | |
* @param $center | |
* @param $radius | |
* @return array | |
*/ | |
function mm_getCoordinatesWithinRadius ( $lat1 , $lng1 , $radius , $available_markers ) { | |
$stores_data = $available_markers; | |
$resultArray= array(); | |
foreach ( $stores_data as $store ) { | |
$lat2 = $store->lat; | |
$lng2 = $store->lng; | |
$distance = 3959 * acos(cos(mm_radians($lat1)) * cos(mm_radians($lat2)) * cos(mm_radians($lng2) - mm_radians($lng1)) + sin(mm_radians($lat1)) * sin(mm_radians($lat2))); | |
if ($distance < $radius){ | |
$resultArray[] = (object) array( 'ID' => $store->ID , 'lat' => $store->lat , 'lng' => $store->lng , 'distance' => $distance ); | |
} | |
} | |
// need to return id,distance,lat and lng | |
return $resultArray; | |
} | |
/** | |
* @usage to convert degree into radians | |
* @param $deg | |
* @return float | |
*/ | |
function mm_radians($deg) { | |
return $deg * M_PI / 180; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment