Geo Location & Spatial Searches with RavenDB–Part V-Spatial Searching
So, all of this have gone quite far. We have seen that we can quite easily go from having the user’s IP address to figuring out its location using our Geo Location database.
The next step is to actually do something about it. Usually, when doing geo location, you care to know what the human readable name for the user’s location is, but in our case, what we most care about is the user’s physical location, so it is lucky that the geo location database that we found also include longitude and latitude information.
With that, we can define the following index, on our events. The Longitude & Latitude information is actually calculated by the browser using the Google Geocoder API, so we just plug in the address, and the site does the rest by figuring out where on the globe we are.
This index allows us to search by spatial query as well as by time:
Using that, we can do:
First, we try to find the location of the user based on the user IP, then we are making a spatial query on top of the events, based on the user location.
What we are doing essentially is asking, “show me the next 2 future events within 200 miles of the user’s location”. Just a tiny little bit of code, but it produces this:
And hopefully, this will narrow things down for you to the obvious: “Of course I am going to the next RavenDB Course”!