views:

657

answers:

4

I am writing JavaScript code using Google Maps API.

map = new google.maps.Map2(document.getElementById("map_canvas"));
map.setCenter(new google.maps.LatLng(37.4419, -122.1419), 13);

The above code sets the default location of the map canvas to Palo Alto.

How can we write the script in such a way that the setCenter function automatically points to the current location of the client?

A: 
map = new google.maps.Map2(document.getElementById("map_canvas"));
pos = new google.maps.LatLng(37.4419, -122.1419);
map.setCenter(pos, 13);
map.panTo(pos);
nex
The above code loads the map with the location set as palo alto. My actual question was, how can we set the location automatically to the current location of the client.
vs1984
Oh I see ... for that you would need geolocation features ... start reading this: http://www.mozilla.com/en/firefox/geolocation/
nex
+1  A: 

You can use the HTML5 GeoLocation API in browsers that support it.

if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(success, error);
} else {
  alert('geolocation not supported');
}

function success(position) {
  alert(position.coords.latitude + ', ' + position.coords.longitude);
}

function error(msg) {
  alert('error: ' + msg);
}
ceejayoz
http://isgeolocationpartofhtml5.com/
npdoty
+2  A: 

I can think of two possible options.

First you may want to consider using the GeoLocation API as ceejayoz suggested. This is very easy to implement, and it is a fully client-side solution. To center the map using GeoLocation, simply use:

map.setCenter(new google.maps.LatLng(position.coords.latitude, 
                                     position.coords.longitude), 13);

... inside the success() callback of the GeoLocation's getCurrentPosition() method.

Unfortunately only a few modern browsers are currently supporting the GeoLocation API. Therefore you can also consider using a server-side solution to resolve the IP address of the client into the user's location, using a service such as MaxMind's GeoLite City. Then you can simply geocode the city and country of the user inside the browser, using the Google Maps API. The following could be a brief example:

<!DOCTYPE html>
<html> 
<head> 
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
    <title>Google Maps API Geocoding Demo</title> 
    <script src="http://maps.google.com/maps?file=api&amp;amp;v=2&amp;amp;sensor=false"
            type="text/javascript"></script> 
  </head> 
  <body onunload="GUnload()"> 

    <div id="map_canvas" style="width: 400px; height: 300px"></div> 

    <script type="text/javascript"> 

    var userLocation = 'London, UK';

    if (GBrowserIsCompatible()) {
       var geocoder = new GClientGeocoder();
       geocoder.getLocations(userLocation, function (locations) {         
          if (locations.Placemark) {
             var north = locations.Placemark[0].ExtendedData.LatLonBox.north;
             var south = locations.Placemark[0].ExtendedData.LatLonBox.south;
             var east  = locations.Placemark[0].ExtendedData.LatLonBox.east;
             var west  = locations.Placemark[0].ExtendedData.LatLonBox.west;

             var bounds = new GLatLngBounds(new GLatLng(south, west), 
                                            new GLatLng(north, east));

             var map = new GMap2(document.getElementById("map_canvas"));

             map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
             map.addOverlay(new GMarker(bounds.getCenter()));
          }
       });
    }
    </script> 
  </body> 
</html>

Simply replace userLocation = 'London, UK' with the server-side resolved address. The following is a screenshot from the above example:

Render google map in based on selected location

You can remove the marker by getting rid of the map.addOverlay(new GMarker(bounds.getCenter())); line.

Daniel Vassallo
Good point re: geolocating from IP.
ceejayoz
Thanks for the explanation. It never occurred to me that the client side component that are commonly used (pre HTML5) do not have fully integrated geolocation support.
vs1984
A: 

You can trying the IP address geo-location to encode the address and passing the value to the Google Map API. I have trying this with IP2Location package, it's return accurate data for me to help my users locate their current location when surfing on my Website.

SuperRomia