当前位置: 首页>>代码示例>>C#>>正文


C# GeoCoordinate.Distance方法代码示例

本文整理汇总了C#中OsmSharp.Math.Geo.GeoCoordinate.Distance方法的典型用法代码示例。如果您正苦于以下问题:C# GeoCoordinate.Distance方法的具体用法?C# GeoCoordinate.Distance怎么用?C# GeoCoordinate.Distance使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在OsmSharp.Math.Geo.GeoCoordinate的用法示例。


在下文中一共展示了GeoCoordinate.Distance方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ProjectOn

        /// <summary>
        /// Calculates the closest point on the route relative to the given coordinate.
        /// </summary>
        /// <returns></returns>
        public bool ProjectOn(GeoCoordinate coordinates, out GeoCoordinate projectedCoordinates, out int entryIndex, out Meter distanceFromStart, out Second timeFromStart)
        {
            double distance = double.MaxValue;
            distanceFromStart = 0;
            timeFromStart = 0;
            double currentDistanceFromStart = 0;
            projectedCoordinates = null;
            entryIndex = -1;

            // loop over all points and try to project onto the line segments.
            GeoCoordinate projected;
            double currentDistance;
            var points = this.GetPoints();
            for (int idx = 0; idx < points.Count - 1; idx++)
            {
                var line = new GeoCoordinateLine(points[idx], points[idx + 1], true, true);
                var projectedPoint = line.ProjectOn(coordinates);
                if (projectedPoint != null)
                { // there was a projected point.
                    projected = new GeoCoordinate(projectedPoint[1], projectedPoint[0]);
                    currentDistance = coordinates.Distance(projected);
                    if (currentDistance < distance)
                    { // this point is closer.
                        projectedCoordinates = projected;
                        entryIndex = idx;
                        distance = currentDistance;

                        // calculate distance/time.
                        double localDistance = projected.DistanceReal(points[idx]).Value;
                        distanceFromStart = currentDistanceFromStart + localDistance;
                        if(this.HasTimes && idx > 0)
                        { // there should be proper timing information.
                            double timeToSegment = this.Segments[idx].Time;
                            double timeToNextSegment = this.Segments[idx + 1].Time;
                            timeFromStart = timeToSegment + ((timeToNextSegment - timeToSegment) * (localDistance / line.LengthReal.Value));
                        }
                    }
                }

                // check first point.
                projected = points[idx];
                currentDistance = coordinates.Distance(projected);
                if (currentDistance < distance)
                { // this point is closer.
                    projectedCoordinates = projected;
                    entryIndex = idx;
                    distance = currentDistance;
                    distanceFromStart = currentDistanceFromStart;
                    if (this.HasTimes)
                    { // there should be proper timing information.
                        timeFromStart = this.Segments[idx].Time;
                    }
                }
                
                // update distance from start.
                currentDistanceFromStart = currentDistanceFromStart + points[idx].DistanceReal(points[idx + 1]).Value;
            }

            // check last point.
            projected = points[points.Count - 1];
            currentDistance = coordinates.Distance(projected);
            if (currentDistance < distance)
            { // this point is closer.
                projectedCoordinates = projected;
                entryIndex = points.Count - 1;
                distance = currentDistance;
                distanceFromStart = currentDistanceFromStart;
                if (this.HasTimes)
                { // there should be proper timing information.
                    timeFromStart = this.Segments[points.Count - 1].Time;
                }
            }
            return true;
        }
开发者ID:cmberryau,项目名称:routing,代码行数:78,代码来源:Route.cs

示例2: ProjectOn

        /// <summary>
        /// Project on route and return the next entry index and coordinate.
        /// </summary>
        /// <param name="route"></param>
        /// <param name="coordinates"></param>
        /// <returns></returns>
        private KeyValuePair<int, GeoCoordinate> ProjectOn(Route route, GeoCoordinate coordinates)
        {
            double distance = double.MaxValue;
            GeoCoordinate closest = null;
            int closestIdx = -1;
            List<GeoCoordinate> points = route.GetPoints();
            for (int idx = 0; idx < points.Count - 1; idx++)
            {
                GeoCoordinateLine line = new GeoCoordinateLine(points[idx], points[idx + 1], true, true);
                PointF2D projectedPoint = line.ProjectOn(coordinates);
                GeoCoordinate projected;
                double currentDistance;
                if (projectedPoint != null) {
                    projected = new GeoCoordinate(projectedPoint[1], projectedPoint[0]);
                    currentDistance = coordinates.Distance(projected);
                    if (currentDistance < distance)
                    {
                        closest = projected;
                        closestIdx = idx + 1;
                        distance = currentDistance;
                    }
                }
                projected = points[idx];
                currentDistance = coordinates.Distance(projected);
                if (currentDistance < distance)
                {
                    closest = projected;
                    closestIdx = idx;
                    distance = currentDistance;
                }

            }
            return new KeyValuePair<int,GeoCoordinate>(closestIdx, closest);
        }
开发者ID:nubix-biz,项目名称:OsmSharp,代码行数:40,代码来源:RouteTracker.cs

示例3: ProjectOn

 /// <summary>
 /// Calculates the closest point on the route.
 /// </summary>
 /// <param name="coordinates"></param>
 /// <returns></returns>
 public GeoCoordinate ProjectOn(GeoCoordinate coordinates)
 {
     double distance = double.MaxValue;
     GeoCoordinate closests = null;
     List<GeoCoordinate> points = this.GetPoints();
     for (int idx = 0; idx < points.Count - 1; idx++)
     {
         GeoCoordinateLine line = new GeoCoordinateLine(points[idx], points[idx + 1]);
         PointF2D projectedPoint = line.ProjectOn(coordinates);
         GeoCoordinate projected = new GeoCoordinate(projectedPoint[1], projectedPoint[0]);
         double currentDistance = coordinates.Distance(projected);
         if (currentDistance < distance)
         {
             closests = projected;
             distance = currentDistance;
         }
     }
     return closests;
 }
开发者ID:nubix-biz,项目名称:OsmSharp,代码行数:24,代码来源:Route.cs


注:本文中的OsmSharp.Math.Geo.GeoCoordinate.Distance方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。