Nibble

Preview of new upcoming Umbraco package, AJAX Store Locator for Umbraco 6

brandA while back I made the locator package (both for use with xslt http://www.nibble.be/?p=68 and v4 razor http://www.nibble.be/?p=105 )

Currently ‘I’m working on a version that runs on Umbraco 6 and makes a lot more use of ajax than the previous versions.

It uses the same core as the razor version but instead of writing the code inside razor macro it’s now done with the help of an UmbracoApiController that you can then use from js.

Also the locator will load with a location that it finds based on your ip address (and also plan to make it work based on the location of your phone / other mobile device)

So the Umbraco specific bit of this project is of course the custom Controller that currently looks like

public class LocatorApiController : UmbracoApiController
    {
 
        public IEnumerable<ExpandoObject> GetNearest(string address, int parentId, string docTypeFilter, string locationPropAlias, int numberOfSearchResults)
        {
            UmbracoHelper help = new UmbracoHelper(UmbracoContext);
 
            var nodesToSearchThrough = help.TypedContent(parentId)
                .Children
                .Where(c => c.DocumentTypeAlias == 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;
        }
 
    }

 

Cool part here is that I’m returning an IEnumerable<ExpandoObject> (since I want to be able to include all property data of the nodes I’m returning, so it’s easy to access them in js).

Preview

 

 

Code

Code is available on github https://github.com/TimGeyssens/LocatorRazorStyle

2 Comments so far

  1. David Zweben on September 12th, 2013

    Looks good! I look forward to its release.

    I have a feature request: It would be great if your package could pull data from other page properties to use for the search string in the backoffice. I’m going to be setting up an Umbraco site with a big locations section in a few months, and the pages will store the location’s address split up across several textstring properties. Being able to use that same data would ensure that things stay consistent.

    I know that’s a tall order; maybe I could figure out how to customize it instead.

    Thanks,
    David

  2. Emailing on April 28th, 2014

    Like a mouse, your fingertip becomes the cursor as you navigate
    from application to application. Break down your
    message into short paragraphs and use bulleted items to condense and stress important points.
    When inquisitory for vitamin A novel business, you volition
    desire to front for those that ar intended to constitute long-run.

    Also visit my web page :: Emailing

Leave a Reply