Overview

Namespaces

  • LeanCloud
    • Engine
    • Operation
    • Storage
    • Uploader

Classes

  • LeanCloud\ACL
  • LeanCloud\AppRouter
  • LeanCloud\Bytes
  • LeanCloud\Client
  • LeanCloud\Engine\Cloud
  • LeanCloud\Engine\LaravelEngine
  • LeanCloud\Engine\LeanEngine
  • LeanCloud\Engine\SlimEngine
  • LeanCloud\File
  • LeanCloud\GeoPoint
  • LeanCloud\LeanObject
  • LeanCloud\MIMEType
  • LeanCloud\Operation\ArrayOperation
  • LeanCloud\Operation\DeleteOperation
  • LeanCloud\Operation\IncrementOperation
  • LeanCloud\Operation\RelationOperation
  • LeanCloud\Operation\SetOperation
  • LeanCloud\Push
  • LeanCloud\Query
  • LeanCloud\Region
  • LeanCloud\Relation
  • LeanCloud\Role
  • LeanCloud\RouteCache
  • LeanCloud\SaveOption
  • LeanCloud\SMS
  • LeanCloud\Storage\CookieStorage
  • LeanCloud\Storage\SessionStorage
  • LeanCloud\Uploader\QCloudUploader
  • LeanCloud\Uploader\QiniuUploader
  • LeanCloud\Uploader\S3Uploader
  • LeanCloud\Uploader\SimpleUploader
  • LeanCloud\User

Interfaces

  • LeanCloud\Operation\IOperation
  • LeanCloud\Storage\IStorage

Exceptions

  • LeanCloud\BatchRequestError
  • LeanCloud\CloudException
  • LeanCloud\Engine\FunctionError
  • Overview
  • Namespace
  • Class
  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 
<?php

namespace LeanCloud;

/**
 * GeoPoint type representation
 *
 * It represents a geographic point, and supports computing geo
 * distance from point to point. It can also be used in Query to
 * build proximity-based queries.
 *
 * @see Query
 */
class GeoPoint {
    /**
     * @var number
     */
    private $latitude;

    /**
     * @var number
     */
    private $longitude;

    /**
     * Initialize a geo point
     *
     * @param number $latitude
     * @param number $longitude
     * @throws InvalidArgumentException
     */
    public function __construct($latitude=0.0, $longitude=0.0) {
        if ($latitude <= 90.0 && $latitude >= -90.0 &&
            $longitude <= 180.0 && $longitude >= -180.0) {
            $this->latitude  = $latitude;
            $this->longitude = $longitude;
        } else {
            throw new \InvalidArgumentException("Invalid latitude or " .
                                                "longitude for geo point");
        }
    }

    /**
     * @return number
     */
    public function getLatitude() {
        return $this->latitude;
    }

    /**
     * @return number
     */
    public function getLongitude() {
        return $this->longitude;
    }

    /**
     * Compute distance (in radians) to a geo point
     *
     * @param GeoPoint $point Other geo point
     * @return number
     */
    public function radiansTo(GeoPoint $point) {
        $d2r = M_PI / 180.0;
        $lat1rad = $this->getLatitude() * $d2r;
        $lon1rad = $this->getLongitude() * $d2r;
        $lat2rad = $point->getLatitude() * $d2r;
        $lon2rad = $point->getLongitude() * $d2r;
        $deltaLat = $lat1rad - $lat2rad;
        $deltaLon = $lon1rad - $lon2rad;
        $sinLat = sin($deltaLat / 2);
        $sinLon = sin($deltaLon / 2);
        $a = $sinLat * $sinLat +
           cos($lat1rad) * cos($lat2rad) * $sinLon * $sinLon;
        $a = min(1.0, $a);
        return 2 * asin(sqrt($a));
    }

    /**
     * Compute distance (in kilometers) to other geo point
     *
     * @param GeoPoint $point Other geo point
     * @return number
     */
    public function kilometersTo(GeoPoint $point) {
        return $this->radiansTo($point) * 6371.0;
    }

    /**
     * Compute distance (in miles) to other geo point
     *
     * @param GeoPoint $point Other geo point
     * @return number
     */
    public function milesTo(GeoPoint $point) {
        return $this->radiansTo($point) * 3958.8;
    }

    public function encode() {
        return array(
            '__type'    => 'GeoPoint',
            'latitude'  => $this->latitude,
            'longitude' => $this->longitude
        );
    }
}
API documentation generated by ApiGen