PHP code example of alexpechkarev / geometry-library

1. Go to this page and download the library: Download alexpechkarev/geometry-library library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

alexpechkarev / geometry-library example snippets


composer 

"		....,
		"alexpechkarev/geometry-library":"1.0.5",
	
	},

$response =  \GeometryLibrary\SphericalUtil::computeHeading(
                ['lat' => 25.775, 'lng' => -80.190], // from array [lat, lng]
                ['lat' => 21.774, 'lng' => -80.190]); // to array [lat, lng]
  echo $response; // -180
  
$response = \GeometryLibrary\SphericalUtil::computeDistanceBetween(
              ['lat' => 25.775, 'lng' => -80.190], //from array [lat, lng]
              ['lat' => 21.774, 'lng' => -80.190]); // to array [lat, lng]
              
  echo $response; // 444891.52998049          
  
  
$response =  \GeometryLibrary\PolyUtil::isLocationOnEdge(
              ['lat' => 25.774, 'lng' => -80.190], // point array [lat, lng]
              [ // poligon arrays of [lat, lng]
                ['lat' => 25.774, 'lng' => -80.190], 
                ['lat' => 18.466, 'lng' => -66.118], 
                ['lat' => 32.321, 'lng' => -64.757]
              ])  ;
              
  echo $response; // true
  
  
  
$response =  \GeometryLibrary\PolyUtil::isLocationOnPath(
              ['lat' => 25.771, 'lng' => -80.190], // point array [lat, lng]
             [ // poligon arrays of [lat, lng]
              ['lat' => 25.774, 'lng' => -80.190], 
              ['lat' => 18.466, 'lng' => -66.118], 
              ['lat' => 32.321, 'lng' => -64.757]
             ]);  
             
  echo $response; // false  
  
$response =  \GeometryLibrary\PolyUtil::containsLocation(
              ['lat' => 23.886, 'lng' => -65.269], // point array [lat, lng]
             [ // poligon arrays of [lat, lng]
                ['lat' => 25.774, 'lng' => -80.190], 
                ['lat' => 18.466, 'lng' => -66.118], 
                ['lat' => 32.321, 'lng' => -64.757]
             ]);  
             
  echo $response; // false    
  
$response =  \GeometryLibrary\PolyUtil::distanceToLine(
              ['lat' => 61.387002, 'lng' => 23.890636], // point array [lat, lng]
              ['lat' => 61.487002, 'lng' => 23.790636], // line startpoint array [lat, lng]
              ['lat' => 60.48047, 'lng' => 22.052754] // line endpoint array [lat, lng]
             );  
             
  echo $response; // 12325.124046196 in meters
  
$response =  \GeometryLibrary\PolyUtil::encode(
              [ 
                ['lat' => 38.5, 'lng' => -120.2], 
                ['lat' => 40.7, 'lng' => -120.95], 
                ['lat' => 43.252, 'lng' => -126.453]
              ]);
              
  echo $response; // '_p~iF~ps|U_ulLnnqC_mqNvxq`@'
  
  
$response =  \GeometryLibrary\PolyUtil::decode('_p~iF~ps|U_ulLnnqC_mqNvxq`@');  

  echo $response; /** array (size=3)
                        0 => 
                          array (size=2)
                            'lat' => float 38.5
                            'lng' => float -120.2
                        1 => 
                          array (size=2)
                            'lat' => float 40.7
                            'lng' => float -120.95
                        2 => 
                          array (size=2)
                            'lat' => float 43.252
                            'lng' => float -126.453
                   */
  


$response =  \GeometryLibrary\PolyUtil::containsLocation(
              ['lat' => 23.886, 'lng' => -65.269], // point array [lat, lng]
             [ // poligon arrays of [lat, lng]
                ['lat' => 25.774, 'lng' => -80.190], 
                ['lat' => 18.466, 'lng' => -66.118], 
                ['lat' => 32.321, 'lng' => -64.757]
             ]);  
             
  echo $response; // false



$response =  \GeometryLibrary\PolyUtil::isLocationOnEdge(
              ['lat' => 25.774, 'lng' => -80.190], // point array [lat, lng]
              [ // poligon arrays of [lat, lng]
                ['lat' => 25.774, 'lng' => -80.190], 
                ['lat' => 18.466, 'lng' => -66.118], 
                ['lat' => 32.321, 'lng' => -64.757]
              ])  ;
              
  echo $response; // true



