Skip to content

Instantly share code, notes, and snippets.

@mohammadmursaleen
Created April 24, 2017 08:15
Show Gist options
  • Save mohammadmursaleen/adad09ceb597888130f5aa5d741bbee1 to your computer and use it in GitHub Desktop.
Save mohammadmursaleen/adad09ceb597888130f5aa5d741bbee1 to your computer and use it in GitHub Desktop.
PHP function to get markers within certain radius
<?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