Subversion Repositories Sites.obs-saisons.fr

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
210 aurelien 1
/**
2
 * @name MapIconMaker
3
 * @version 1.1
4
 * @author Pamela Fox
5
 * @copyright (c) 2008 Pamela Fox
6
 * @fileoverview This gives you static functions for creating dynamically
7
 *     sized and colored marker icons using the Charts API marker output.
8
 */
9
 
10
/*
11
 * Licensed under the Apache License, Version 2.0 (the "License");
12
 * you may not use this file except in compliance with the License.
13
 * You may obtain a copy of the License at
14
 *
15
 *     http://www.apache.org/licenses/LICENSE-2.0
16
 *
17
 * Unless required by applicable law or agreed to in writing, software
18
 * distributed under the License is distributed on an "AS IS" BASIS,
19
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
 * See the License for the specific language governing permissions and
21
 * limitations under the License.
22
 */
23
 
24
/**
25
 * @name MarkerIconOptions
26
 * @class This class represents optional arguments to {@link createMarkerIcon},
27
 *     {@link createFlatIcon}, or {@link createLabeledMarkerIcon}. Each of the
28
 *     functions use a subset of these arguments. See the function descriptions
29
 *     for the list of supported options.
30
 * @property {Number} [width=32] Specifies, in pixels, the width of the icon.
31
 *     The width may include some blank space on the side, depending on the
32
 *     height of the icon, as the icon will scale its shape proportionately.
33
 * @property {Number} [height=32] Specifies, in pixels, the height of the icon.
34
 * @property {String} [primaryColor="#ff0000"] Specifies, as a hexadecimal
35
 *     string, the color used for the majority of the icon body.
36
 * @property {String} [cornerColor="#ffffff"] Specifies, as a hexadecimal
37
 *     string, the color used for the top corner of the icon. If you'd like the
38
 *     icon to have a consistent color, make the this the same as the
39
 *     {@link primaryColor}.
40
 * @property {String} [strokeColor="#000000"] Specifies, as a hexadecimal
41
 *     string, the color used for the outside line (stroke) of the icon.
42
 * @property {String} [shadowColor="#000000"] Specifies, as a hexadecimal
43
 *     string, the color used for the shadow of the icon.
44
 * @property {String} [label=""] Specifies a character or string to display
45
 *     inside the body of the icon. Generally, one or two characters looks best.
46
 * @property {String} [labelColor="#000000"] Specifies, as a hexadecimal
47
 *     string, the color used for the label text.
48
 * @property {Number} [labelSize=0] Specifies, in pixels, the size of the label
49
 *     text. If set to 0, the text auto-sizes to fit the icon body.
50
 * @property {String} [shape="circle"] Specifies shape of the icon. Current
51
 *     options are "circle" for a circle or "roundrect" for a rounded rectangle.
52
 * @property {Boolean} [addStar = false] Specifies whether to add a star to the
53
 *     edge of the icon.
54
 * @property {String} [starPrimaryColor="#FFFF00"] Specifies, as a hexadecimal
55
 *     string, the color used for the star body.
56
 * @property {String} [starStrokeColor="#0000FF"] Specifies, as a hexadecimal
57
 *     string, the color used for the outside line (stroke) of the star.
58
 */
59
 
60
/**
61
 * This namespace contains functions that you can use to easily create
62
 *     dynamically sized, colored, and labeled icons.
63
 * @namespace
64
 */
65
var MapIconMaker = {};
66
 
67
/**
68
 * Creates an icon based on the specified options in the
69
 *   {@link MarkerIconOptions} argument.
70
 *   Supported options are: width, height, primaryColor,
71
 *   strokeColor, and cornerColor.
72
 * @param {MarkerIconOptions} [opts]
73
 * @return {GIcon}
74
 */
