source: trunk/gpx.npemap.org.uk/favourite_postcode.xml @ 659

Last change on this file since 659 was 659, checked in by Nick Burch, 11 years ago

Nearly there

  • Property svn:eol-style set to native
File size: 3.4 KB
Line 
1<?xml version="1.0" encoding="UTF-8" ?> 
2<Module>
3  <ModulePrefs title="Favourite Postcodes" > 
4    <Require feature="opensocial-0.7"/>
5  </ModulePrefs>
6<Content type="html">
7<![CDATA[
8<script type="text/javascript" src="http://www.mattkruse.com/javascript/datadumper/datadumper.js"></script>
9<script type="text/javascript"> 
10  gadgets.util.registerOnLoadHandler(startApp);
11  var geocoder = "http://www.npemap.org.uk/cgi/geocoder.fcgi?format=js&postcode=";
12  var persistentTag = "favourite_postcode";
13  var lastLookedForPC = null;
14
15  function startApp() {
16        findPC();
17        findFriends();
18  }
19
20  function findFriends() {
21        var req = opensocial.newDataRequest();
22        req.add(req.newFetchPeopleRequest(opensocial.DataRequest.Group.VIEWER_FRIENDS), 'viewerFriends');
23        req.add(req.newFetchPersonAppDataRequest("VIEWER_FRIENDS", [persistentTag]), "pc");
24        req.send(loadedFriends);
25  }
26  function loadedFriends(data) {
27        var html = "<ul>";
28
29        var fd = data.get("pc").getData();
30
31        var friends = data.get("viewerFriends").getData();
32        friends.each(function(person) {
33                var postcode = "(not set)";
34                var theirData = fd[person.getId()]
35                if(theirData && theirData[persistentTag]) {
36                        postcode = theirData[persistentTag];
37                }
38                html += "<li>" + person.getDisplayName() + " " + postcode + "</li>";
39        });
40        html += "</ul>";
41
42        document.getElementById("friends").innerHTML = html;
43  }
44
45  function findPC() {
46        var req = opensocial.newDataRequest();
47        req.add(req.newFetchPersonAppDataRequest("VIEWER", [persistentTag]), "pc");
48        req.send(gotPC);
49  }
50  function gotPC(data) {
51        if(data.hadError()) {
52                alert(data.getError());
53        } else {
54                var pc = data.get("pc").getData();
55                for (var k in pc) {
56                        var postcode = pc[k][persistentTag];
57                        startShowMap(postcode);
58                }
59        }
60  }
61
62  function pickPC() {
63        var pc = document.getElementById("pc").value;
64
65        var req = opensocial.newDataRequest();
66        req.add(req.newUpdatePersonAppDataRequest("VIEWER",persistentTag, pc));
67        req.send(checkOK);
68  }
69  function checkOK(data) {
70        if(data.hadError()) {
71                alert(data.getError());
72        } else {
73                alert("Updated your favourite postcode!");
74                findPC();
75        }
76  }
77
78  function startShowMap(postcode) {
79        lastLookedForPC = postcode;
80        document.getElementById("pc").value = postcode;
81
82        // Ditch any spaces
83        postcode = postcode.replace(" ","");
84
85        // Find the details
86        var url = geocoder + postcode;
87        var params = {};
88        params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.JSON;
89        gadgets.io.makeRequest(url, doShowMap, params);
90  }
91  function doShowMap(obj) {
92        // Zap old
93        var map = document.getElementById("yourmap");
94        while(map.hasChildNodes()) {
95                map.removeChild(map.firstChild);
96        }
97
98        var json = obj.data;
99//      if(obj.errors) {
100//              alert(obj.errors);
101//              alert(obj.text);
102//      }
103        if(json) {
104                var tile_e = '000' + Math.floor(json["e"]/1000);
105                var tile_n = '000' + Math.floor(json["n"]/1000);
106                tile_e = tile_e.substring(tile_e.length-3);
107                tile_n = tile_n.substring(tile_n.length-3);
108                var tile = "http://tile.npemap.org.uk/osgb/scaled1/" + tile_e + "/" + tile_n + ".jpg";
109
110                map.innerHTML = "<img src='" + tile + "' alt='Map of "+ json["pc"] +"' />";
111        } else {
112                map.innerHTML = "No map found for postcode " + lastLookedForPC + ", sorry";
113        }
114  }
115</script>
116<div id="content_div">
117 <div id="yourmap"></div>
118 <div id="pickpc">
119   <label for="pc">Favourite Postcode:</label>
120   <input type="text" id="pc" style="width: 10em" onblur="pickPC()" />
121 </div>
122 <div>Your friends:</div>
123 <div id="friends"></div>
124</div>
125]]> 
126</Content>
127</Module>
Note: See TracBrowser for help on using the repository browser.