erdem / django-map-widgets

Highly customizable, intuitive, and user-friendly map widgets for GeoDjango applications.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

center map from user ip adress using GeoIP

michaelhjulskov opened this issue · comments

Hi :)

In my Sign up form I want my mapwidget to attempt to center as near as possible to the user.
I use GeoIP.

The code inside my RegisterView looks something like this:

def get_client_ip(request):
    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
    if x_forwarded_for:
        ip = x_forwarded_for.split(',')[0]
    else:
        ip = request.META.get('REMOTE_ADDR')
    return ip

from django.contrib.gis.geoip import GeoIP
def RegisterView(request):
    center_here = False
    initial = {'language': to_locale(get_language())}
    ip = get_client_ip(request)
    if ip:
        g = GeoIP()
        c = g.city(ip)
        if c:
            if c['country_code']:
                initial['country'] = c['country_code']
            if c['latitude'] and c['longitude']:
                center_here = Point(c['longitude'], c['latitude'])
    form = RegisterForm(request.POST or None, request.FILES or None, initial=initial)
    if request.method == 'POST':
        if form.is_valid():
          .............saving stuff............
        else:
            messages.error(request, _("Please correct the error below."))
            if not form.cleaned_data['location']:
                center_here = False

    return render(request, 'registration/register.html', {
        'form': form,
        'center_here': center_here,
    })

so now i need to find a way to implement center_here in my html template form and how to trigger mapwidget to center the map there.
anyone have an idea how to trigger map to be centered on center_here point
thanks :)

By the way if anybody want to copy my method above, remember to download the data files from:
https://dev.maxmind.com/geoip/legacy/geolite/
and add GEOIP_PATH to your settings file
read more about GeoIP here: https://docs.djangoproject.com/en/1.11/ref/contrib/gis/geoip/
or GeoIP2 read here: https://docs.djangoproject.com/en/2.0/ref/contrib/gis/geoip2/

please help. i still dont know how to trigger:
-center map on x,y point (via jquery/js)
-set marker on x,y point (via jquery/js)

I have searched, a lot!
I hope someone can just give me a hint how i should do this, please :)

here is my latest attempt - form field name is 'location'

var locationRio = {lat: -22.915, lng: -43.197};
var marker = new google.maps.Marker({
  position: locationRio,
  map: $("#location-map-elem").map,
  title: 'Hello World!'
});

If you visit https://dev.maxmind.com/geoip/legacy/geolite/ and Maxmind has announced that discontinue of coordinates in GeoLite2 in 2019. Will it impact the function of RegisterView which is using coordinates?