75
MapIconMaker.createMarkerIcon = function (opts) {
76
  var width = opts.width || 32;
77
  var height = opts.height || 32;
78
  var primaryColor = opts.primaryColor || "#ff0000";
79
  var strokeColor = opts.strokeColor || "#000000";
80
  var cornerColor = opts.cornerColor || "#ffffff";
81
 
82
  var baseUrl = "http://chart.apis.google.com/chart?cht=mm";
83
  var iconUrl = baseUrl + "&chs=" + width + "x" + height +
84
      "&chco=" + cornerColor.replace("#", "") + "," +
85
      primaryColor.replace("#", "") + "," +
86
      strokeColor.replace("#", "") + "&ext=.png";
87
  var icon = new GIcon(G_DEFAULT_ICON);
88
  icon.image = iconUrl;
89
  icon.iconSize = new GSize(width, height);
90
  icon.shadowSize = new GSize(Math.floor(width * 1.6), height);
91
  icon.iconAnchor = new GPoint(width / 2, height);
92
  icon.infoWindowAnchor = new GPoint(width / 2, Math.floor(height / 12));
93
  icon.printImage = iconUrl + "&chof=gif";
94
  icon.mozPrintImage = iconUrl + "&chf=bg,s,ECECD8" + "&chof=gif";
95
  iconUrl = baseUrl + "&chs=" + width + "x" + height +
96
      "&chco=" + cornerColor.replace("#", "") + "," +
97
      primaryColor.replace("#", "") + "," +
98
      strokeColor.replace("#", "");
99
  icon.transparent = iconUrl + "&chf=a,s,ffffff11&ext=.png";
100
 
101
  icon.imageMap = [
102
    width / 2, height,
103
    (7 / 16) * width, (5 / 8) * height,
104
    (5 / 16) * width, (7 / 16) * height,
105
    (7 / 32) * width, (5 / 16) * height,
106
    (5 / 16) * width, (1 / 8) * height,
107
    (1 / 2) * width, 0,
108
    (11 / 16) * width, (1 / 8) * height,
109
    (25 / 32) * width, (5 / 16) * height,
110
    (11 / 16) * width, (7 / 16) * height,
111
    (9 / 16) * width, (5 / 8) * height
112
  ];
113
  for (var i = 0; i < icon.imageMap.length; i++) {
114
    icon.imageMap[i] = parseInt(icon.imageMap[i]);
115
  }
116
 
117
  return icon;
118
};
119
 
120
 
121
/**
122
 * Creates a flat icon based on the specified options in the
123
 *     {@link MarkerIconOptions} argument.
124
 *     Supported options are: width, height, primaryColor,
125
 *     shadowColor, label, labelColor, labelSize, and shape..
126
 * @param {MarkerIconOptions} [opts]
127
 * @return {GIcon}
128
 */
129
MapIconMaker.createFlatIcon = function (opts) {
130
  var width = opts.width || 32;
131
  var height = opts.height || 32;
132
  var primaryColor = opts.primaryColor || "#ff0000";
133
  var shadowColor = opts.shadowColor || "#000000";
134
  var label = MapIconMaker.escapeUserText_(opts.label) || "";
135
  var labelColor = opts.labelColor || "#000000";
136
  var labelSize = opts.labelSize || 0;
137
  var shape = opts.shape ||  "circle";
138
  var shapeCode = (shape === "circle") ? "it" : "itr";
139
 
140
  var baseUrl = "http://chart.apis.google.com/chart?cht=" + shapeCode;
141
  var iconUrl = baseUrl + "&chs=" + width + "x" + height +
142
      "&chco=" + primaryColor.replace("#", "") + "," +
143
      shadowColor.replace("#", "") + "ff,ffffff01" +
144
      "&chl=" + label + "&chx=" + labelColor.replace("#", "") +
145
      "," + labelSize;
146
  var icon = new GIcon(G_DEFAULT_ICON);
147
  icon.image = iconUrl + "&chf=bg,s,00000000" + "&ext=.png";
148
  icon.iconSize = new GSize(width, height);
149
  icon.shadowSize = new GSize(0, 0);
150
  icon.iconAnchor = new GPoint(width / 2, height / 2);
151
  icon.infoWindowAnchor = new GPoint(width / 2, height / 2);
152
  icon.printImage = iconUrl + "&chof=gif";
153
  icon.mozPrintImage = iconUrl + "&chf=bg,s,ECECD8" + "&chof=gif";
154
  icon.transparent = iconUrl + "&chf=a,s,ffffff01&ext=.png";
155
  icon.imageMap = [];
156
  if (shapeCode === "itr") {
157
    icon.imageMap = [0, 0, width, 0, width, height, 0, height];
158
  } else {
159
    var polyNumSides = 8;
160
    var polySideLength = 360 / polyNumSides;
161
    var polyRadius = Math.min(width, height) / 2;
162
    for (var a = 0; a < (polyNumSides + 1); a++) {
163
      var aRad = polySideLength * a * (Math.PI / 180);
164
      var pixelX = polyRadius + polyRadius * Math.cos(aRad);
165
      var pixelY = polyRadius + polyRadius * Math.sin(aRad);
166
      icon.imageMap.push(parseInt(pixelX), parseInt(pixelY));
167
    }
168
  }
169
 
170
  return icon;
171
};
172
 
