load maps key from a configuration file

46 views
Skip to first unread message

koalina

unread,
Feb 17, 2009, 4:35:50 AM2/17/09
to Google API Libraries for GWT
hi all,
I would like to retrieve the google maps key from a configuration file
on serverside and then associate it whit the actual MapWidget
object... Is there a solution to avoid to find replace in compiled
js?
thx a lot, have a nice day,
Michela

Nicolas Garnier

unread,
Feb 17, 2009, 5:12:49 AM2/17/09
to gwt-goo...@googlegroups.com
Hey Kaolina,

I guess you could make your file accessible to the client (a URL) and read it from the client using a RequestBuilder (http://google-web-toolkit.googlecode.com/svn/javadoc/1.5/com/google/gwt/http/client/RequestBuilder.html)

This solution is nice because you can modify the key without recompiling and deploying...

You can also use ImmutableResourceBundle from gwt-incubator but basically that will include the code into the compiled JS AFAIK. You use something like that:

public interface Resources extends ImmutableResourceBundle {
  public static final Resources INSTANCE = (Resources) GWT.create(Resources.class);

  @Resource("com/google/myapp/key.txt")
  public TextResource key();
}


Then anywhere you need your key you can do a : Resources.INSTANCE.key().getText()

Cheers,
Nicolas
--
Nicolas Garnier
Developer Programs Engineer
Google Ireland Ltd.  Gordon House, Barrow Street, Dublin 4, Ireland
Registered in Dublin, Ireland | Registration Number: 368047


The above terms reflect a potential business arrangement, are provided solely as a basis for further discussion, and are not intended to be and do not constitute a legally binding obligation. No legally binding obligations will be created, implied, or inferred until an agreement in final form is executed in writing by all parties involved.

This email may be confidential or privileged. If you received this communication by mistake, please don't forward it to anyone else, please erase all copies and attachments, and please let me know that it went to the wrong person. Thanks

koalina

unread,
Feb 17, 2009, 5:19:42 AM2/17/09
to Google API Libraries for GWT
ok, a possible solution to read a configuration file is also through
rpc...and then how can i associate the MapWidget object to the correct
key?
thx
Michela

On 17 Feb, 11:12, Nicolas Garnier <ni...@google.com> wrote:
> Hey Kaolina,
>
> I guess you could make your file accessible to the client (a URL) and read
> it from the client using a RequestBuilder (http://google-web-toolkit.googlecode.com/svn/javadoc/1.5/com/google/g...
> )
>
> This solution is nice because you can modify the key without recompiling and
> deploying...
>
> You can also use ImmutableResourceBundle from gwt-incubator but basically
> that will include the code into the compiled JS AFAIK. You use something
> like that:
>
> public interface Resources extends ImmutableResourceBundle {
>   public static final Resources INSTANCE = (Resources)
> GWT.create(Resources.class);
>
>   @Resource("com/google/myapp/key.txt")
>   public TextResource key();
>
> }
>
> Then anywhere you need your key you can do a :
> Resources.INSTANCE.key().getText()
>
> Cheers,
> Nicolas
>
> On Tue, Feb 17, 2009 at 9:35 AM, koalina <koalin...@gmail.com> wrote:
>
> > hi all,
> > I would like to retrieve the google maps key from a configuration file
> > on serverside and then associate it whit the actual MapWidget
> > object... Is there a solution to avoid to find replace in compiled
> > js?
> > thx a lot, have a nice day,
> > Michela
>
> --
> *Nicolas Garnier*
> Developer Programs Engineer
> *Google *Ireland Ltd.  *Gordon House, Barrow Street, Dublin 4, Ireland
> Registered in Dublin, Ireland | Registration Number: 368047*
>
> *The above terms reflect a potential business arrangement, are provided
> solely as a basis for further discussion, and are not intended to be and do
> not constitute a legally binding obligation. No legally binding obligations
> will be created, implied, or inferred until an agreement in final form is
> executed in writing by all parties involved.
>
> This email may be confidential or privileged. If you received this
> communication by mistake, please don't forward it to anyone else, please
> erase all copies and attachments, and please let me know that it went to the
> wrong person. Thanks*

Eric Ayers

unread,
Feb 17, 2009, 6:36:23 AM2/17/09
to gwt-goo...@googlegroups.com
I have a patch out for review with the Ajax loader API

http://galgwt-reviews.appspot.com/3604/show

This class will use script injection to load the Maps API after your
GWT application starts up. I think the best way to associate the key
is to parse Window.location and have some kind of map of keys to URL.
--
Eric Z. Ayers - GWT Team - Atlanta, GA USA
http://code.google.com/webtoolkit/

koalina

unread,
Feb 17, 2009, 8:18:37 AM2/17/09
to Google API Libraries for GWT
sorry eric but I can't see how apply this to the mapWidget... script
src is declared in main.gwt.xml and then compiled in js.
thx for help,
Michela

Eric Ayers

unread,
Feb 17, 2009, 8:34:04 AM2/17/09
to gwt-goo...@googlegroups.com
The AjaxLoader eliminates all need for adding the script to the
main.gwt.xml. Check out the demo - you'll see no <script> tags there.

koalina

unread,
Feb 17, 2009, 9:12:15 AM2/17/09
to Google API Libraries for GWT
sorry eric, but I have the Main.gwt.xml where I put <script
src="http://maps.google.com/maps?
file=api&amp;v=2&amp;key=ABQIAAAAUO34edsqljM2WeuC9Nog6RRIpen0A0_SeI0jb_jiblj7dPOYwRS1JBN0gnB9ziM9tkwB36NWjwJWFg" /
>
the problem is that, when I switch from developing server to the
deploy one, ip changes and then key isn't correct in this second case.
The main goal for me is to avoid find and replace in
file .....nocache, I would like to retrieve key from a configuration
file (via rpc or something else) and load maps api only after have
read this value. I can't see ho ajax loader can help me in this. I've
tried to write in Main.gwt.xml the following
<script src="js/loadApi.js" /> (instead of the previous tag)
where loadApi.js is:
document.write('<script src="http://maps.google.com/maps?
file=api&amp;v=2&amp;key=' +getId()+'></script>' );
function getId(){return
"ABQIAAAAUO34edsqljM2WeuC9Nog6RRIpen0A0_SeI0jb_jiblj7dPOYwRS1JBN0gnB9ziM9tkwB36NWjwJWFg";}
...with no luck..
any suggestions?
thx a lot,
Michela

Eric Ayers

unread,
Feb 17, 2009, 10:25:37 AM2/17/09
to gwt-goo...@googlegroups.com
So, what you would do is to not include any <script> tags in your module.xml::

public MyApp extends EntryPoint {

// Lookup the host in a Map and match it with a Maps API Key
private String lookupKey(String host) {
...
}

public void onModuleLoad() {
String key = lookupKey (Window.Location.getHost());
AjaxLoader.init(key);
AjaxLoaderOptions options = AjaxLoaderOptions.newInstance();
options.setCallback(new Runnable() {
public void run () {
// Now you can create your maps interface and call the Maps API methods
j ...
}
}
AjaxLoader.loadApi(maps, "2", options);

koalina

unread,
Feb 17, 2009, 11:11:47 AM2/17/09
to Google API Libraries for GWT
Hi Eric,
thx I'm going on the right way...
I've copied the following sorce-code files in my project:
AjaxLoader.java
ArrayHelper.java
ExceptionHelper.java
the package is com.google.gwt.ajaxloader.client
then, in the class where I called the MapWidget, I've put the code tou
suggest.
What module have I to inhertit?
then, is it correct to add in html file:
<head>
<meta name='gwt:module' content='tsf.ccn.Main=tsf.ccn.Main'>
<title>ttt</title>
<link rel="shortcut icon" href="tsf.ccn.Main/images/
favicon.ico" />
<!--170209Miki: importo script Visualization -->
<script type="text/javascript" src="http://www.google.com/
jsapi"></script>
<script language="javascript" src="tsf.ccn.Main/
tsf.ccn.Main.nocache.js"></script>
</head>
<body>
and nothing else?
another question... the first parameter in case of google maps api, is
correct?
AjaxLoader.loadApi("maps", "2", options);

thx again,
Michela

Eric Ayers

unread,
Feb 17, 2009, 11:32:27 AM2/17/09
to gwt-goo...@googlegroups.com
You will need to copy the file: com.google.gwt.ajaxloader.AjaxLoader.gwt.xml

The loadApi method wraps the google.load() method documented here for Maps:

http://code.google.com/apis/maps/documentation/#AJAX_Loader

You should always use the callback argument in the Options object when
using the AjaxLoader with GWT so that you don't try to instantiate
Maps API objects before the Maps JS Api is finished loading.

koalina

unread,
Feb 18, 2009, 3:11:24 AM2/18/09
to Google API Libraries for GWT
Hi Eric,
thank you for help, I'm close by the solution.
There's still only a problem... when the page is loading I get the
message "this key... etc etc ...apis/maps" but then the page is
correctly loaded.
The key is the correct one. I've put in the head of the html file the
following lines:
<script type="text/javascript" src="http://www.google.com/jsapi"></
script>
<script language="javascript" src="tsf.ccn.Main/
tsf.ccn.Main.nocache.js"></script>
is it an issue?
thanx
have a nice day,
Michela


koalina

unread,
Feb 18, 2009, 3:12:41 AM2/18/09
to Google API Libraries for GWT
Hi Eric,
thank you for help, I'm close by the solution.
There's still only a problem... when the page is loading I get the
message "this key... etc etc ...apis/maps" but then the page is
correctly loaded.
The key is the correct one. I've put in the head of the html file the
following lines:
<script type="text/javascript" src="http://www.google.com/jsapi"></
script>
<script language="javascript" src="tsf.ccn.Main/
tsf.ccn.Main.nocache.js"></script>
is it an issue?
thanx
have a nice day,
Michela


On 17 Feb, 17:32, Eric Ayers <zun...@google.com> wrote:

koalina

unread,
Feb 18, 2009, 3:38:45 AM2/18/09
to Google API Libraries for GWT
Hi Eric,
thank you for help, I'm close by the solution.
There's still only a problem... when the page is loading I get the
message "this key... etc etc ...apis/maps" but then the page is
correctly loaded.
The key is the correct one. I've put in the head of the html file the
following lines:
<script type="text/javascript" src="http://www.google.com/jsapi"></
script>
<script language="javascript" src="tsf.ccn.Main/
tsf.ccn.Main.nocache.js"></script>
is it an issue?
thanx
have a nice day,
Michela


On 17 Feb, 17:32, Eric Ayers <zun...@google.com> wrote:

koalina

unread,
Feb 18, 2009, 3:39:19 AM2/18/09
to Google API Libraries for GWT
sorry for posting twice... my mistake ....

koalina

unread,
Feb 18, 2009, 5:39:53 AM2/18/09
to Google API Libraries for GWT
Still have the problem with the key... Looking at the AjaxLoader code
I see
public static void loadApi(final String api, final String version,
final AjaxLoaderOptions settings) {

// Initialize the API if it hasn't already been initialized.
init();


.. is it correct? in case of google maps api isn't the method with the
key parameter to be called?
however, even if i commetn the this line, i have the same behaviour...
warning popup saying to retrieve another key, then application
correctly loaded (cache cleaned, tomcat restarted etc etc)
In MainEntryPoint, onModuleLoad() method i have the following code:
String key =
"ABQIAAAAUO34edsqljM2WeuC9Nog6RRIpen0A0_SeI0jb_jiblj7dPOYwRS1JBN0gnB9ziM9tkwB36NWjwJWFg";
AjaxLoader.init(key);
AjaxLoaderOptions options = AjaxLoaderOptions.newInstance();
options.setCallback(new Runnable() {

public void run() {

..... code using MapWidget object
}

});

AjaxLoader.loadApi("maps", "2", options);

then in html this:

<html>
<head>
<!--meta name='gwt:module'
content='tsf.ccn.Main=tsf.ccn.Main'-->
<title>CCN_Browser, versione 6.0.0.0</title>
<link rel="shortcut icon" href="tsf.ccn.Main/images/
favicon.ico" />
<!--170209Miki: importo script Visualization -->
<script type="text/javascript" src="http://www.google.com/
jsapi"></script>
<meta name='gwt:module' content='tsf.ccn.Main=tsf.ccn.Main'>
<script language="javascript" src="tsf.ccn.Main/
tsf.ccn.Main.nocache.js"></script>
</head>
<body>

<!--script language="javascript" src="tsf.ccn.Main/
tsf.ccn.Main.nocache.js"></-->

</body>
</html>

thx for help,
Michela

koalina

unread,
Feb 18, 2009, 9:55:57 AM2/18/09
to Google API Libraries for GWT
could it be because the server works behind a proxy? but then, after
the alert is shown, the site is correctly loaded...please help...I've
tried with the following html:
<html>
<head>

<title></title>
<link rel="shortcut icon" href="tsf.ccn.Main/images/
favicon.ico" />

</head>
<body>
<script src="tsf.ccn.Main/tsf.ccn.Main.nocache.js" type="text/
javascript"></script>


</body>
</html>

... but no luck... always alert showned before loading site...
thxm
Michela
> ...
>
> leggi tutto

koalina

unread,
Feb 18, 2009, 10:01:49 AM2/18/09
to Google API Libraries for GWT
the really strange thing I notice is that every key I put, even a
dummy one, the behaviour is always the same.... alert and then site
loaded correctly.... how is it possible? O___o
tyhx,
Michela
> ...
>
> leggi tutto

Eric Ayers

unread,
Feb 18, 2009, 10:03:29 AM2/18/09
to gwt-goo...@googlegroups.com
On Wed, Feb 18, 2009 at 3:11 AM, koalina <koal...@gmail.com> wrote:
>
> Hi Eric,
> thank you for help, I'm close by the solution.
> There's still only a problem... when the page is loading I get the
> message "this key... etc etc ...apis/maps" but then the page is
> correctly loaded.
> The key is the correct one. I've put in the head of the html file the
> following lines:
> <script type="text/javascript" src="http://www.google.com/jsapi"></
> script>

You don't need this line. When you add this line, you are loading the
API without a key set, hence the message.

koalina

unread,
Feb 18, 2009, 10:14:43 AM2/18/09
to Google API Libraries for GWT
I've deleted this line, but the error is still the same...

On 18 Feb, 16:03, Eric Ayers <zun...@google.com> wrote:

Eric Ayers

unread,
Feb 18, 2009, 10:18:22 AM2/18/09
to gwt-goo...@googlegroups.com
Sorry, there is a bug:

In AjaxLoader.java, the line:

String key = (apiKey == null) ? "" : ("key" + apiKey + "&");

should be

String key = (apiKey == null) ? "" : ("key=" + apiKey + "&");

I corrected this in my local copy but neglected to update the patch.

koalina

unread,
Feb 18, 2009, 10:28:18 AM2/18/09
to Google API Libraries for GWT
ok, now I have
String key = (apiKey == null) ? "" : ("key" + apiKey + "&");
String src = "http://www.google.com/jsapi?" + key
+ "callback=__gwt_AjaxLoader_onLoad";

...but still the error... I've tried with ip and ip:port...but always
the same result
thx,
Michela

On 18 Feb, 16:18, Eric Ayers <zun...@google.com> wrote:
> Sorry, there is a bug:
>
> In AjaxLoader.java, the line:
>
>   String key = (apiKey == null) ? "" : ("key" + apiKey + "&");
>
> should be
>
>  String key = (apiKey == null) ? "" : ("key=" + apiKey + "&");
>
> I corrected this in my local copy but neglected to update the patch.
>
> ...
>
> leggi tutto

Eric Ayers

unread,
Feb 18, 2009, 10:29:13 AM2/18/09
to gwt-goo...@googlegroups.com
Change "key" to "key=" in that line

koalina

unread,
Feb 18, 2009, 10:30:32 AM2/18/09
to Google API Libraries for GWT
sorry... String key = (apiKey == null) ? "" : ("key=" + apiKey +
"&"); and the same error (however ..it was already online)
> ...
>
> leggi tutto

koalina

unread,
Feb 18, 2009, 10:50:28 AM2/18/09
to Google API Libraries for GWT
I'm afraid the problem is that I'm working on a local server, with
local ip. Without AjaxLoader it runs, but the key is then coded in an
obfuscated js; with ajaxLoaded I retrieve the key with rpc method, but
the page is loaded correctly only *after* have showned the alert..

Michela
> ...
>
> leggi tutto

Eric Ayers

unread,
Feb 18, 2009, 10:55:31 AM2/18/09
to gwt-goo...@googlegroups.com
With the AJAX loader, you shouldn't invoke any calls to the Maps API
until your callback passed into AjaxLoader.loadApi() returns. Could
that be the problem?

koalina

unread,
Feb 18, 2009, 11:04:43 AM2/18/09
to Google API Libraries for GWT
It doesn't seem to be the problem..
public void onModuleLoad() {

lookupKey(Window.Location.getHost());


}

public void createInterface()
{

....use Map Widget
}


private void lookupKey(String host) {


getServiceConf().getGoogleKey(host, new AsyncCallback(){
public void onFailure(Throwable caught) {
GWT.log("Errore nella chiamata al metodo asincrono
getGoogleKey()", null);

}

public void onSuccess(Object result) {
GWT.log("Letta chiave: "+(String) result, null);
googleKey = (String)result;
AjaxLoader.init(googleKey);



AjaxLoaderOptions options =
AjaxLoaderOptions.newInstance();
options.setCallback(new Runnable() {

public void run() {

createInterface();
}

});

AjaxLoader.loadApi("maps", "2", options);



}
});

}

On 18 Feb, 16:55, Eric Ayers <zun...@google.com> wrote:
> With the AJAX loader, you shouldn't invoke any calls to the Maps API
> until your callback passed into AjaxLoader.loadApi() returns.    Could
> that be the problem?
>
> ...
>
> leggi tutto

Eric Ayers

unread,
Feb 18, 2009, 11:47:25 AM2/18/09
to gwt-goo...@googlegroups.com
Hello Michela,

I just updated the patch at:
http://galgwt-reviews.appspot.com/3604/show to include the bugfix and
a sample of how to load the Maps API. Here are some things I can
think of that might be causing your key problem:

- Are you hitting compile/browse after you update your source code?
If you don't you'll be looking at a stale version of your app when you
hit it from another web browser other than hosted mode.
- Are you using exactly the same URL? The fully qualified domain name
and port (if there is one) must match exactly to the one you used when
you generated the key.

-Eric.

koalina

unread,
Feb 18, 2009, 2:47:48 PM2/18/09
to Google API Libraries for GWT
perhaps there's a conflict while manage the google maps api for
gwt ... in the example you update, there's no reference to the
MapWidget object... GMap object is declared in js native code... is it
important?in the version "running", I had in the main.gwt.xml the
script src tag set with the reference to the google maps api (just as
explained in the getting started for the module "google maps api for
gwt")... now I've deleted this line, in main.gwt.xml there's still the
inherits tag pointing to Google maps module for gwt..the really
strange thing happening in version with AjaxLoader is that the alert
box is showned but, after ok is clicked, everything is correctly
loaded.I really can't get a solution for this problem...I've tried a
key for ip and another for ip:port (app is running on tomcat server),
then another one dummy but *always* the same behaviour...Tomorrow I
will post a bit of code to better explain... thx again, good evening,
Michela

On Feb 18, 5:47 pm, Eric Ayers <zun...@google.com> wrote:
> Hello Michela,
>
> I just updated the patch at:http://galgwt-reviews.appspot.com/3604/showto include the bugfix and
> a sample of how to load the Maps API. Here are some things I can
> think of that might be causing your key problem:
>
> - Are you hitting compile/browse after you update your source code?
> If you don't you'll be looking at a stale version of your app when you
> hit it from another web browser other than hosted mode.
> - Are you using exactly the same URL? The fully qualified domain name
> and port (if there is one) must match exactly to the one you used when
> you generated the key.
>
> -Eric.
>
> ...
>
> read more »

koalina

unread,
Feb 19, 2009, 3:20:11 AM2/19/09
to Google API Libraries for GWT
Thx Eric, problem solved :-D

I've dowloaded you new patch, put the following code:

AjaxLoader.init(googleKey);

AjaxLoader.loadApi("maps", "2", new Runnable() {

public void run() {
createInterface();
}

}, null);

instead of
// AjaxLoader.init(key);
// AjaxLoaderOptions options = AjaxLoaderOptions.newInstance();
// options.setCallback(new Runnable() {
//
// public void run() {
//
// createInterface();
// }
//
// });
//
// AjaxLoader.loadApi("maps", "2", options);

..the problem seems to be caused by the AjaxLoaderOptions class

the key is unique for ip AND port ;-)

thx again, have a nice day!
Michela

On 18 Feb, 20:47, koalina <koalin...@gmail.com> wrote:
> perhaps there's a conflict while manage the google maps api for
> gwt ... in the example you update, there's no reference to the
> MapWidget object... GMap object is declared in js native code... is it
> important?in the version "running", I had in the main.gwt.xml the
> script src tag set with the reference to the google maps api (just as
> explained in the getting started for the module "google maps api for
> gwt")... now I've deleted this line, in main.gwt.xml there's still the
> inherits tag pointing to Google maps module for gwt..the really
> strange thing happening in version with AjaxLoader is that the alert
> box is showned but, after ok is clicked, everything is correctly
> loaded.I really can't get a solution for this problem...I've tried a
> key for ip and another for ip:port (app is running on tomcat server),
> then another one dummy but *always* the same behaviour...Tomorrow I
> will post a bit of code to better explain... thx again, good evening,
> Michela
>
> On Feb 18, 5:47 pm, Eric Ayers <zun...@google.com> wrote:
>
> > Hello Michela,
>
> > I just updated the patch at:http://galgwt-reviews.appspot.com/3604/showtoinclude the bugfix and
> ...
>
> leggi tutto
Reply all
Reply to author
Forward
0 new messages