Django with GXmlHttp

3 views
Skip to first unread message

504Django

unread,
Nov 23, 2009, 11:01:36 PM11/23/09
to Django users
There seems to be a dearth of examples illustrating best practices in
deploying Google Maps with Django.

Common recommendations are to use GeoDjango.

Of course, it doesn't have to be Google Maps. It could be
OpenSteetMap, Yahoo Maps, or some other mapping API.

Not necessarily related, there are also suggestions to use JQuery to
handle AJAX requests.

I'm trying to understand how to take the next step -- to make the
required GXmlHttp GET requests calling a Django view that returns
marker points, while not violating the RESTful nature of Django URLs.

What would be the recommendation?

To help the discussion along with an example, this might be the
implementation of a pure AJAX solution returning XML with a Get
request to a PHP page:

var request = GXmlHttp.create();
request.open('GET','query.php?var1=' + val1 + '&var2=' + val1, true);
request.onreadystatechange = function() {
if (request.readyState == 4) {
if (request.status == 200) {
var xmlsource = request.responseXML;
var markers = xmlsource.documentElement.getElementsByTagName
("marker");
for (var i = 0; i < markers.length; i++) {
var lat = parseFloat(markers[i].getAttribute("lat"));
var lng = parseFloat(markers[i].getAttribute("lng"));
var latlng = new GLatLng(lat,lng);

504Django

unread,
Nov 24, 2009, 11:30:46 PM11/24/09
to Django users
Hmm ... the lack of a suggested solution to my question is
discouraging. The lack of common knowledge in the developer community
leads me to think that, aside from using the GeoDjango module, Django
may not be able to elegantly integrate with Google Maps AJAX API. It
that's truly the case, perhaps I should consider looking at a
different framework before I invest too much more effort.

Tim Sawyer

unread,
Nov 25, 2009, 8:56:15 AM11/25/09
to django...@googlegroups.com
Not quite sure what you're looking for, but I've used google maps with
django, without using GeoDjango.

http://www.brassbandresults.co.uk/map/

The script used in this page is here:

http://www.brassbandresults.co.uk/map/map_script.js

which is generated using this template:

function initialize() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map_canvas"));
{% if Band %}
map.setCenter(new GLatLng({{Band.latitude}},
{{Band.longitude}}), 12);
{% else %}
map.setCenter(new GLatLng(53.800651, -1.454), 8);
{% endif %}
map.setUIToDefault();
var mgr = new MarkerManager(map);
var lBandMarkers = [];
{% for band in Bands %}
{% if band.latitude %}
{% if band.longitude %}
lPoint{{band.id}} = new GLatLng({{band.latitude}}, {{band.longitude}});
lMarker{{band.id}} = new GMarker(lPoint{{band.id}}, {title:
"{{band.name}}"});
GEvent.addListener(lMarker{{band.id}}, "click", function() {
var myHtml = "<b>{{band.name}}</b><br/>{% if band.rehearsal_night_1
%}Rehearsals {{band.rehearsal_nights}}{%endif%}<br/>[<a
href='/bands/{{band.slug}}/'>Contest&nbsp;Results</a>] [<a
target='_blank' href='{{band.website}}'>Band&nbsp;Website</a>]";
map.openInfoWindowHtml(lPoint{{band.id}}, myHtml);
});
lBandMarkers.push(lMarker{{band.id}});
{% endif %}
{% endif %}
{% endfor %}
mgr.addMarkers(lBandMarkers, 0);
mgr.refresh();
}
}

Hope that helps,

Tim.
> --
>
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>
>
>

504Django

unread,
Nov 25, 2009, 1:13:57 PM11/25/09
to Django users
Tim,

Thanks. That's exactly the kind of simple example that I was looking
for of how to bridge the two worlds of Django templates and Ajax. BTW,
me living in a major crucible of brass band music, I'm completely
fascinated by your site and brass band culture in England -- of which
to date I was completely unfamiliar despite living there for six
months.

The example poses an additional dilemma, however, which is probably
the subject of separate post: How to present data in a browser using a
mapping API without exposing valuable or sensitive data to
unscrupulous hackers.

Let's say it's sensitive data that required an enormous investment to
scrape and aggregate.

The project I'm working would require mapping and charting events
without exposing the bulk of data or other attributes in a way that
could be easily filched.

I think the answer might be server-side Javascript -- something
ASP.NET can handle with the Ajax Toolkit, and Ruby on Rails can handle
-- as I understand it -- by allowing injection of Javascript into its
templates. Django seems to have left Javascript out of mind, which I
view as a weakness.

There do appear to be some Django kludges -- using Jaxer for example.

http://filthyweasel.co.uk/post/7/

Then again, maybe I should really be investing my time learning how to
deploy GeoDjango.

I really looking for an expert opinion before I discover that I've
scaled the wrong mountain.

I'd appreciate any additional comments the Django community might
offer. This seems like a discussion others would benefit from as well.
Reply all
Reply to author
Forward
0 new messages