Created
November 15, 2017 08:29
-
-
Save tnqsoft/94e9d45dbf6d5512f02be934fa7d7766 to your computer and use it in GitHub Desktop.
Google Map get distance from latitude and longitude
This file contains 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
DELIMITER $$ | |
USE `fashino`$$ | |
DROP FUNCTION IF EXISTS `GeoCalDistanceByLatLon`$$ | |
CREATE DEFINER=`root`@`localhost` FUNCTION `GeoCalDistanceByLatLon`( TYPE ENUM('mi', 'km'), lat1 DECIMAL(10,7), lon1 DECIMAL(10,7), lat2 DECIMAL(10,7), lon2 DECIMAL(10,7) ) RETURNS DECIMAL(10,7) | |
BEGIN | |
RETURN IF(TYPE = 'km', 6371, 3959)*ACOS(SIN(lat1*PI()/180)*SIN(lat2*PI()/180)+COS(lat1*PI()/180)*COS(lat2*PI()/180)*COS((lon2*PI()/180)-(lon1*PI()/180))); | |
END$$ | |
DELIMITER ; |
This file contains 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
DELIMITER $$ | |
USE `fashino`$$ | |
DROP FUNCTION IF EXISTS `GeoDistDiff`$$ | |
CREATE DEFINER=`root`@`localhost` FUNCTION `GeoDistDiff`( TYPE ENUM('mi', 'km'), lat1 DECIMAL(10,7), lon1 DECIMAL(10,7), lat2 DECIMAL(10,7), lon2 DECIMAL(10,7) ) RETURNS DECIMAL(10,7) | |
BEGIN | |
RETURN ( IF(TYPE = 'km', 6371, 3959) * ACOS( COS( RADIANS(lat2) ) * COS( RADIANS( lat1 ) ) * COS( RADIANS( lon1 ) - RADIANS(lon2) ) + SIN( RADIANS(lat2) ) * SIN( RADIANS( lat1 ) ) ) ); | |
END$$ | |
DELIMITER ; |
This file contains 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 | |
namespace CoreBundle\DQL; | |
use \Doctrine\ORM\Query\AST\Functions\FunctionNode; | |
use \Doctrine\ORM\Query\Lexer; | |
use \Doctrine\ORM\Query\SqlWalker; | |
use \Doctrine\ORM\Query\Parser; | |
/** | |
* Symfony DQL Custom Functions | |
* http://www.doctrine-project.org/api/orm/2.5/source-class-Doctrine.ORM.Query.Lexer.html#33 | |
*/ | |
class GeoDistDiffFunction extends FunctionNode | |
{ | |
public $type = null; | |
public $lat1 = null; | |
public $lon1 = null; | |
public $lat2 = null; | |
public $lon2 = null; | |
public $parameters = array(); | |
public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker) | |
{ | |
return sprintf( | |
'GeoDistDiff(%s, %s, %s, %s, %s)', | |
$this->type->dispatch($sqlWalker), | |
$this->lat1->dispatch($sqlWalker), | |
$this->lon1->dispatch($sqlWalker), | |
$this->lat2->dispatch($sqlWalker), | |
$this->lon2->dispatch($sqlWalker) | |
); | |
} | |
public function parse(\Doctrine\ORM\Query\Parser $parser) | |
{ | |
$parser->match(Lexer::T_IDENTIFIER); | |
$parser->match(Lexer::T_OPEN_PARENTHESIS); | |
$this->type = $parser->StringPrimary(); | |
$parser->match(Lexer::T_COMMA); | |
$this->lat1 = $parser->StringPrimary(); | |
$parser->match(Lexer::T_COMMA); | |
$this->lon1 = $parser->StringPrimary(); | |
$parser->match(Lexer::T_COMMA); | |
$this->lat2 = $parser->SimpleArithmeticExpression(); | |
$parser->match(Lexer::T_COMMA); | |
$this->lon2 = $parser->SimpleArithmeticExpression(); | |
$parser->match(Lexer::T_CLOSE_PARENTHESIS); | |
} | |
} |
This file contains 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
// Use | |
$query->addSelect("GeoDistDiff(:type, :latitude, :longitude, s.locationLatitude, s.locationLongitude) AS distance") | |
->setParameter('type', 'km') | |
->setParameter('latitude', $latitude) | |
->setParameter('longitude', $longitude); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment