views:

701

answers:

3

I want to store a user's current location on an ASP.NET MVC website for the duration of a session. Because I want to use the location server-side, the way I think this should work (maybe other suggestions?) is that when a user first hits the website, I get the location and store it in the session, so on later requests it does not have to be looked up again.

The best way I can find is to use Google's ClientLocation method:

<script src="http://www.google.com/jsapi?key=YOURAPIKEY" type="text/javascript"></script>
<script>
    google.load("jquery", "1.2.6");
    google.load("jqueryui", "1.5.2");
    var yourLocation = google.loader.ClientLocation.address.city + ", "
       + google.loader.ClientLocation.address.region;
</script>

My question is how best to get the location looked up in this Javascript code back to the server to store in the database or in the session. I know that I could call a controller action using jQuery to pass the location:

$.post("/Home/StoreLocation", location, function(){});

But for performance I don't want this to be happening on every single page load. The other way i thought to do it was to call Google's apis from .NET code by doing something like this (a call to geocoding):

string path = "http://maps.google.com/maps/geo?q=" + strAddress + "&output=csv&key=" + apiKey;
WebClient client = new WebClient();
string[] eResult = client.DownloadString(sPath).ToString().Split(',');
//Parse the array

But I can't find any equivalent way to call ClientLocation directly from a URL like this - all the examples I have seen use google.loader (which I don't think I can use when doing it this way?) Another thing I have considered is using Google's .NET library, but I can't find anything in there to do a location lookup (maybe I am wrong?).

Any suggestions greatly appreciated - I feel I am over-thinking this and missing something obvious!

+1  A: 

In your second example, I don't quite understand strAddress. Do you have the client's address? Or is that their IP? I just tried that URL with my IP address and it says I'm about 1000 miles away.

Anyways, I don't see what's wrong with #1. Just have something like this in your Site.Master:

<script type="text/javascript">
  //other javascript here
  <% if (Sesssion.Contents["has_location"] == null) { %>
    $(document).ready(function_to_submit_location)
  <% } %>
</script>

edited to put the if statement in ASP tags -- it's not a javascript if.

edit for those that want to use this code you'd have to put something in your controller in order to set session["has_location"] = true when you get the location.

James S
Yes! The c# conditional statement !session["has_location"] to show/hide the jQuery call was what I was missing, don't know why i didn't think of that! Thanks James! (The second example is GeoCoding, just to demonstrate how I would call a URL from code - I don't actually want to geocode. Yes you pass in the client address and it should return a Longitude / Latitude.)
James Allen
A: 

This question prompted me to finish off a blog post I had in my draft folder: http://www.alexjamesbrown.com/geek/development/dotnet/reverse-geocoding-with-google-api-and-c/

Please let me know if that's of any help (it's not quite what you are asking... but it could be adapted to do what you want)

alex
Thanks alex, I was thinking of doing something similar myself but I couldn't find a URI to do exactly what I want (the example you use converts an address to a coordinate, I want to find out what the user's location is). Thinking about it, that makes sense because it would have to be called form the client's side of things, from their browser.
James Allen
I did some searching and couldn't find a google API to get the location from IP address. That's all google uses to provide the location to the javascript file, so there's no reason they couldn't expose it to an API so that you could send the IP and get the location... but it seems they don't.
James S
Google Gears has something like this.... i forget what it is called, but i know there is something.
alex
A: 

The Google Client Location Method is designed to be used on the front end/in the browser. It sounds like you're looking for something that would run on your server - either a web service or a local IP database.

MaxMind's has a free GeoIP database with a C# api. You pass the client's IP address to the GeoIP library and it returns the city and state.

http://www.maxmind.com/app/geolitecity

MaxMind also has a webservice available for a nominal fee.

andrew