Skip to content
This repository has been archived by the owner on Feb 8, 2024. It is now read-only.
This repository has been archived by the owner on Feb 8, 2024. It is now read-only.

Support for GeoDjango Maps #196

Open
psychok7 opened this issue Nov 15, 2016 · 6 comments
Open

Support for GeoDjango Maps #196

psychok7 opened this issue Nov 15, 2016 · 6 comments

Comments

@psychok7
Copy link

Is there any support or plans for GeoDjango Maps in the admin PointField for example?? At the moment i only get the text version SRID=4326;POINT (-4.306640624400504 12.72608429520555)

thanks

@kmmbvnr
Copy link
Collaborator

kmmbvnr commented Nov 15, 2016

How GeoDjango supports them in the build-in django admin?

@psychok7
Copy link
Author

@kmmbvnr In a "normal" Django admin installation you can see an actual map and you can choose an open street maps map for example, by importing from django.contrib.gis import admin and using OSMGeoAdmin. Everything works pretty much out of the box with Django admin

screen

@psychok7
Copy link
Author

psychok7 commented Nov 15, 2016

To be more specific:

from django.contrib.gis import admin as gis_admin


class SecureOSMGeoAdmin(gis_admin.OSMGeoAdmin):
    openlayers_url = (
        'https://cdnjs.cloudflare.com/ajax/libs/openlayers/2.13.1/OpenLayers.js'
    )

And

class AccommodationAdmin(SecureOSMGeoAdmin):
   pass

admin.site.register(Accommodation, AccommodationAdmin) 

The model:

class Accommodation(models.Model):    
    location = models.PointField(srid=4326, db_index=True, blank=True, null=True)

@kmmbvnr
Copy link
Collaborator

kmmbvnr commented Nov 16, 2016

Mm, yep, that's need to be implemented. The scope of the open version of django-material is to cover the core django functionality.

Thanks for the code examples.

@nyejon
Copy link

nyejon commented Oct 27, 2017

If you want to display a map you could use some variation of this which has worked for me for now (I'm sure there is a better way to do it so if you know one please let me know!):

    def show_map(self, instance):
        '''
        Adds an OSM map to the admin form. (requires leaflet to be included in the base.html and the #mapid css to be configured to specify a height)
        see: http://leafletjs.com/examples/quick-start/ for a guide
        '''
        html = '<div id="mapid"></div>'
        html += f'<script>var mymap = L.map("mapid").setView([{instance.latitude}, {instance.longitude}], 17);'
        html +=  '''L.tileLayer( 'http://{{s}}.tile.openstreetmap.org/{{z}}/{{x}}/{{y}}.png', {{
                attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a>',
                subdomains: ['a','b','c']
                }}).addTo( mymap );
                '''
        html += f'''L.marker( [{instance.latitude}, {instance.longitude}] )
                .bindPopup( '<a href="https://www.openstreetmap.org/?mlat={instance.latitude}&mlon={instance.longitude}&zoom=17" target="_blank">{instance.name}</a>' )
                .addTo( mymap );
                </script>'''
        return format_html(html)

This assumes you have a latitude and longitude field in your model..
Then add show_map to your readonly_fields and your fieldset and it should show your map in the admin.

This is what it looks like:
image

@opalczynski
Copy link

opalczynski commented Nov 3, 2017

Guys, What is the status of this - Will be django geo gis support added to the django-material? in some reasonable future?

@kmmbvnr kmmbvnr added frontend and removed admin labels Dec 15, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants