Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center Google Map Based on geocoded IP

Basically whenever someones opens up my (Google) map I want it default to their approximate location.

Is there an easy way to do it with Google's API or do I have to write a custom code (this is python based app)?

like image 657
Eeyore Avatar asked Sep 02 '09 03:09

Eeyore


People also ask

How do I center an embedded Google Map?

How to center embedded Google map on webpage? How do I get the map centered? Hi Michael, You can simply add text-align: center to the paragraph element that is around the iframe.

Does Google use IP address to find location?

The location used and stored with your Web & App Activity can come from signals such as the device's IP address, your past activity or from your device, if you've chosen to turn on your device's location settings.

Is Google geocoding API free?

The Geocoding API uses a pay-as-you-go pricing model. Geocoding API requests generate calls to one of two SKUs depending on the type of request: basic or advanced. Along with the overall Google Terms of Use, there are usage limits specific to the Geocoding API.


2 Answers

You can use Google API's built-in 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);
    }
}
like image 77
Tim S. Van Haren Avatar answered Sep 20 '22 14:09

Tim S. Van Haren


Check out http://www.ipinfodb.com/. You can get a latitude and longitude value by passing their services an IP address. I did something recently where 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 this line:

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

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

like image 38
typeoneerror Avatar answered Sep 21 '22 14:09

typeoneerror