public Distance DistanceBetween(Location location, DistanceUnits units)
 {
     double num = (units == DistanceUnits.Miles) ? 3956.545 : 6378.2;//3438.147;
     double num2 = this.ToRadian(location.Latitude - this.Latitude);
     double num3 = this.ToRadian(location.Longitude - this.Longitude);
     double d = Math.Pow(Math.Sin(num2 / 2.0), 2.0) + ((Math.Cos(this.ToRadian(this.Latitude)) * Math.Cos(this.ToRadian(location.Latitude))) * Math.Pow(Math.Sin(num3 / 2.0), 2.0));
     double num5 = 2.0 * Math.Asin(Math.Min(1.0, Math.Sqrt(d)));
     return new Distance(num * num5, units);
 }
        public IEnumerable<ExpandoObject> GetNearest(string address, int parentId, int docTypeFilter, string locationPropAlias, int numberOfSearchResults)
        {
            UmbracoHelper help = new UmbracoHelper(UmbracoContext);

            var nodesToSearchThrough = help.TypedContent(parentId)
                .Descendants().Where(c => c.DocumentTypeId == docTypeFilter).Where("Visible");

            var distanceunit = DistanceUnits.Kilometers;

            var items = new List<GeoItem>();
            var searchLocation = new Location();

            var r = GoogleGeoCoder.CallGeoWS(address);

            searchLocation = new Location(r.Results[0].Geometry.Location.Lat, r.Results[0].Geometry.Location.Lng);

            foreach (var node in nodesToSearchThrough) {

              var itemLocation = new Location(
                    Convert.ToDouble(node.GetProperty(locationPropAlias).Value.ToString().Split(',')[0], Utility.NumberFormatInfo),
                    Convert.ToDouble(node.GetProperty(locationPropAlias).Value.ToString().Split(',')[1], Utility.NumberFormatInfo));

              items.Add(new GeoItem(node,itemLocation,searchLocation.DistanceBetween(itemLocation,distanceunit)));
               }

            items.Sort(new GeoItemComparer());

            var retval = new List<ExpandoObject>();
            foreach (GeoItem geo in items.Take(numberOfSearchResults))
            {
                IPublishedContent node = ((IPublishedContent)geo.Node);

                var obj = new ExpandoObject() as IDictionary<string, Object>;

                obj.Add("Distance", geo.Distance);
                obj.Add("Latitude", geo.Location.Latitude);
                obj.Add("Longitude", geo.Location.Longitude);

                obj.Add("Name", node.Name);
                obj.Add("Url", node.Url);

                foreach (var nodeProp in node.Properties)
                {
                    obj.Add(nodeProp.Alias, nodeProp.Value);
                }

                retval.Add((ExpandoObject)obj);
            }
            return retval;
        }
 static Location()
 {
     Empty = new Location();
 }
 public Distance DistanceBetween(Location location)
 {
     return this.DistanceBetween(location, DistanceUnits.Miles);
 }
Example #5
0
 public GeoItem(object Node, Location Location, Distance Distance)
 {
     this.Node = Node;
     this.Location = Location;
     this.Distance = Distance;
 }