Google Maps implementation for Ubuntu-django-foundations

Blueprint: https://blueprints.launchpad.net/ubuntu-django-foundations/+spec/maps-jquery-plugin

Download: jquery-ubuntu-maps-0.2.4.js

Required js libraries:

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript" src="https://github.com/jquery/jquery-tmpl/raw/master/jquery.tmpl.min.js"></script>
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&language=en&region=US"></script>
    <script type="text/javascript" src="http://google-maps-utility-library-v3.googlecode.com/svn/tags/markerclusterer/1.0/src/markerclusterer_packed.js"></script>

The plugin consists of 2 commands e.g.

$('map-element').selectLocation({ params })
$('map-element').showLocations({ params })

Both commands with their parameters and uses will be exmplained below

selectLocation

Allows the user to add a marker to the map.

There are 3 methods to add/move a marker on the map:

  1. By clinking an empty space on the map
  2. By changing the <input> elements that represents the latitude and longitude

  3. By changing the <input> elements that is a representation of the address


html_lat and html_lng

The HTML <input> element that represents the latitude and longitude of the marker e.g.

Default

null

maybe change this to #id_lng

Example

HTML

<input type="text" id="id_lat" name="lat" value="0.353452" />
<input type="text" id="id_lng" name="lng" value="0.435323" />

Javascript

$('map-element').selectLocation({ html_lat: $('#id_lat'),
                                  html_lng: $('#id_lng') });

Specification


marker_icon

The url of the icon that is displayed as marker.

Default

/media.icons/marker.png

Example

$('map-element').selectLocation({ marker_icon: '/media/icons/my_marker.png' });

Specification


html_addr

The HTML <input> elements where the adress is specified

See the google api for more information

Default

null

Example

HTML

<input type="text" id="id_country" name="country" />
<input type="text" id="id_city" name="city" />
<input type="text" id="id_address" name="address" />

Javascript

$('map-element').selectLocation({ html_addr: $('#id_country, #id_city, #id_address') });

Specification

showLocations

Shows multiple markers on the map. The markers cannot be changed position. When clicked on the marker, it can (if specified) show some HTML content with information about that marker. If there are many markers and zoomed out, the markers are clustered to save some CPU power. When zoomed in, the cluster is separated into single markers. Clicking on a cluster shows the markers in it (max 10).


marker_icon

The url of the icon that is displayed as marker.

Default

null

Example

$('map-element').showLocations({ marker_icon: '/media/icons/my_marker.png' });

Specification


markers_url

A list of markers, from url OR a predefined list

Default

null

Example

$('map-element').showLocations({ markers_url: '/map/locations/all/'  });

Specification The URL should return:

[{lng:<float>, lat:<float>},
 {lng:<float>, lat:<float>}
 ...]

or (specially for django)

[{pk: <int>, fields: {lng:<float>, lat:<float>}},
 {pk: <int>, fields: {lng:<float>, lat:<float>}},
 ...]

Additional information can be added to fields.

The JSON can be easily created by the django function:

Example models.py class MyModel(models.Model): <tab>lat = models.FloatField() <tab>lng = models.FloatField()

views.py from django.core import serializers data = serializers.serialize('json', MyModel.objects.all(), ensure_ascii=False) return HttpResponse(data, mimetype="application/json")


markers_list

A list of markers in json format

Default

null

Example

$('map-element').showLocations({ markers_list: [ {lat: 0.345, lng: 0.3456}, 
                                                 {lat: 44.345, lng: 34.3456} ] });

position_name

If your json file does not contain 'lat' and 'lng' but uses other names for these fields, you can specify yhe names here

Default

{ lat: 'lat', lng: 'lng' }

Example

$('map-element').showLocations({ position_name: { lat: 'latitude', lng: 'longitude' } });

The JSON data provided by markers_url or markers_list should then looks like

[ { latitude: 0.345, longitude: 0.3456}, 
  { latitude: 44.345, longitude: 34.3456} ]

marker_content_url

The content that need to be displayed when clicked on a marker, loaded with an url

Default

null

Example

$('map-element').showLocations({ marker_content_url: '/marker/user/${user}/${username}/' });

The part ${user} and ${username} are replaced by the values of the marker

{ lat: 45.3245, lng: 23.4563, user: 1, username: 'myname' }

is translated to te url '/marker/user/1/myname/'

Specification

marker_content_tmpl

If all the values you want to display are in the marker itself, this function can be used to translate the marker data into a nice template

