1 | <!DOCTYPE html> |
---|
2 | <html> |
---|
3 | <head> |
---|
4 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
---|
5 | <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"> |
---|
6 | <meta name="apple-mobile-web-app-capable" content="yes"> |
---|
7 | <title>OpenLayers BBOX Strategy Example</title> |
---|
8 | <link rel="stylesheet" href="../theme/default/style.css" type="text/css"> |
---|
9 | <link rel="stylesheet" href="style.css" type="text/css"> |
---|
10 | <script src="../lib/OpenLayers.js"></script> |
---|
11 | <script type="text/javascript"> |
---|
12 | var map, photos; |
---|
13 | |
---|
14 | /** |
---|
15 | * A specific format for parsing Flickr API JSON responses. |
---|
16 | */ |
---|
17 | OpenLayers.Format.Flickr = OpenLayers.Class(OpenLayers.Format, { |
---|
18 | read: function(obj) { |
---|
19 | if(obj.stat === 'fail') { |
---|
20 | throw new Error( |
---|
21 | ['Flickr failure response (', |
---|
22 | obj.code, |
---|
23 | '): ', |
---|
24 | obj.message].join('')); |
---|
25 | } |
---|
26 | if(!obj || !obj.photos || |
---|
27 | !OpenLayers.Util.isArray(obj.photos.photo)) { |
---|
28 | throw new Error( |
---|
29 | 'Unexpected Flickr response'); |
---|
30 | } |
---|
31 | var photos = obj.photos.photo, photo, |
---|
32 | x, y, point, |
---|
33 | feature, features = []; |
---|
34 | for(var i=0,l=photos.length; i<l; i++) { |
---|
35 | photo = photos[i]; |
---|
36 | x = photo.longitude; |
---|
37 | y = photo.latitude; |
---|
38 | point = new OpenLayers.Geometry.Point(x, y); |
---|
39 | feature = new OpenLayers.Feature.Vector(point, { |
---|
40 | title: photo.title, |
---|
41 | img_url: photo.url_s |
---|
42 | }); |
---|
43 | features.push(feature); |
---|
44 | } |
---|
45 | return features; |
---|
46 | } |
---|
47 | }); |
---|
48 | |
---|
49 | function init() { |
---|
50 | map = new OpenLayers.Map('map'); |
---|
51 | |
---|
52 | var base = new OpenLayers.Layer.OSM(); |
---|
53 | |
---|
54 | var style = new OpenLayers.Style({ |
---|
55 | externalGraphic: "${img_url}", |
---|
56 | pointRadius: 30 |
---|
57 | }); |
---|
58 | |
---|
59 | photos = new OpenLayers.Layer.Vector("Photos", { |
---|
60 | projection: "EPSG:4326", |
---|
61 | strategies: [new OpenLayers.Strategy.BBOX({resFactor: 1})], |
---|
62 | protocol: new OpenLayers.Protocol.Script({ |
---|
63 | url: "http://api.flickr.com/services/rest", |
---|
64 | params: { |
---|
65 | api_key: 'b5e8c0e287e678671c3d8b2c0f3ced85', |
---|
66 | format: 'json', |
---|
67 | method: 'flickr.photos.search', |
---|
68 | extras: 'geo,url_s', |
---|
69 | per_page: 10, |
---|
70 | page: 1 |
---|
71 | }, |
---|
72 | callbackKey: 'jsoncallback', |
---|
73 | format: new OpenLayers.Format.Flickr() |
---|
74 | }), |
---|
75 | styleMap: new OpenLayers.StyleMap(style) |
---|
76 | }); |
---|
77 | |
---|
78 | map.addLayers([base, photos]); |
---|
79 | map.setCenter( |
---|
80 | new OpenLayers.LonLat(-567468.5392481, |
---|
81 | 4950672.5471436), 5); |
---|
82 | } |
---|
83 | |
---|
84 | </script> |
---|
85 | </head> |
---|
86 | <body onload="init()"> |
---|
87 | <h1 id="title">BBOX Strategy Example</h1> |
---|
88 | <div id="tags"> |
---|
89 | vector, feature, stylemap, bbox, strategy, script, flickr |
---|
90 | </div> |
---|
91 | <p id="shortdesc"> |
---|
92 | Uses a BBOX strategy to request features within a bounding box. |
---|
93 | </p> |
---|
94 | <div id="map" class="smallmap"></div> |
---|
95 | <div id="docs"> |
---|
96 | <p>The BBOX strategy requests data within a bounding box. When the |
---|
97 | previously requested data bounds are invalidated (by browsing to |
---|
98 | some area not covered by those bounds), another request for data |
---|
99 | is issued.</p> |
---|
100 | |
---|
101 | <p>This particular example uses the <a |
---|
102 | href="http://www.flickr.com/services/api/">Flickr API.</a></p> |
---|
103 | |
---|
104 | </div> |
---|
105 | </body> |
---|
106 | </html> |
---|