public bool PointIsInPolygon(EMapPoint[] mapPoints, EMapPoint mapPoint) { if (mapPoints.Length < 3) { throw new ArgumentException("多边形顶点必须大于3个!"); } int j = 0, count = 0; for (int i = 0; i < mapPoints.Length; i++) { j = (i == mapPoints.Length - 1) ? 0 : j + 1; if ((mapPoints[i].Lat != mapPoints[j].Lat) && (((mapPoint.Lat >= mapPoints[i].Lat) && (mapPoint.Lat < mapPoints[j].Lat)) || ((mapPoint.Lat >= mapPoints[j].Lat) && (mapPoint.Lat < mapPoints[i].Lat))) && (mapPoint.Lng < (mapPoints[j].Lng - mapPoints[i].Lng) * (mapPoint.Lat - mapPoints[i].Lat) / (mapPoints[j].Lat - mapPoints[i].Lat) + mapPoints[i].Lng)) { count++; } } return (count % 2 > 0) ? true : false; }
public bool PointIsInCircle(EMapPoint mapCenterPoint, double radius, EMapPoint mapPoint) { return GetDistance(mapCenterPoint, mapPoint) <= radius; }
public bool PointIsInRect(EMapPoint mapAPoint, EMapPoint mapBPoint, EMapPoint mapPoint) { double maxx = 0; double minx = 0; double maxy = 0; double miny = 0; if (mapAPoint.Lng > mapBPoint.Lng) { maxx = mapAPoint.Lng; minx = mapBPoint.Lng; } else { maxx = mapBPoint.Lng; minx = mapAPoint.Lng; } if (mapAPoint.Lat > mapBPoint.Lat) { maxy = mapAPoint.Lat; miny = mapBPoint.Lat; } else { maxy = mapBPoint.Lat; miny = mapAPoint.Lat; } if (mapPoint.Lng < maxx && mapPoint.Lng > minx && mapPoint.Lat < maxy && mapPoint.Lat > miny) { return true; } else { return false; } }
public EMapLocation GetLocation(EMapPoint mapPoint) { EMapLocation location = new EMapLocation(); try { var url = string.Format("http://api.map.baidu.com/geocoder/v2/?output=xml&coordtype={0}ll&location={1},{2}&ak={3}", mapPoint.Coordinates.ToString().ToLower(), mapPoint.Lat, mapPoint.Lng, _baduKey); var xml = HttpLibSyncRequest.Get(url); XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); XmlElement root = doc.DocumentElement; XmlNode res = root.SelectSingleNode("result"); location.Address = res.SelectSingleNode("formatted_address").InnerText; location.Province = res.SelectSingleNode("addressComponent/province").InnerText; location.City = res.SelectSingleNode("addressComponent/city").InnerText; location.District = res.SelectSingleNode("addressComponent/district").InnerText; location.Street = res.SelectSingleNode("addressComponent/street").InnerText; } catch { location.Address = "无法解析地址"; } return location; }
public double GetDistance(EMapPoint mapPoint1, EMapPoint mapPoint2) { double radLat1 = mapPoint1.Lat * Math.PI / 180.0; double radLat2 = mapPoint2.Lat * Math.PI / 180.0; double a = radLat1 - radLat2; double b = mapPoint1.Lng * Math.PI / 180.0 - mapPoint2.Lng * Math.PI / 180.0; double s = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(a / 2), 2) + Math.Cos(radLat1) * Math.Cos(radLat2) * Math.Pow(Math.Sin(b / 2), 2))); s = s * (_r / 1000); s = Math.Round(s * 10000) / 10; return s; }
public bool IsOutOfChina(EMapPoint mapPoint) { if (mapPoint.Lng < 72.004 || mapPoint.Lng > 137.8347) return true; if (mapPoint.Lat < 0.8293 || mapPoint.Lat > 55.8271) return true; return false; }
public void ReCalcMileageReportDays(int deviceId, DateTime reportDay) { var mrd = _rep.Get<EMileageReportDay>(p => p.DeviceId == deviceId && p.ReportDay == reportDay.GetDayStartTime()); if (mrd == null) { CreateMileageReportDayByReportDay(deviceId, reportDay); } else { mrd.UpdateTime = DateTime.Now; var list = _positionService.GetDeviceHistoryDatas(deviceId, reportDay); if (list.Count > 1) { mrd.DayMileage = _mileageService.CalcMileage(list); var spt = new EMapPoint(list[0].E, list[0].F); mrd.StartTime = list[0].I; mrd.StartLatLng = spt.ToString(); mrd.StartAddress = _mapService.GetLocation(spt).Address; var ept = new EMapPoint(list[list.Count - 1].E, list[list.Count - 1].F); mrd.EndTime = list[list.Count - 1].I; mrd.EndLatLng = ept.ToString(); mrd.EndAddress = _mapService.GetLocation(ept).Address; } _rep.Save(mrd, p => p.DeviceId == deviceId && p.ReportDay == reportDay.GetDayStartTime()); } }
public EMapPoint[] Wgs84ToGcj02(EMapPoint[] mapPoints) { for (var i = 0; i < mapPoints.Length; i++) { mapPoints[i] = Wgs84ToGcj02(mapPoints[i]); } return mapPoints; }
public EMapPoint Wgs84ToBd09(EMapPoint mapPoint) { try { var url = string.Format("http://api.map.baidu.com/geoconv/v1/?output=xml&coords={0},{1}&from=1&to=5&ak={2}", mapPoint.Lng, mapPoint.Lat, _baduKey); var xml = HttpLibSyncRequest.Get(url); var doc = new XmlDocument(); doc.LoadXml(xml); var root = doc.DocumentElement; var res = root.SelectSingleNode("result"); mapPoint.Lng = Convert.ToDouble(res.SelectSingleNode("point/x").InnerText); mapPoint.Lat = Convert.ToDouble(res.SelectSingleNode("point/y").InnerText); mapPoint.Coordinates = EnumMapCoordinates.Bd09; } catch (Exception ex) { _logger.Error("Wgs84ToBd09出错", ex); } return mapPoint; }
public EMapPoint Wgs84ToGcj02(EMapPoint mapPoint) { if (IsOutOfChina(mapPoint)) { return mapPoint; } var dLat = TransFormLat(mapPoint.Lng - 105.0, mapPoint.Lat - 35.0); var dLon = TransFormLng(mapPoint.Lng - 105.0, mapPoint.Lat - 35.0); var radLat = mapPoint.Lat / 180.0 * Math.PI; var magic = Math.Sin(radLat); magic = 1 - _e * magic * magic; var sqrtMagic = Math.Sqrt(magic); dLat = (dLat * 180.0) / ((_r * (1 - _e)) / (magic * sqrtMagic) * Math.PI); dLon = (dLon * 180.0) / (_r / sqrtMagic * Math.Cos(radLat) * Math.PI); var mgLat = mapPoint.Lat + dLat; var mgLon = mapPoint.Lng + dLon; return new EMapPoint(mgLat, mgLon) { Coordinates = EnumMapCoordinates.Gcj02 }; }
public EMapPoint[] Gcj02ToWgs84(EMapPoint[] mapPoints) { for (var i = 0; i < mapPoints.Length; i++) { mapPoints[i] = Gcj02ToWgs84(mapPoints[i]); } return mapPoints; }
public EMapPoint Gcj02ToWgs84(EMapPoint mapPoint) { var mapPoint1 = TransForm(mapPoint); var lontitude = mapPoint.Lng * 2 - mapPoint1.Lng; var latitude = mapPoint.Lat * 2 - mapPoint1.Lat; return new EMapPoint(latitude, lontitude) { Coordinates = EnumMapCoordinates.Wgs84 }; }
public ActionResult GetLocation(EMapPoint mapPoint) { var location = _mapService.GetLocation(mapPoint); return JsonResult(location); }
public void CreateMileageReportDayByReportDay(int deviceId, DateTime reportDay) { var mrd = new EMileageReportDay() { DeviceId = deviceId, ReportDay = reportDay }; var list = _positionService.GetDeviceHistoryDatas(deviceId, reportDay); if (list.Count > 1) { mrd.DayMileage = _mileageService.CalcMileage(list); var spt = new EMapPoint(list[0].E, list[0].F); mrd.StartTime = list[0].I; mrd.StartLatLng = spt.ToString(); mrd.StartAddress = _mapService.GetLocation(spt).Address; var ept = new EMapPoint(list[list.Count - 1].E, list[list.Count - 1].F); mrd.EndTime = list[list.Count - 1].I; mrd.EndLatLng = ept.ToString(); mrd.EndAddress = _mapService.GetLocation(ept).Address; } mrd.CreateTime = DateTime.Now; mrd.UpdateTime = DateTime.Now; _rep.Add(mrd); }
public bool PointIsInLineSegment(EMapPoint mapStartPoint, EMapPoint mapEndPoint, EMapPoint mapPoint, double offset = 1) { //result>0 在线的左边 result<0在线的右边 result=0 在线上 var result = ((mapStartPoint.Lng - mapPoint.Lng) * (mapEndPoint.Lat - mapPoint.Lat) - (mapEndPoint.Lng - mapPoint.Lng) * (mapStartPoint.Lat - mapPoint.Lat)) * 1000000; var absResult = Math.Abs(result); if (absResult < offset) { //如果已经确定点在线上了(这里仅仅是确定是在这个线上,线是无线延长的 故还不是确定在线段里面) 这里还需要确定一下 这个点是否是在这两个点组成的矩形里面 return PointIsInRect(mapStartPoint, mapEndPoint, mapPoint); } return false; }
public EMapPoint[] Wgs84ToBd09(EMapPoint[] mapPoints) { List<EMapPoint> list = new List<EMapPoint>(mapPoints.Length); try { var size = 100; var totalPage = mapPoints.Length / size; if (mapPoints.Length % size > 0) { totalPage++; } for (int i = 0; i < totalPage; i++) { var converts = mapPoints.Skip(i * size).Take(size); var url = string.Format("http://api.map.baidu.com/geoconv/v1/?output=xml&coords={0}&from=1&to=5&ak={1}", string.Join(";", converts.Select(p => p.Lng + "," + p.Lat).ToArray()), _baduKey); var xml = HttpLibSyncRequest.Get(url); var doc = new XmlDocument(); doc.LoadXml(xml); var root = doc.DocumentElement; var res = root.SelectSingleNode("result").Cast<XmlNode>().Select(p => new EMapPoint() { Lng = Convert.ToDouble(p.SelectSingleNode("x").InnerText), Lat = Convert.ToDouble(p.SelectSingleNode("y").InnerText) }); list.AddRange(res); } } catch (Exception ex) { _logger.Error("批量Wgs84ToBd09出错", ex); return mapPoints; } return list.ToArray(); }
public bool PointIsInLine(EMapPoint[] mapPoints, EMapPoint mapPoint, double offset = 1) { if (mapPoints.Length < 2) { throw new ArgumentException("线上的点必须大于2个!"); } //取出线上的每一段出来匹配 如果只要有点在其中一段上 那么说明点在线上 for (int i = 0, j = 1; i < mapPoints.Length - 1; i++, j++) { if (PointIsInLineSegment(mapPoints[i], mapPoints[j], mapPoint, offset)) { return true; } } return false; }
private EMapPoint TransForm(EMapPoint mapPoint) { if (IsOutOfChina(mapPoint)) { return mapPoint; } var dLat = TransFormLat(mapPoint.Lng - 105.0, mapPoint.Lat - 35.0); var dLon = TransFormLng(mapPoint.Lng - 105.0, mapPoint.Lat - 35.0); var radLat = mapPoint.Lat / 180.0 * Math.PI; var magic = Math.Sin(radLat); magic = 1 - _e * magic * magic; var sqrtMagic = Math.Sqrt(magic); dLat = (dLat * 180.0) / ((_r * (1 - _e)) / (magic * sqrtMagic) * Math.PI); dLon = (dLon * 180.0) / (_r / sqrtMagic * Math.Cos(radLat) * Math.PI); var mgLat = mapPoint.Lat + dLat; var mgLon = mapPoint.Lng + dLon; return new EMapPoint(mgLat, mgLon); }
public VDeviceCurrentData GetVDeviceCurrentData(int deviceId, EnumMapCoordinates coordinates) { var deviceCurrentData = _rep.Query<EDevice, EDeviceCurrentData, VDeviceCurrentData>() .LeftJoin<EDeviceCurrentData>((d, c) => d.Id == c.DeviceId) .Where(p => p.Id == deviceId) .Select((d, c) => new Columns(d.Id.As("DeviceId"), d.DeviceName, c)).Single(); EMapPoint tpt = null; EMapPoint fpt = new EMapPoint(deviceCurrentData.Latitude, deviceCurrentData.Longitude); switch (coordinates) { case EnumMapCoordinates.Gcj02: tpt = _mapService.Wgs84ToGcj02(fpt); break; case EnumMapCoordinates.Bd09: tpt = _mapService.Wgs84ToBd09(fpt); break; } if (tpt != null) { deviceCurrentData.Latitude = tpt.Lat; deviceCurrentData.Longitude = tpt.Lng; } return deviceCurrentData; }
private void button5_Click(object sender, EventArgs e) { var ms = CdevContainer.Ins.Resolve<IMapService>(); EMapPoint wgs84 = new EMapPoint(22.8094582785, 108.3377612871); var location = ms.GetLocation(wgs84); EMapPoint gcj02 = new EMapPoint(22.8067264385, 108.3417899671); EMapPoint gcj022 = ms.Wgs84ToGcj02(wgs84); EMapPoint wgs842 = ms.Gcj02ToWgs84(gcj02); Trace.WriteLine("wgs84 :" + wgs84); Trace.WriteLine("wgs842 :" + wgs842); Trace.WriteLine("d1 :" + ms.GetDistance(wgs84, wgs842)); Trace.WriteLine("gcj02 :" + gcj02); Trace.WriteLine("gcj022 :" + gcj022); Trace.WriteLine("d2 :" + ms.GetDistance(gcj02, gcj022)); Console.Read(); }