开发者

show IP location on google map

I am trying to get user location from IP address as the user logs in into my django app. but don't know how to do it exactly. I am using GeoIP for 开发者_开发技巧this purpose. my question is how to write a view for this purpose and how can I show the location of the IP on a google map.


I guess you are talking about how to plot markers, cause GeoIP looks to provide you the latitude and longitude. For plotting markers tutorial you can look here.

For v2 Api of google maps

http://econym.org.uk/gmap/basic1.htm

For v3 Api of google maps.

http://code.google.com/apis/maps/documentation/javascript/tutorial.html


Showing geoip on a google map you can use the free geoip service on http://freegeoip.net/json/ and plot the map points as below:

    var geoLocations = getLocations();
    var center = getCenter();
    var map = new google.maps.Map(document.getElementById('googlemap'), 
    {
      zoom: 1,
      center: new google.maps.LatLng(center[0], center[1]),
      mapTypeId: google.maps.MapTypeId.SATELLITE 
    });
    var markerInfo = new google.maps.InfoWindow();
    var pointMarker, i;
    // Go through the location array...
    for (i = 0; i < geoLocations.length; i++) 
    { 
        // Add the marker.
        pointMarker = new google.maps.Marker(
            {
                position: new google.maps.LatLng(geoLocations[i][1], geoLocations[i][2]),
                map: map
            });
        // Add the information window when clicking the marker
        google.maps.event.addListener(pointMarker, 'click', (function(pointMarker, i) 
        {       
            return function() 
            {
                markerInfo.setContent(geoLocations[i][0] + ' -> ' + geoLocations[i][3]);
                markerInfo.open(map, pointMarker);
            }
        })(pointMarker, i));

        // Zoom on double click
        google.maps.event.addListener(pointMarker, 'dblclick', (function(pointMarker, i) 
        {       
            return function() 
            {
                map.setZoom(17);
            }
        })(pointMarker, i));            
    }

Sample code for calling the free geoip service using python you can find here: https://github.com/jamesrep/geoipard

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