def find_weather_stations(request): if request.method == 'POST': form = SearchForm(request.POST) if form.is_valid(): location = urllib.quote_plus(form.cleaned_data['location']) request_url = "http://maps.google.com/maps/geo?q=%s&output=%s&key=%s" % (location, "csv", settings.GOOGLE_MAPS_API_KEY) data = urllib.urlopen(request_url).read() dlist = data.split(',') if dlist[0] == '200': point = Point((float(dlist[3]), float(dlist[2])),) else: point = None markers = [] if point: marker = GMarker(point, request.POST['location']) marker.add_event(GEvent('click', 'function() { geodjango.map_marker1.openInfoWindowHtml("%s") }' % form.cleaned_data['location'])) # the gis.maps.google.* stuff doesn't allow for ^^^^^ dynamic naming of these - with multiple points, it will be # necessary to for loop through the points with your own counter (start from zero) and that should coincide with # the template forloop counter being used - but by all means cross-check that every time to make sure it's right. markers.append(marker) weather_stations = find_nearest_weather_stations(point, qty=10) count = 2 for station in weather_stations: marker = GMarker(station.point, '%s %s' % (station.code, station.get_name())) marker.add_event(GEvent('click', 'function() { geodjango.map_marker%s.openInfoWindowHtml("%s"); }' % \ (count, "%s, (altitude %s ft.) - %s mi @ %s deg""" \ % (station.code, floatformat(m_to_ft(station.elevation), 0), floatformat(station.distance.mi, 2), floatformat(rad_to_deg(station.azimuth)))) )) markers.append(marker) count = count + 1 map = GoogleMap(key=settings.GOOGLE_MAPS_API_KEY, markers=markers) return render_to_response('locationfinder.html', { 'location': form.cleaned_data['location'], 'location_point': point, 'google': map, 'weather_stations': weather_stations, }) else: return render_to_response('locationfinder.html', { 'location': location, }) else: return render_to_response('locationfinder.html', {'form': form,}) else: form = SearchForm() return render_to_response('locationfinder.html', {'form': form,})
def googleMapShowUtils(points): markers = [] for point in points: marker = GMarker('POINT(%s %s)' % (point['lng'], point['lat'])) event = GEvent('click', 'function() { location.href = "%s"}' % point['href']) marker.add_event(event) markers.append(marker) google = GoogleMap(center=(0, 0), zoom=1, markers=markers, key=settings.GOOGLE_MAPS_API_PASSWORD) return google #add search utility here
def gmap(self): """return a gmap object that we can use in templates""" gmap = cache.get('place_%s_gmap' % (self.id)) if not gmap: area_polygons = [] area_markers = [] districts = self.in_districts() for district in districts: try: area = district.area.difference(district.area.difference(self.area)) # find the part of the district that is inside the place ( district - (district - area)) except Exception, e: # the above sometimes flips out with a geometry error with particularly odd shapes. (See flagstaff.) # in that case, get the bounding box for an area and use that as the cut-out shape instead. area = district.area.difference(district.area.difference(self.area.envelope)) icon = GIcon('district_%d' % district.id, '/images/markers/district_markers%d.png' % district.id, iconsize=(29, 32), iconanchor=(0,32), shadow='/images/markers/shadow_fake.png') if area.area > 0.0005: try: gp = GPolygon(area, stroke_color="#000", fill_color=district.color, fill_opacity="0.2") area_polygons.append(gp) # if the centroid is not on the surface of the shape, get a point on the surface if area.centroid.within(area): marker = GMarker(area.centroid, icon=icon) else: marker = GMarker(area.point_on_surface, icon=icon) event = GEvent('click', 'function() { location.href = "%s"}' % ( district.get_absolute_url() )) marker.add_event(event) print("check %s %s" % (district.id, area.area)) area_markers.append(marker) except Exception, e: if area.centroid.within(area): marker = GMarker(area.centroid, icon=icon) else: marker = GMarker(area.point_on_surface, icon=icon) event = GEvent('click', 'function() { location.href = "%s"}' % ( district.get_absolute_url() )) marker.add_event(event) area_markers.append(marker) for poly in area: try: gp = GPolygon(poly, stroke_color="#000", fill_color=district.color, fill_opacity="0.2") area_polygons.append(gp) print("check %s %s" % (district.id, poly.area)) except Exception, e: print e
def google_map(request): points = [ { 'lat': '35.42', 'lng': '139.42', 'href': 'http://en.wikipedia.org/wiki/Tokyo' }, { 'lat': '51.30', 'lng': '0.73', 'href': 'http://en.wikipedia.org/wiki/London' }, { 'lat': '40.43', 'lng': '-74.0', 'href': 'http://en.wikipedia.org/wiki/New_York_City' }, { 'lat': '34.03', 'lng': '-118.15', 'href': 'http://en.wikipedia.org/wiki/Los_Angeles' }, { 'lat': '36.774402', 'lng': '-119.755405', 'href': 'http://en.wikipedia.org/wiki/Fresno' }, ] markers = [] for point in points: marker = GMarker('POINT(%s %s)' % (point['lng'], point['lat'])) event = GEvent('click', 'function() { location.href = "%s"}' % point['href']) marker.add_event(event) markers.append(marker) google = GoogleMap(center=(0, 0), zoom=1, markers=markers, key=settings.GOOGLE_MAPS_API_PASSWORD) return render(request, 'rango/google_map.html', {'google': google})