Verify the php code of the coordinates in a coordinate area,

Source: Internet
Author: User

Verify the php code of the coordinates in a coordinate area,

The previous requirement was to display the company's services on a map, the user's daily access coordinates are calculated to see how many users are within the radius of the service range that can be reached.

The following is the implementation of the PHP code (only verify that the coordinates are in a coordinate area)

<? Php/*** verify whether the coordinate point is in a certain area * @ author xiaoliang <1058436713@qq.com> * Class validationMap */class validationMap {private static $ coordArray; private static $ vertx = []; private static $ verty = [];/*** set the coordinate Region * @ param mixed $ coordArray */public static function setCoordArray (array $ coordArray) {self: $ coordArray = $ coordArray;}/*** verification region range * @ param array $ coordArray * @ return bool */public static fu Nction isCityCenter (array $ coordArray) {if (! Self: vaildatePoint ($ coordArray) {return false;} return self: pnpoly (count (self: $ coordArray), $ coordArray ['lng '], $ coordArray ['lat']);} /*** compare Region coordinates ** @ param $ nvert * @ param $ testx * @ param $ testy * @ return bool */private static function pnpoly ($ nvert, $ testx, $ testy) {$ c = false; for ($ I = 0, $ j = $ nvert-1; $ I <$ nvert; $ j = $ I ++) {if (self ::$ verty [$ I]> $ testy )! = (Self ::$ verty [$ j]> $ testy) & ($ testx <(self ::$ vertx [$ j]-self :: $ vertx [$ I]) * ($ testy-self: $ verty [$ I])/(self ::$ verty [$ j]-self :: $ verty [$ I]) + self: $ vertx [$ I]) $ c =! $ C;} return $ c;}/*** verify coordinate * @ param array $ pointArray * @ return bool */private static function vaildatePoint (array $ pointArray) {$ maxY = $ maxX = 0; $ minY = $ minX = 9999; foreach (self: $ coordArray as $ item) {if ($ item ['lng ']> $ maxX) $ maxX = $ item ['lng']; if ($ item ['lng '] <$ minX) $ minX = $ item ['lng ']; if ($ item ['lat']> $ maxY) $ maxY = $ item ['lat']; if ($ item ['lat'] <$ minY) $ minY = $ item ['lat']; self: $ vertx [] = $ item ['lng ']; self: $ verty [] = $ item ['lat'];} if ($ pointArray ['lng '] <$ minX | $ pointArray ['lng']> $ maxX | $ pointArray ['lat'] <$ minY | $ pointArray ['lat']> $ maxY) {return false;} return true ;}}
/**************************** Test ********** * ************************/$ map = [// Shanghai ["lng" => 121.488286, "lat" => 31.420147], ["lng" => 121.702154, "lat" => 31.294828], ["lng" => 121.780918, "lat" => 31.141157], ["lng" => 121.782068, "lat" => 30.941157], ["lng" => 121.492885, "lat" => 30.909931], ["lng" => 121.22325, "lat" => 30.890099], ["lng" => 121.161482, "lat" => 31.015526], ["lng" => 121.076395, "lat" => 31.226239], ["lng" => 121.189873, "lat" => 31.339688], ["lng" => 121.459509, "lat" => 31.41368],]; $ array = ["lat" => 31.218681, "lng" => 121.08604]; // The verification region validationMap: setCoordArray ($ map); var_dump (validationMap :: isCityCenter ($ array ));

Application in map:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.