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

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

Start on open social

  • Property svn:eol-style set to native
File size: 2.7 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//      startShowMap("OX2 6DR");
17        findPC();
18  }
19
20  function findPC() {
21        var req = opensocial.newDataRequest();
22        req.add(req.newFetchPersonAppDataRequest("VIEWER", [persistentTag]), "pc");
23        req.send(gotPC);
24  }
25  function gotPC(data) {
26        if(data.hadError()) {
27                alert(data.getError());
28        } else {
29                var pc = data.get("pc").getData();
30                for (var k in pc) {
31                        var postcode = pc[k][persistentTag];
32                        startShowMap(postcode);
33                }
34        }
35  }
36
37  function pickPC() {
38        var pc = document.getElementById("pc").value;
39
40        var req = opensocial.newDataRequest();
41        req.add(req.newUpdatePersonAppDataRequest("VIEWER",persistentTag, pc));
42        req.send(checkOK);
43  }
44  function checkOK(data) {
45        if(data.hadError()) {
46                alert(data.getError());
47        } else {
48                alert("Updated your favourite postcode!");
49                findPC();
50        }
51  }
52
53  function startShowMap(postcode) {
54        lastLookedForPC = postcode;
55        document.getElementById("pc").value = postcode;
56
57        // Ditch any spaces
58        postcode = postcode.replace(" ","");
59
60        // Find the details
61        var url = geocoder + postcode;
62        var params = {};
63        params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.JSON;
64        gadgets.io.makeRequest(url, doShowMap, params);
65  }
66  function doShowMap(obj) {
67        // Zap old
68        var map = document.getElementById("yourmap");
69        while(map.hasChildNodes()) {
70                map.removeChild(map.firstChild);
71        }
72
73        var json = obj.data;
74//      if(obj.errors) {
75//              alert(obj.errors);
76//              alert(obj.text);
77//      }
78        if(json) {
79                var tile_e = '000' + Math.floor(json["e"]/1000);
80                var tile_n = '000' + Math.floor(json["n"]/1000);
81                tile_e = tile_e.substring(tile_e.length-3);
82                tile_n = tile_n.substring(tile_n.length-3);
83                var tile = "http://tile.npemap.org.uk/osgb/scaled1/" + tile_e + "/" + tile_n + ".jpg";
84
85                map.innerHTML = "<img src='" + tile + "' alt='Map of "+ json["pc"] +"' />";
86        } else {
87                map.innerHTML = "No map found for postcode " + lastLookedForPC + ", sorry";
88        }
89  }
90</script>
91<div id="content_div">
92 <div id="yourmap"></div>
93 <div id="pickpc">
94   <label for="pc">Favourite Postcode:</label>
95   <input type="text" id="pc" style="width: 10em" onblur="pickPC()" />
96 </div>
97 <br />
98 <h3>Your friends:</h3>
99 <div id="friends"></div>
100</div>
101]]> 
102</Content>
103</Module>
Note: See TracBrowser for help on using the repository browser.