How to auto fill latitude and longitude fields in Django2.0.6?

375 views
Skip to first unread message

prateek gupta

unread,
Jun 19, 2018, 1:45:14 AM6/19/18
to Django users

Hi All,


I have created a store address form using Django2.0.6+Mysql+Python3.6.

Currently user has to manually fill latitude and longitude for the store address.

I want to auto-fill both fields once user entered the address.

Below is my current code-

models.py-

class Store(models.Model):
    building = models.ForeignKey(Building, related_name='building', 
    on_delete=models.SET_NULL, blank=True, null=True)
    name = models.CharField(max_length=100)
    postal_code = models.CharField(max_length=6)
    address = models.TextField()
    latitude = models.FloatField(validators=[MinValueValidator(-90.0), 
    MaxValueValidator(90.0)])
    longitude = models.FloatField(validators=[MinValueValidator(-180.0), 
    MaxValueValidator(180.0)])

class Meta:
    managed = False
    db_table = 'store'


admin.py-

class StoreAdmin(admin.ModelAdmin):
    list_display = ('id', 'name', 'postal_code', 'address', 'latitude', 
 'longitude')


Can anyone please help me how to achieve this requirement?

john fabiani`

unread,
Jun 19, 2018, 2:45:54 AM6/19/18
to django...@googlegroups.com

I use pygeocoder.py and something like this!

johnf

class Getlatitudelongitude(dObject):
    ###############################
    #   gmaps.py
    ###############################

    import urllib
    from time import sleep
    import pygeocoder


    key = "here_goes_your_key"

    def location2latlong(self, query, key="here_goes_your_key" ):
        """pass a complete address
        18 Tadlock Place Woodland, CA 95776"""
        """Get the ( latitute, longitude ) coordinates corresponding
        to the query. If something goes wrong return None."""
        import urllib
        from time import sleep
        import pygeocoder

        #output = 'csv'

        #params = { }
        ### The key below was generated by accessing google http://code.google.com/apis/maps/signup.html
        #params[ 'key' ] = 'ABQIAAAALTR1FL24DycBouLv0t9q3xTEP5gmNW-Z6UMR8J4ewevcZqC8sxRRfI_D2-yrCM4YXmAZEPFdIzSijA' #key
        #params[ 'output' ] = output
        #params[ 'q' ] = query

        #params = urllib.urlencode( params )
        #print params
        a_geo_class = pygeocoder.Geocoder()

        try:
            #f = urllib.urlopen( "http://maps.google.com/maps/geo?%s" % params )
            f = a_geo_class.geocode(query)
            response = f.coordinates
            latitude = response[0]
            longitude = response[1]
            #f.close()
            return latitude, longitude
        except pygeocoder.GeocoderError as err:
            return None

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/56497a07-9c46-4413-9fab-db3f17298146%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

prateek gupta

unread,
Jun 19, 2018, 4:05:19 AM6/19/18
to Django users
It seems for this I need to use API key from google.
Have you heard about this python package- from location_field.models.plain import PlainLocationField?

I tried with this one but getting some issues-

anthon...@btinternet.com

unread,
Jun 20, 2018, 3:07:03 AM6/20/18
to django...@googlegroups.com
You need access to a service (or data set) which translates addresses to latitude/longitude.

It will depend on your country whether this information is available and/or whether you need to pay for access to that service, and how that service

Which countries will your application cover - maybe with some extra information someone can give you some more details.

There is nothing in django that provides this capability.

Question - why do you need latitude and longitude ?

--
-- Anthony Flury
email : *Anthon...@btinternet.com*
Twitter : *@TonyFlury <https://twitter.com/TonyFlury/>*
 
--
Anthony Flury
anthon...@btinternet.com



From: prateek gupta <gupt...@gmail.com>
To: Django users <django...@googlegroups.com>
Sent: Tuesday, June 19, 2018 6:45 AM
Subject: How to auto fill latitude and longitude fields in Django2.0.6?

prateek gupta

unread,
Jun 20, 2018, 3:54:35 AM6/20/18
to Django users
I am working in Singapore and trying to store details of different merchants of Singapore.
So if any merchant wants to add a new store, I am giving him this option build on Django.
So once he entered his store address, the lat/long fields will be auto-fill and location field will show the map.
The implementation part I have already covered above, you can check what package I am using for this functionality.
Reply all
Reply to author
Forward
0 new messages