Google Map Center Based on Geocoded IP

Basically, when someone opens my (Google) map, I want it to be approximate by default.

Is there an easy way to do this using the Google APIs or do I need to write my own code (this is a python based application)?

+5
source share
5 answers

Check out http://www.ipinfodb.com/ . You can get the latitude and longitude by passing the IP address to your services. I recently did something when I created a simple service that grabbed the current IP address and then passed it to the service ("api / location / city" is just a service that curls the ipinfodb service). Using jquery:

$.get("api/location/city", null, function(data, textStatus)
{        
    if (data != null)
    {
        if (data.Status == "OK")
        {
            var lat = parseFloat(data.Latitude);
            var lng = parseFloat(data.Longitude);

            $.setCenter(lat, lng, $.settings.defaultCityZoom);

            manager = new MarkerManager(map, {trackMarkers : true });

            var e = $.createUserMarker(map.getCenter());
            e.bindInfoWindowHtml($("#marker-content-event").html());

            var m = [];
            m.push(e);

            // map.addOverlay(e);
            manager.addMarkers(m, 10);
            manager.refresh();
        }
        else
        {
            $.setCenter($.settings.defaultLat, $.settings.defaultLng, $.settings.defaultZoom);
        }
    }
}, "json");

The key here is the following line:

$.setCenter(lat, lng, $.settings.defaultCityZoom);

Just set the center to lat / lng of the service call result.

+3
source

You can use the built-in Google API ClientLocation object:

if (GBrowserIsCompatible()) 
{   
    var map = new google.maps.Map2(document.getElementById("mapdiv"));

    if (google.loader.ClientLocation) 
    {        
        var center = new google.maps.LatLng(
            google.loader.ClientLocation.latitude,
            google.loader.ClientLocation.longitude
        );
        var zoom = 8;

        map.setCenter(center, zoom);
    }
}
+8
source

Per , map.setCenter(new GLatLng(37.4419, -122.1419), 13); . Javascript.

IP lat long, , API Google , -, maxmind, hostip , . , - , !

0

FireFox 3.5/google gears, lat lng . fooobar.com/questions/1032686/...

0

All Articles