Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added lat/long auto-calculation to the admin and commented out the Ho…

…me marker
  • Loading branch information...
commit 6dc2f7aab015660344d4188bc2a7236f0482ea71 1 parent c2c969d
Mark Ransom authored
View
23 store_locator/admin.py
@@ -1,11 +1,34 @@
from django.contrib import admin, messages
+from django.conf.urls.defaults import patterns, url
from models import Location
from django.conf import settings
+from store_locator.views import get_lat_long
class LocationAdmin(admin.ModelAdmin):
list_display = ('id','name','address','phone', 'url')
list_display_links = ('id','name',)
list_filter = ('name',)
+
+ fieldsets = (
+ (None, {
+ 'fields': ('name',)
+ }),
+ ('Address', {
+ 'fields': ('address', ('latitude', 'longitude'))
+ }),
+ ('Other Information', {
+ 'fields': ('phone', 'url', 'description')
+ }),
+ )
+ class Media:
+ js = ("store_locator/js/store_locator_admin.js",)
+
+ def get_urls(self):
+ old_urls = super(LocationAdmin, self).get_urls()
+ new_urls = patterns('',
+ url(r'^get_lat_long/$', get_lat_long)
+ )
+ return new_urls + old_urls
admin.site.register(Location, LocationAdmin)
View
6 store_locator/models.py
@@ -51,9 +51,9 @@ def GetDistance(self, source_latitude, source_longitude, target_location):
class Location(models.Model):
name = models.CharField(max_length=255)
- address = models.TextField(max_length=255, blank=True)
- latitude = models.FloatField(blank=True, null=True)
- longitude = models.FloatField(blank=True, null=True)
+ address = models.TextField(max_length=255, blank=False)
+ latitude = models.FloatField(blank=False, null=True)
+ longitude = models.FloatField(blank=False, null=True, help_text="If you do not enter a latitude and longitude we will try to find them for you using Google Maps.")
description = models.TextField(blank=True, null=True)
phone = models.CharField(max_length=255, blank=True, null=True)
url = models.URLField(max_length=255, blank=True, null=True)
View
13 store_locator/static/store_locator/js/store_locator_admin.js
@@ -0,0 +1,13 @@
+$j = django.jQuery
+
+function get_lat_long() {
+ var address = $j(this).val();
+ $j.get(window.location.href + "../get_lat_long/?q=" + address, function(data, code) {
+ $j("input#id_latitude").val(data.split(",")[2]);
+ $j("input#id_longitude").val(data.split(",")[3]);
+ });
+}
+
+$j(document).ready(function() {
+ $j("textarea#id_address").blur(get_lat_long);
+});
View
79 store_locator/static/store_locator/js/store_locator_map.js
@@ -0,0 +1,79 @@
+var zoom_list = {
+ "1" : 13,
+ "5" : 12,
+ "10" : 11,
+ "15" : 11,
+ "25" : 10,
+ "50" : 9,
+ "100" : 8,
+}
+var markers = new Array;
+var latlng = new google.maps.LatLng(39.0997265,-94.5785667);
+var myOptions = {
+ zoom: 10,
+ center: latlng,
+ mapTypeId: google.maps.MapTypeId.ROADMAP
+};
+var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
+var infoWindow = new google.maps.InfoWindow();
+var search_value = '';
+document.map = map;
+function clear_markers() {
+ for (i in markers) {
+ markers[i].setMap(null);
+ }
+ markers.length = 0;
+}
+
+function location_search() {
+ clear_markers();
+ search_value = $("#locations_search_field").val();
+ var distance = $("#distance_field").val();
+ var new_zoom = zoom_list[distance];
+ $.get(get_lat_long_url + "?q=" + search_value, function(data) {
+ var latitude = data.split(',')[2];
+ var longitude = data.split(',')[3];
+ map.setZoom(new_zoom);
+ /*var marker = new google.maps.Marker({
+ position: new google.maps.LatLng(latitude, longitude),
+ title: search_value,
+ });
+ marker.setMap(map);
+ markers.push(marker);*/
+ map.setCenter(new google.maps.LatLng(latitude, longitude));
+ $.getJSON(get_locations_url + "?lat=" + latitude + "&long=" + longitude + "&distance=" + distance, function(data) {
+ $.each(data, function() {
+ location_info = this;
+ var location_marker = new google.maps.Marker({
+ position: new google.maps.LatLng(location_info.latitude, location_info.longitude),
+ title: location_info.name,
+ });
+ location_marker.setMap(map);
+ markers.push(location_marker);
+ google.maps.event.addListener(location_marker, "click", get_location_marker_click_listener(location_info, location_marker));
+ });
+ });
+ });
+}
+
+function get_location_marker_click_listener(location_info, location_marker) {
+ return function() {
+ content = "<strong>" + location_info.name + "</strong><br>" +
+ location_info.address.replace(/\n/g, '<br />') + "<br>" +
+ "<a href='http://maps.google.com/maps?saddr=" + search_value + "&daddr=" + location_info.address + "'>Directions</a>";
+ if (location_info.url != '') {
+ content += "<br><strong>Website:</strong> <a href='" + location_info.url + "'>" + location_info.url + "</a>";
+ }
+ if (location_info.phone != '') {
+ content += "<br><strong>Phone:</strong> " + location_info.phone;
+ }
+ if (location_info.description != '') {
+ content += "<br><i> " + location_info.description + "</i>";
+ }
+
+ infoWindow.setContent(content);
+ infoWindow.open(map, location_marker);
+
+ }
+}
+
View
109 store_locator/templates/store_locator/store_locator_map.html
@@ -3,97 +3,26 @@
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=true"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js" type="text/javascript"></script>
-<form>
- <input type="text" id="locations_search_field" />
- <select id="distance_field">
- <option value="1">1 Mile</option>
- <option value="5">5 Miles</option>
- <option value="10">10 Miles</option>
- <option value="15">15 Miles</option>
- <option value="25" selected="selected">25 Miles</option>
- <option value="50">50 Miles</option>
- <option value="100">100 Miles</option>
- </select>
- <input type="submit" onClick="location_search(); return false;" value="Search" />
-</form>
-<br />
-<div id="map_canvas" style="width:600px; height:400px"></div>
+<div class="locations_search_fields">
+ <form>
+ <input type="text" id="locations_search_field" />
+ <select id="distance_field">
+ <option value="1">1 Mile</option>
+ <option value="5">5 Miles</option>
+ <option value="10">10 Miles</option>
+ <option value="15">15 Miles</option>
+ <option value="25" selected="selected">25 Miles</option>
+ <option value="50">50 Miles</option>
+ <option value="100">100 Miles</option>
+ </select>
+ <input type="submit" onClick="location_search(); return false;" value="Search" />
+ </form>
+</div>
+<div id="map_canvas" lass="locations_map" style="width:600px; height:400px"></div>
+<script type="text/javascript" src="{{ STATIC_URL }}store_locator/js/store_locator_map.js"></script>
<script type="text/javascript">
- var zoom_list = {
- "1" : 13,
- "5" : 12,
- "10" : 11,
- "15" : 11,
- "25" : 10,
- "50" : 9,
- "100" : 8,
- }
- var markers = new Array;
- var latlng = new google.maps.LatLng(39.0997265,-94.5785667);
- var myOptions = {
- zoom: 10,
- center: latlng,
- mapTypeId: google.maps.MapTypeId.ROADMAP
- };
- var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
- var infoWindow = new google.maps.InfoWindow();
- var search_value = '';
- document.map = map;
- function clear_markers() {
- for (i in markers) {
- markers[i].setMap(null);
- }
- markers.length = 0;
- }
-
- function location_search() {
- clear_markers();
- var get_lat_long_url = "{{ get_lat_long_url }}";
- var get_locations_url = "{{ get_locations_url }}";
- search_value = $("#locations_search_field").val();
- var distance = $("#distance_field").val();
- var new_zoom = zoom_list[distance];
- $.get(get_lat_long_url + "?q=" + search_value, function(data) {
- var latitude = data.split(',')[2];
- var longitude = data.split(',')[3];
- map.setZoom(new_zoom);
- var marker = new google.maps.Marker({
- position: new google.maps.LatLng(latitude, longitude),
- title: search_value
- });
- google.map
- marker.setMap(map);
- markers.push(marker);
- map.setCenter(marker.position);
- $.getJSON(get_locations_url + "?lat=" + latitude + "&long=" + longitude + "&distance=" + distance, function(data) {
- $.each(data, function() {
- location_info = this;
- var location_marker = new google.maps.Marker({
- position: new google.maps.LatLng(location_info.latitude, location_info.longitude),
- title: location_info.name
- });
- location_marker.setMap(map);
- markers.push(location_marker);
- google.maps.event.addListener(location_marker, "click", get_location_marker_click_listener(location_info, location_marker));
- });
- });
- });
- }
-
- function get_location_marker_click_listener(location_info, location_marker) {
- return function() {
- content = "<strong>" + location_info.name + "</strong><br>" +
- location_info.address.replace(/\n/g, '<br />') + "<br>" +
- "<a href='http://maps.google.com/maps?saddr=" + search_value + "&daddr=" + location_info.address + "'>Directions</a>";
- if (location_info.phone != '') {
- content += "<br><strong>Phone:</strong> " + location_info.phone;
- }
-
- infoWindow.setContent(content);
- infoWindow.open(map, location_marker);
-
- }
- }
+ var get_lat_long_url = "{{ get_lat_long_url }}";
+ var get_locations_url = "{{ get_locations_url }}";
</script>
View
2  store_locator/views.py
@@ -10,7 +10,7 @@
def get_lat_long(request):
if not request.GET.get('q'):
- return ''
+ return HttpResponse('')
args = urllib.urlencode({'q': request.GET.get('q')})
r = urllib2.urlopen("http://maps.google.com/maps/geo?output=csv&%s" % args)
return HttpResponse(r.read())
Please sign in to comment.
Something went wrong with that request. Please try again.