Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting user location with google.loader.ClientLocation working for me but not others

Am trying to use google.loader.ClientLocation along with the maps api to get the location of the visitor and center a map on it. The following is working fine for me (on Safari, Firefox and Chrome), but a friend I got to test it (in Safari and Firefox) just sees a white box with the Google logo.

 <script type="text/javascript" src="http://www.google.com/jsapi?key=MykeyHere"></script>

  <script type="text/javascript">
   google.load("maps", "2");
  </script>

  <script type="text/javascript"> 

      function initialize() {
        if (GBrowserIsCompatible()) {
          var map = new GMap2(document.getElementById("local_map"));
    map.setMapType(G_PHYSICAL_MAP);
    map.setCenter(new GLatLng(google.loader.ClientLocation.latitude, google.loader.ClientLocation.longitude), 15);
          // map.setCenter(new GLatLng(54.5975, -5.920278), 15);
          map.setUIToDefault();

        } 

      </script>

Any ideas what could be going wrong? The site is at http://www.chris-armstrong.com/ticktalk (the map is near the bottom)

like image 310
Chris Armstrong Avatar asked Jan 13 '10 00:01

Chris Armstrong


People also ask

Why is Google location wrong?

The primary reason for Google Maps giving wrong location details is due to bad or no internet connection. If the internet on your android phone is active and running you will be able to get the exact location details.

How do you get geolocation?

The Geolocation API is accessed via a call to navigator. geolocation ; this will cause the user's browser to ask them for permission to access their location data. If they accept, then the browser will use the best available functionality on the device to access this information (for example, GPS).

What is geolocation tracking?

Geolocation refers to the identification of the geographic location of a user or computing device via a variety of data collection mechanisms. Typically, most geolocation services use network routing addresses or internal GPS devices to determine this location. Geolocation is a device-specific API.


1 Answers

Google ClientLocation uses IP address geolocation. There are several IP address geolocation providers out there (the related questions in the sidebar on the right will give you several of those) and they give different results to different users. Google ClientLocation won't be able to locate everyone and it won't always give answers that are as precise or as accurate as some competitors.

On my test page, for example, I use Google ClientLocation and IPInfoDB and Google ClientLocation doesn't find me at home right now, while IPInfoDB thinks I'm in Sunnyvale instead of Berkeley. You may find that one provider is better than the others (particularly if you pay for its database), but I suspect that the most robust method, if you really need it, is to use several and hope that at least one will have a result. You should know up front that IP geolocation will not always work for all users (those behind an anonymizing proxy, for example, will never be located this way).

Also, if you want a more precise location, you can use a Javascript API to ask the browser for the user's exact location. On supported browsers the user will receive a small pop-up asking for their permission and, if given, the browser will then use GPS (Mobile Safari on the iPhone, for example) or WiFi triangulation (Firefox 3.5+) to determine latitude and longitude. You can see how this works on my test page, and compare it with IP geolocation. Not all browsers support the W3C Geolocation API, but a growing number do.

like image 165
npdoty Avatar answered Oct 22 '22 08:10

npdoty