$response =  \GeometryLibrary\PolyUtil::isLocationOnPath(
              ['lat' => 25.774, 'lng' => -80.190], // point array [lat, lng]
              [ // poligon arrays of [lat, lng]
                ['lat' => 25.774, 'lng' => -80.190], 
                ['lat' => 18.466, 'lng' => -66.118], 
                ['lat' => 32.321, 'lng' => -64.757]
              ])  ;
              
  echo $response; // true



$response =  \GeometryLibrary\PolyUtil::distanceToLine(
              ['lat' => 61.387002, 'lng' => 23.890636], // point array [lat, lng]
              ['lat' => 61.487002, 'lng' => 23.790636], // line startpoint array [lat, lng]
              ['lat' => 60.48047, 'lng' => 22.052754] // line endpoint array [lat, lng]
             );  
             
  echo $response; // 12325.124046196 in meters



$response =  \GeometryLibrary\PolyUtil::decode('_p~iF~ps|U_ulLnnqC_mqNvxq`@');  

  echo $response; /** array (size=3)
                        0 => 
                          array (size=2)
                            'lat' => float 38.5
                            'lng' => float -120.2
                        1 => 
                          array (size=2)
                            'lat' => float 40.7
                            'lng' => float -120.95
                        2 => 
                          array (size=2)
                            'lat' => float 43.252
                            'lng' => float -126.453
                   */



$response =  \GeometryLibrary\PolyUtil::encode(
              [ 
                ['lat' => 38.5, 'lng' => -120.2], 
                ['lat' => 40.7, 'lng' => -120.95], 
                ['lat' => 43.252, 'lng' => -126.453]
              ]);
              
  echo $response; // '_p~iF~ps|U_ulLnnqC_mqNvxq`@'



$response =  \GeometryLibrary\SphericalUtil::computeHeading(
              ['lat' => 25.775, 'lng' => -80.190], 
              ['lat' => 21.774, 'lng' => -80.190]));
              
  echo $response; // -180



$response =  \GeometryLibrary\SphericalUtil::computeOffset(['lat' => 25.775, 'lng' => -80.190], 152, 120);
              
  echo $response; /** array (size=2)
                      'lat' => float 25.774316510639
                      'lng' => float -80.188685385944
                  */



$response =  \GeometryLibrary\SphericalUtil::computeOffsetOrigin(['lat' => 25.775, 'lng' => -80.190], 152, 120);
              
  echo $response; /** array (size=2)
                        'lat' => float 14.33435503928
                        'lng' => float -263248.24242931
                  */



$response =  \GeometryLibrary\SphericalUtil::interpolate(['lat' => 25.775, 'lng' => -80.190], 
                                                          ['lat' => 26.215, 'lng' => -81.218], 2);
              
  echo $response; /** array (size=2)
                      'lat' => float 26.647635362403
                      'lng' => float -82.253737943391
                  */



$response =  \GeometryLibrary\SphericalUtil::computeDistanceBetween(['lat' => 25.775, 'lng' => -80.190], ['lat' => 26.215, 'lng' => -81.218]);
              
  echo $response; //float 113797.92421349



$response =  \GeometryLibrary\SphericalUtil::computeLength([ 
                ['lat' => 38.5, 'lng' => -120.2], 
                ['lat' => 40.7, 'lng' => -120.95], 
                ['lat' => 43.252, 'lng' => -126.453]
              ]);
              
  echo $response; //float 788906.98459431



$response =  \GeometryLibrary\SphericalUtil::computeArea([ 
                ['lat' => 38.5, 'lng' => -120.2], 
                ['lat' => 40.7, 'lng' => -120.95], 
                ['lat' => 43.252, 'lng' => -126.453]
              ]);
              
  echo $response; //float 44766785529.143



$response =  \GeometryLibrary\SphericalUtil::computeSignedArea([ 
                ['lat' => 38.5, 'lng' => -120.2], 
                ['lat' => 40.7, 'lng' => -120.95], 
                ['lat' => 43.252, 'lng' => -126.453]
              ]);
              
  echo $response; //float 44766785529.143