Default

null

Example

$('map-element').showLocations({ marker_content_tmpl: '/path/to/template.tmpl' });

The template file should look something like this:

<script id="markerTemplate" type="text/x-jquery-tmpl"> 
    <div>
        <p>UserId: ${ marker.user }</p>
        <p>Username: ${ marker.username }</p>
        <p>other param: ${ marker.other }</p>
    </div>
</script>

The JSON data provided by markers_url or markers_list should then looks like

[ { latitude: 0.345, longitude: 0.3456, user: 1, username: 'myname', other: 'blablabla' }, 
  { latitude: 44.345, longitude: 34.3456, user: 2, username: 'anothername', other: 'not important' } ]


user_location_zoom

Uses the HTML5 geolocation to determine the location of the user. If the value is null this behaviour is disabled

See the Mozilla developer page for more information

Default

7

Examples

$('map-element').showLocations({ user_location_zoom: 9 });
$('map-element').showLocations({ user_location_zoom: null } });

Specifications

single_marker_zoom

The zoomlevel for the map when there is only one marker on it. Use null to disable this future Default

7

Example

$('map-element').showLocations({ single_marker_zoom: 9 });
$('map-element').showLocations({ single_marker_zoom: null });

Specification

ajax_load_error

The error that is shown when the remote page could not be loaded

Default

'<p>The page could not be loaded</p>',

Example

$('map-element').showLocations({ ajax_load_error: '<p>Contact the site admin, the page could not be loaded</p>' });

ajax_load_icon

The url of the icon that is shown when an ajax call is executed

Default

'/ubuntu-website/media/images/ajax-loader.gif'

Example

$('map-element').showLocations({ ajax_load_icon: '/path/to/icon.gif' });

cluster_tmpl

The link to the template that is shown when someone clicks on a cluster

Default

null

Example

$('map-element').showLocations({ cluster_tmpl: '/path/to/template.tmpl' });

The template should look like this. marker_list is the list of markers. From here the attributes of the marker can be used.

<script id="clusterTemplate" type="text/x-jquery-tmpl"> 
    <div>
        <ul>
            {{each marker_list.slice(0, 10)}}
            <li>${ user } - ${ is_support }</li>
            {{/each}}
        </ul>
        {{if marker_list.length > 10}}
        <p>and ${ marker_list.length - 10 } more</p>
        {{/if}}
    </div>
</script>

filter

A filter object for displaying the right markers, which filter on the attributes of the marker

Default

null

Example

$('map-element').showLocations({ filter: [ { element: $('#filter-programmers'), attrs: { program: true },
                                           { element: $('#filter-support'), attrs: { support: true },
                                           { element: $('#filter-none), attrs: {} ] });

This example has 2 filters (programmers and support), the last one shows all the markers (no filtering)

The element should be a jQuery DOM element (button to click on, for triggering the filter)

<a href="javascript:void(0);" id="filter-programmers">Programmers</a>
<a href="javascript:void(0);" id="filter-support">Support</a>
<div id="filter-none">Show all</div>

The markers should have the attibutes program and support

Specification

Full Example 1 (showLocations)

models.py

class Marker(models.Model):
    user = models.ForeignKey(User)
    lat = models.FloatField()
    lng = models.FloatField()

views.py

from django.core import serializers
from django.contrib.auth.models import User
from django.shortcuts import get_object_or_404
from models import Marker

def show_all_markers(request):
    data = serializers.serialize('json', Marker.objects.all(), ensure_ascii=False)
    return HttpResponse(data, mimetype="application/json")

def marker_html(request, userId):
    user = get_object_or_404(User, pk=userId)
    return render_to_response('templatename.html', {'user':user})

urls.py

    url('^map/markers/all', 'app.views.show_all_markers', name='map-markers-all'),
    url('^map/marker/user/(?P<user>\d+)/', 'app.views.marker_html', name='marker_html'),

HTML

<html>
<body>
    <section id="mymap">
    </section>
</body>
</html>

CSS

#mymap { height: 400px; width: 500px; }

Javascript

$(function(){
    $('#mymap').showLocations({ markers_url: '{% url map-markers-all %}',
                                marker_content_url: '/map/marker/user/${user}/',
                                marker_icon: '{{ MEDIA_URL }}icons/marker.png' });
});

TODO

ubuntu-django-foundations/map (last edited 2011-04-11 11:40:40 by 41)