Baidu map PC side to determine whether the user in the distribution range _ios

Source: Internet
Author: User

Recently received a project, which has the project requirements are as follows:

Set up in the PC side of the distribution range, the user in the order, according to the user set the distribution site to determine whether within the scope of distribution, and give users the appropriate tips.

Next I realize the idea:

1. Use the Baidu map to set the distribution range on the PC side, can drag and drop the choice

2. Based on the distribution address set by the user to determine whether within the distribution range

One, Baidu map PC-side Acquisition range

Change Baidu Map website demo, set distribution range.

Idea: Get the vertices of the polygon and save it to the database in JSON form.

Baidu API on Polygon overlay:

constructor Function:

Polygon (points:array<point>[, opts:polygonoptions]) creates a polygon overlay

Method:

SetPath (path:array<point>) None to set up a multilateral array of points (added since 1.2)
GetPath () array<point> returns a multilateral array of points (added since 1.2)

Realize:

Main code:

 Set Distribution scope
function SetRange (_point, _ppoints) {
  var polygon = new Bmap.polygon (_ppoints, {strokecolor: "Blue", Strokeweight:2, strokeopacity:0.5}); Create a polygon
  
  map.addoverlay (polygon);  Increase polygon
  polygon.enableediting ();  Allows editing
  
  of Polygon.addeventlistener ("Lineupdate", function (e) {
    var Rangearr = Polygon.getpath ();
    $ ("#distributeRange"). Val (json.stringify (Rangearr));}

The above code is mainly to monitor the Lineupdate event, each time drag and drop Baidu map callback function will return the vertices of the polygon, and then through the Json.stringify method to the string type exists inside a label, to be followed by the form submission operation.

Second, judge whether the point is within the scope

Go to the Internet to look at, judge whether the point is in the distribution range of methods are many, probably using the Ray method.
But some methods are not considered comprehensively, resulting in a lack of accuracy.
In the Baidu map of the geoutils found in the "Judge Dot whether polygon inside" this method.
Because I need to make a judgment on the back end, and then directly to the JS into Java, testing, joy! (The test method is attached later)

 /** * Determines whether the point is within the polygon * @param Point checkpoint * @param the vertex of the PTS polygon * @return Point returns True in the polygon, otherwise it returns false */public static Boolea
  N Isptinpoly (point2d.double point, list<point2d.double> pts) {int N = Pts.size (); Boolean Boundorvertex = true; If the point is on the vertex or edge of the polygon, it is also counted as a point within the polygon, returning true int intersectcount = 0;//cross points count of x double precision = 2e-10; The tolerance point2d.double P1, p2;//neighbour bound vertices point2d.double p = Point are compared with 0 when the floating-point type is calculated.
      Current point p1 = Pts.get (0);//left vertex for (int i = 1; I <= N; ++i) {//check All Rays if (P.equals (p1)) { Return boundorvertex;//p are an vertex} P2 = pts.get (i% N);//right Vertex if (p.x < Math.min (p1.x , p2.x) | | 
      p.x > Math.max (p1.x, p2.x)) {//ray is outside to our interests P1 = P2; Continue;//next Ray left point} if (P.x > Math.min (p1.x, p2.x) && p.x < Math.max (p1.x, p2.x)) {//ray is crossing-algorithm (common part of) if (P.y <= Math.max (P1.Y, p2.y)) {//x is before of ray if (p1.x = p2.x && p.y >= math.min (p1.y, p2.y)) {/
        /overlies on a horizontal ray return Boundorvertex;
            } if (p1.y = = p2.y) {//ray is vertical if (p1.y = = p.y) {//overlies on a vertical ray
          return Boundorvertex;
          }else{//before Ray ++intersectcount; }}else{//cross point on the left side double xinters = (p.x-p1.x) * (P2.Y-P1.Y)/(P2.
            x-p1.x) + P1.y;//cross point of Y if (Math.Abs (p.y-xinters) < precision) {//overlies on a ray
          return Boundorvertex;
          } if (P.y < xinters) {//before Ray ++intersectcount; }}}else{//special case when Ray is crossing through the vertex if (p.x = p2.x &AMP;&A mp P.Y <= p2.y) {//p crossing over p2 point2d.double p3 = pts.get ((i+1)% N); Next vertex if (p.x >= math.min (p1.x, p3.x) && p.x <= Math.max (p1.x, p3.x)) {//p.x lies B
        Etween p1.x & p3.x ++intersectcount;
        }else{Intersectcount + + 2;
  }} p1 = P2;//next ray left point} if (intersectcount% 2 = 0) {//even) return false outside the polygon;
  else {//odd number in the polygon return true; }
}

The main is to judge and the feasibility of this method.

Write a test method for this.

Train of thought: get the vertices of a polygon and then randomly point a point

1. Call the Baidu map of the method to determine whether the point in the range
2. According to the Baidu map to obtain the latitude and longitude of the shop, their own procedures to determine whether within the scope of

The way to call Baidu map:

  

The interface is as follows:

After clicking on a point on the page, get the coordinates of the point (for your own method test) and call the Inoroutpolygon to determine whether the store is within this range.

Background test methods:

Test whether a point is public
static void Main (string[] args) in a polygon {
  point2d.double dot = new Point2d.double (116.404072, 39 .916605);
  list<point2d.double> pts = new arraylist<point2d.double> ();
  Pts.add (New Point2d.double (116.395, 39.910));
  Pts.add (New Point2d.double (116.394, 39.914));
  Pts.add (New Point2d.double (116.403, 39.920));
  Pts.add (New Point2d.double (116.402, 39.914));
  Pts.add (New Point2d.double (116.410, 39.913));
  if (Isptinpoly (Point, pts)) {
    System.out.println ("dot in Polygon");
  } else{
    System.out.println ("Point is outside the polygon");
  }

After the test, the result is satisfactory.

To sum up, the process of implementation is most important to preserve those vertices and to determine whether a point is within a polygon surrounded by those vertices, based on the vertices that are saved (in a certain order).

Baidu Map is still very useful to feel. The API is very full, and all are equipped with demo, is very beneficial to our developers.

The above content is for Baidu map PC side to judge whether the user is in the distribution scope of relevant knowledge, hope to help everyone.

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.