Objective
Company to carry out a project, according to the customer's mobile phone location to obtain accurate ads around, in particular, the Administrator on the map to draw polygons on the advertising range, fall within the scope of the customer to see this ad. The following is a brief description of my implementation method, for the same needs of friends reference.
EF Flat Object Dbgeometry
Dbgeometry can represent points, lines, polygons and other planar objects, the input object can be wkt (well-known text), we can convert the point of the Polygon object on the Baidu map to the wkt description of the polygon, and then to Dbgeometry to save.
/// <summary> ///generating polygons from multi-point data/// </summary> /// <param name= "Pointsjson" >Point set of Baidu map polygon</param> Public StaticDbgeometry Polygonfrompointsjson (stringPointsjson) { varPoints = jsonserializer.toentity<list<lnglat>>(Pointsjson); if(Points. First (). GetHashCode ()! =points. Last (). GetHashCode ()) {points. ADD (points. First ()); } varClosepoints = ((ienumerable<lnglat>) points). Select (item =string. Format ("{0} {1}", item. Lng, item. Lat)); varWKT ="POLYGON (("+string. Join (",", closepoints) +"))"; returnDbgeometry.polygonfromtext (wkt, system_id); }
View Code
Use Dbgeometry type to record polygon range in AD entity
/// <summary> ///AD Scope Table/// </summary>[Serializable] Public classadvrange:entity {/// <summary> ///AD Scope name/// </summary>[Required] [Length ( -)] Public stringName {Get;Set; } /// <summary> ///City Identification ID/// </summary> PublicGuid Cityid {Get;Set; } /// <summary> ///Polygon/// </summary> PublicDbgeometry Polygon {Get;Set; } }
View Code
Get the point of a polygon from a Dbgeometry object
The Wellknownvalue property of the Dbgeometry object can get wkt text, currently I do not find the relevant tool to wkt to JSON, so use regular expressions to get also meet the needs, and then convert the results to JSON can be placed on the map to show.
/// <summary> ///gets the point of the polygon/// </summary> /// <returns></returns> Public StaticList<lnglat> getpolygonpoints ( Thisdbgeometry geometry) { varList =NewList<lnglat>(); varPoints = geometry. WellKnownValue.WellKnownText.Matches (@"-? [1-9]\d*\.\d*|-?0\.\d*[1-9]\d*"). Select (item =decimal. Parse (item)). ToArray (); for(vari =0; I < points. Length; i = i +2) {list. ADD (NewLnglat {Lng = Points[i], Lat = Points[i +1] }); } if(list. First (). GetHashCode () = =list. Last (). GetHashCode ()) {list. RemoveAt (list. Count-1); } returnlist; }
View Code
Whether the ad scope contains a point for the customer's location
Dbgeometry has a number of methods and properties that EF can translate to the corresponding SQL statement, where polygon.intersects (point) can determine whether the points are within the polygon.
(The center's red callout is the centroid of the polygon)