Labels

Sunday 14 April 2013

Google Maps JavaScript API V2 to V3

Currently (at work) I have been given the task of upgrading our Google maps API from version 2 to version 3. This is because the JavaScript API that we use is becoming deprecated as of May 19th 2013, as seen here.

Now I know you're thinking, why as a Java Developer would I be given a web development task? Well the reason is that I currently work in a very small company which has a "lets all muck in" attitude, this suits me just fine as it's good to get a bit of experience in everything.

The company I work for provides real time traffic information so we use the Google maps to pin-point locations.
Upgrading from version 2 to version 3 wasn't really that hard. The first thing to do is make sure you're using the correct JavaScript file:

http://maps.google.com/maps/api/js?v=3&sensor=set_to_true_or_false&key=your_api_key

As you can see they have added a "sensor" parameter to the request. From what I understand the sensor parameter is set to true if you have a GPS sensor,this is used to determine the user's location. (So I'm guessing this is more for mobile applications). The things to remember are the following;


Drop the G's

Google seems to have replaced the old object 'GMap2' for an object referenced by a namespace 'google.maps.Map'. Most of the objects seems to be the namespace followed by the original object minus the 'G' for eaxmple:
  • 'GMarker' becomes 'google.maps.Marker'
  • 'GEvent' becomes 'google.maps.Event'
    You get the idea..

    Now I'm not a JavaScript guru but I would think the reason for doing this is to stop the overuse of global variables. I've found that using global variables in JavaScript can cause some pretty nasty problems, namespaces seem to offer an almost object orientated approach which I think would work a lot better.

    Check for deprecated Objects

    Just renaming a few things isn't going to work...
    Version 2 has quite a few objects which are no longer supported in version 3. For example if you wanted to use custom overlay types (as we do at work) then instead of inheriting from GOverlay version 3's custom overlays are inherited from OverlayView.

    EDIT
    When you update an API you should document all the changes you have made, kind of a no brainer really, although Google seems to have missed a few things. In version 2 of the API we have access to the 'getBoundsZoomLevel' object. This object does exactly what it says and it's quite useful when searching for specific icons on the map. Version 3 does not have this option and annoyingly they don't provide a way around this when upgrading from version 2. I have written a function which basically does what the old object used to, it seems to me that Google should really have provided a way them selves rather then not even mentioning it. Maybe somewhere on-line they do mention it, if you find anything please let me know...until then, the blood runs hot.

    There's quite a few little irritating things like this knocking about. For a slightly more detailed explanation try looking at the Removing Obsolete Code section of the API documentation.
  • No comments:

    Post a Comment