Serve a web-page
In this lesson we create a web-page that shows locations on a map. We
give a skeleton for the code that can display Google maps decorated with
markers below. Please create a new file map.pl
with the content
below. This file defines a new module (map
), which imports the
web-server infrastructure as well as our previously created reasoning
module. It does not provide any exports because its functionality (the
predicate map/1) is called from the web-server based on the
http_handler/3 declaration.
:- module(map, []). % Web-server libraries :- use_module(library(http/html_write)). :- use_module(library(http/http_dispatch)). :- use_module(library(http/http_parameters)). % Pirates package Google map interface :- use_module(components(gmap)). % Import our query predicates :- use_module(demo). % Bind /map to the predicate map/1 :- http_handler('/map', map, []). % The implementation of map/1 map(Request) :- http_parameters(Request, [ lat1(Lat1, [float]), lon1(Lon1, [float]), lat2(Lat2, [float]), lon2(Lon2, [float]) ]), % **** reply_html_page(title('Piracy events'), [ \gmap(Points) ]).
The predicate reply_html_page/2 generates an HTML page from terms and calls to grammar rules. The rule gmap//1 emits an HTML page with embedded JavaScript that opens Google Maps. Creating HTML from Prolog is subject of the [[Prolog web-service HOWTO][[http://www.swi-prolog.org/howto/http/]].
Exercises
- Complete the code above to show piracy events in the given
bounding box on the map. You do this by replacing
% ***
above with calls that produce a list ofpoint(Lat, Lon)
terms from the given parameters. You can use the predicates you exported fromdemo.pl
and combine them using findall/3 to create a list of point-terms. Test the page using the following queries.
You are now ready to do more exercises.