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 />

Time for code to run<br />

Synchronous method calls<br />

methodA is executed<br />

methodB is executed<br />

Asynchronous method calls<br />

methodA is executed<br />

methodB is executed<br />

Figure 2-1. The difference between synchronous and asynchronous method calls<br />

In the asynchronous method call, methodB doesn’t have to wait for methodA to finish. This is great<br />

because you get better performance, but it also means that you can’t rely on a method call to be finished<br />

when you invoke another method later in the code. This behavior has consequences in how you use v3.<br />

In some cases, you need to check whether an object is properly initialized before you can interact with it,<br />

whereas in v2 you didn’t have to do this because it all happened sequentially.<br />

One example of this is when you need to access the bounds of the map. You can’t just call the<br />

getBounds() method of the Map object after you’ve initialized the map, because at that point the map<br />

isn’t ready and it doesn’t have any bounds. Instead, you have to listen to the bounds_changed event<br />

before trying to grab it. The bounds_changed event fires whenever the bounds of the map have changed.<br />

One of those occurrences is when the map has loaded. It also occurs whenever you pan or zoom<br />

the map.<br />

google.maps.event.addListener(map, 'bounds_changed', function() {<br />

var bounds = map.getBounds();<br />

});<br />

Converting from Version 2 to 3<br />

In this section, you will learn how to perform basic tasks such as creating a map and adding markers and<br />

also compare how these tasks are done in v2 vs. v3.<br />

Adding a Reference to the <strong>API</strong><br />

The most significant change here is that you no longer need an <strong>API</strong> key. This is really convenient since<br />

you don’t have to create a new <strong>API</strong> key for every domain that you want to use <strong>Google</strong> <strong>Maps</strong> on.<br />

9

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

Saved successfully!

Ooh no, something went wrong!