27.10.2014 Views

Google Maps API 3

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 2 ■ TRANSFERRING FROM VERSION 2 TO 3<br />

Extensive Use of Object Literals<br />

Another difference is that in v3 object literals are almost exclusively used to pass parameters. I think that<br />

this is brilliant since it makes the <strong>API</strong> consistent and makes it really easy to extend. Version 2 also used<br />

objects and object literals to pass parameters but to a lesser extent and less consistently.<br />

For example, when creating a new marker, all parameters are passed as an object literal, including<br />

the position:<br />

var marker = new google.maps.Marker({<br />

position: new google.maps.LatLng(40.756, -73.986),<br />

map: map,<br />

title: 'A marker'<br />

});<br />

This makes the <strong>API</strong> both more consistent and more easily extendable. Imagine, for example, that in<br />

the future a need arises for a parameter that adds a marker at several locations on the same map.<br />

Extending the <strong>API</strong> with this would then be as simple as adding a positions property to the options object<br />

that would take an array of google.maps.LatLng objects as its value. This addition would feel natural and<br />

wouldn’t break any other functionality.<br />

■ Warning When creating an object literal, be sure not to have a comma after the last property since it will make<br />

Internet Explorer choke.<br />

Asynchronous by Nature<br />

The v2 <strong>API</strong> relied heavily on synchronous method calls. This made it hard to modularize the <strong>API</strong> and was<br />

probably the biggest reason for the total remake. The new <strong>API</strong> is asynchronous by nature, which allows it<br />

to be modularized.<br />

What’s the point of modularizing the <strong>API</strong>? The answer is simply performance. The old <strong>API</strong> had to<br />

load big parts of the <strong>API</strong> before displaying a simple map, even parts of the <strong>API</strong> that weren’t used.<br />

The new <strong>API</strong> being modularized only needs to load the necessary parts before initializing the map.<br />

Therefore, the perceived performance is much better; in other words, the map shows up on the web<br />

page much faster.<br />

Synchronous vs. Asynchronous<br />

When using the synchronous method, everything happens in a sequence. If you call methodA, it must<br />

finish running before methodB is invoked. If the methods instead are asynchronous, you can call methodA<br />

and methodB, and they can run parallel to each other.<br />

Consider this example where you invoke two methods after one another:<br />

methodA();<br />

methodB();<br />

methodA takes longer than methodB to run. Figure 2-1 shows how they would execute using<br />

synchronous vs. asynchronous method calls.<br />

8

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!