173
 
174
/**
175
 * Creates a labeled marker icon based on the specified options in the
176
 *     {@link MarkerIconOptions} argument.
177
 *     Supported options are: primaryColor, strokeColor,
178
 *     starPrimaryColor, starStrokeColor, label, labelColor, and addStar.
179
 * @param {MarkerIconOptions} [opts]
180
 * @return {GIcon}
181
 */
182
MapIconMaker.createLabeledMarkerIcon = function (opts) {
183
  var primaryColor = opts.primaryColor || "#DA7187";
184
  var strokeColor = opts.strokeColor || "#000000";
185
  var starPrimaryColor = opts.starPrimaryColor || "#FFFF00";
186
  var starStrokeColor = opts.starStrokeColor || "#0000FF";
187
  var label = MapIconMaker.escapeUserText_(opts.label) || "";
188
  var labelColor = opts.labelColor || "#000000";
189
  var addStar = opts.addStar || false;
190
 
191
  var pinProgram = (addStar) ? "pin_star" : "pin";
192
  var baseUrl = "http://chart.apis.google.com/chart?cht=d&chdp=mapsapi&chl=";
193
  var iconUrl = baseUrl + pinProgram + "'i\\" + "'[" + label +
194
      "'-2'f\\"  + "hv'a\\]" + "h\\]o\\" +
195
      primaryColor.replace("#", "")  + "'fC\\" +
196
      labelColor.replace("#", "")  + "'tC\\" +
197
      strokeColor.replace("#", "")  + "'eC\\";
198
  if (addStar) {
199
    iconUrl += starPrimaryColor.replace("#", "") + "'1C\\" +
200
        starStrokeColor.replace("#", "") + "'0C\\";
201
  }
202
  iconUrl += "Lauto'f\\";
203
 
204
  var icon = new GIcon(G_DEFAULT_ICON);
205
  icon.image = iconUrl + "&ext=.png";
206
  icon.iconSize = (addStar) ? new GSize(23, 39) : new GSize(21, 34);
207
  return icon;
208
};
209
 
210
 
211
/**
212
 * Utility function for doing special chart API escaping first,
213
 *  and then typical URL escaping. Must be applied to user-supplied text.
214
 * @private
215
 */
216
MapIconMaker.escapeUserText_ = function (text) {
217
  if (text === undefined) {
218
    return null;
219
  }
220
  text = text.replace(/@/, "@@");
221
  text = text.replace(/\\/, "@\\");
222
  text = text.replace(/'/, "@'");
223
  text = text.replace(/\[/, "@[");
224
  text = text.replace(/\]/, "@]");
225
  return encodeURIComponent(text);
226
};
227
 
228