It doesn't matter how Google checks the key. Maybe it's a hash of the
hostname and your Google account. Maybe they have a big database of keys
somewhere. Actually, Andrew's reply answers that "maybe", but really the
exact technique they use is nothing that you or I need to be concerned with.
Google is happy to provide all the API keys we want to request.

The localhost hack is a *terrible* idea. Definitely not recommended at all.

Instead, just go get all the API keys you need, one for each domain/hostname
you want to use, and then use JavaScript code to select the correct key.
Here's one way to do it. Change the domain and hostname strings and the
'apikey' strings as needed:

    <script type="text/javascript">
        (function() {
            var key = {
                'example.com': 'apikey',
                'www.example.com': 'wwwapikey',
                'mytestmachine': 'anotherapikey',
                'localhost': 'andanotherapikey'
                '': ''
            }[location.host];

            document.write(
                '<script type="text/javascript" ',
                    'src="http://www.google.com/jsapi?key=',
                    key, '">',
                '<\/script>'
            );
        })();
    </script>

    <script type="text/javascript">
        google.load( 'jquery', '1.3.2' );
        google.load( 'maps', '2' );
    </script>

This code uses the Google Ajax Library API loader, but you can use the same
approach if you want to load the Maps API with a direct script tag. Just put
that URL in the document.write() call instead of the jsapi URL.

If you use this code, and you want your map to work with both the www and
non-www versions of your site URL, you'll need to get keys for both of those
and put them in the code above as shown with the example URLs. As Marcelo
mentioned earlier, a non-www key will work for the www domain (because it's
a subdomain), but this key selection code requires separate keys for each.

But that isn't really such a good idea either. For non-map-related reasons,
you really don't want people getting to your site both with and without the
www. Pick one or the other and use a permanent redirect. I'd suggest having
the non-www URL redirect to the www version.

For example, if you go here in your browser:

http://google.com/

you will see that it immediately redirects to:

http://www.google.com/

Your site should do the same.

-Mike

On Mon, Mar 29, 2010 at 10:59 AM, CharlesHarrison <
[email protected]> wrote:

> If it isn't stored anywhere, how do they check it's the correct one?
> On the page where you sign up for one, it suggests that the key is
> linked to and associated with your Google account:
>
> http://code.google.com/apis/maps/signup.html
> "You must have a Google Account  to get a Maps API key, and your API
> key will be connected to your Google Account."
>
> On Mar 29, 2:17 pm, Rossko <[email protected]> wrote:
> > > Seems rather wasteful to generate another one rather than have the
> > > current one altered, but if that's the only way to do it, so be it.
> >
> > The 'cost' is your time to type it in, and Google's time to calculate
> > it ( a millisecond or so).  That's it ; it isn't stored anywhere.
> > Thus, there is no method to alter or retrieve anything, you just get
> > another one.  No trees will be cut down.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Google Maps API" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<google-maps-api%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/google-maps-api?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Google Maps API" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-maps-api?hl=en.

Reply via email to