Google Maps API

I started working some with the Google Maps API, and it’s pretty nice. The documentation is decent, the examples are good, etc. I realized that I’ve come to expect this from Google, so I wanted to take the time instead to point out that this is an exceptional product. For example, I began to look into making the scroll wheel zoom on my maps like they do on the Google Maps site. What does it entail? One line:

map.enableScrollWheelZoom();

However, as great of a product as it is, it seems to be lacking some things. For example, if I want an info window that gives the options to get directions to or from that location, I have to do all that myself. I have to generate the links, and make them replace the content of the info window with a form that I have to make, and I have to make that form run some JavaScript when executed to get the directions. Why? This has GOT to be a common task. Why not build it into the API? However, in the end, that pales in comparison to the real problem. There is no way to validate a Google Maps API key! Instead, you have to load JavaScript using that key, and it uses an alert to announce that the key was bad! No one wants a JavaScript alert to pop up on their page! Since I’m making a WordPress plugin out of this (teaser!), I needed to make sure that the users of my plugin wouldn’t have this issue. I ended up having to override the alert function on the options page for my plugin like this:

var KillAlerts = true;
var realAlert = alert;
var alert = new Function('a', 'if(!KillAlerts){realAlert(a)}');

Then I added a function that runs on page load, re-enables the alerts, and checks if the key was valid:

function load()
{
    // Re-anable alerts
    KillAlerts = false;

    if (GBrowserIsCompatible()) {
        // Key is valid
    } else {
        if (G_INCOMPAT) {
            // Key is NOT valid.
        } else {
            // Can't tell if the Google API Key is valid, due to the browser not being compatible with the Google Maps API.
        }
    }
}

There is no reason that it should be that complicated. They should have a web service where I can send a request to verify a key, or their script should set a global variable rather than send an ugly alert! In the end, it’s a great product, but I would have expected that these kind of rough edges would have been taken care of by now. Google, you make me sad.

About Aaron D. Campbell

Owner and lead developer at BlueDog, Aaron has 10+ years of web development experience, it a regular core contributor to the WordPress project, and has released many WordPress plugins.
This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

6 thoughts on “Google Maps API

  1. Pamela Fox says:

    Hi Adam-

    The reason we can’t have a key verification service is that we don’t store a list of keys anywhere. The key is merely a hash of your domain and your google account, and the key is checked against window.location.host when the script is loaded into the page. The best thing to do is always register for http://domain.com, as this yields the most flexibility (subdomains, ports, folders, etc).

    I suppose we could have a global variable, but I think we’d still have to have an alert – many newbie programmers wouldn’t think to check the state of the global variable, and would be stuck in a state of confusion over their lack of a map.

    Regardless, feel free to file feature requests at gmaps-api-issues.googlecode.com.

    - pamela

  2. Pamela:
    Thanks for your comment. I realize that you have to cater to everyone, including those that have never programmed at all and want maps on their site (and I must admit that I often overlook this group). However, the more I think about it though, the more I think there should be a solution, it’s just not as simple as changing from an alert to a global variable. Instead, it’s a two step process. First, you need to allow your user to set a global before they include your JavaScript (similar to how you set your Google Analytics key). Depending on the value of the global, you would either send an alert (default), or set another global (or reset the same, so as not to further pollute the global namespace). You could even make a third setting which would do both (set the global AND send the alerts). I’m out today, but I’ll try to log a request as soon as I can.

  3. Nice workaround – stumbled across this looking for a way of supressing alerts for a script I'm customising, and this did the trick perfectly! Thanks!

  4. Thanks, it took me a while to figure it out, then I wanted to smack myself because it was so simple!

  5. y920 says:

    Very Good Explanation L:-)

  6. Mark says:

    Not too technical and I do no get my head deep into the programming but nice little explanation!

    Thanks for the blog and good to follow.

Leave a Reply

Your email address will not be published. Required fields are marked *

Note: If you are replying to another commenter, click the "Reply to {NAME} ↵" button under their comment!