Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Graham Carr 277 posts 389 karma points
    Oct 19, 2015 @ 19:37
    Graham Carr
    0

    Postcode/location radius

    Hello,

    A project I am working on currently has the requirement for a postcode/location search within a selected radius of miles.

    What is the best way of implementing such a search, I am currently using EZSearch for simple Examine based searches but I know this does not support such a search as it was created to allow simple Examine searches.

    Many thanks,

    Graham

  • Matthieu Nelmes 102 posts 385 karma points
    Oct 19, 2015 @ 19:57
    Matthieu Nelmes
    0

    I recently discovered .Net 4.6+ has built in methods for detecting distances between lat/longs co-ords.

    I haven't got example code to hand but what you want to do is store all locations with their appropriate lat/longs.

    When a user puts in their postcode or location, fire off a geocode request to Google Maps Geocoding api to return their lat/Long you can then use GeoCoordinate.GetDistanceTo to match the nearest results: https://msdn.microsoft.com/en-us/library/system.device.location.geocoordinate.getdistanceto(v=vs.110).aspx

  • Matthieu Nelmes 102 posts 385 karma points
    Oct 20, 2015 @ 07:58
    Matthieu Nelmes
    0

    Ok, I've found examples to show you what I mean, these all go on the assumption you're populating your Views with strongly typed models.

    IEnumberable<Location-View=Model> locations = Method-to-populate-view-models();
    

    First you'll need to get all of your searchable locations into a list. Within your method that populates each of your location ViewModels, you'll want to extend to contain a GeoCoordinate object for each location (System.Device.Location.GeoCoordinate). Then this can be populated with pre-saved lat/long information about the address (I can cover this as well if needed).

    With the lat/longs you can then hydrate the GeoCoordinate object like this:

    location.GeoCoordinate = new System.Device.Location.GeoCoordinate(location.lat, location.long)
    

    Now the clever part, take the search location entered by the user (or the start location), you can pass this address into Googles GeoCoding service to return a lat/long, which you can then inturn convert into a GeoCoordinate using the method above. So you should end up with one start GeoCoordinate, and a list of ViewModels with their own respective GeoCoordinates, you then find the nearest with this:

    nearest = (from x in locations select x).OrderBy(x => x.GeoCoordinate.GetDistanceTo(start-location-geocoordinate)).ToList();
    
  • Matthieu Nelmes 102 posts 385 karma points
    Oct 20, 2015 @ 08:01
    Matthieu Nelmes
    0

    Just to add on regarding search radius, what I'm doing is looping back through my results and populating a DistanceToSearch parameter on each location. The GeoCoordinate.GetDistanceTo() returns the distance in meters. You can then use this to limit the results to a certain radius.

Please Sign in or register to post replies

Write your reply to:

Draft