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 10 ■ LOCATION, LOCATION, LOCATION<br />

The Complete JavaScript Code for This Example<br />

Listing 10-9 shows complete JavaScript code for this example.<br />

Listing 10-9. The Complete JavaScript Code for Example 10-3<br />

(function() {<br />

// Loading the <strong>Google</strong> <strong>Maps</strong> <strong>API</strong><br />

google.load('maps', 3, {<br />

'other_params': 'sensor=false&language=en'<br />

});<br />

window.onload = function() {<br />

// Getting the position<br />

if (google.loader.ClientLocation.latitude && google.loader.ClientLocation.longitude) {<br />

// Defining the position<br />

var latLng = new google.maps.LatLng(google.loader.ClientLocation.latitude,<br />

google.loader.ClientLocation.longitude);<br />

// Creating the content for the InfoWindow<br />

var location = 'You are located in '<br />

location += google.loader.ClientLocation.address.city + ', ';<br />

location += google.loader.ClientLocation.address.country;<br />

} else {<br />

}<br />

// Providing default values as a fallback<br />

var latLng = new google.maps.LatLng(0, 0);<br />

var location = 'Your location is unknown';<br />

// Creating a map<br />

var options = {<br />

zoom: 2,<br />

center: latLng,<br />

mapTypeId: google.maps.MapTypeId.ROADMAP<br />

};<br />

map = new google.maps.Map(document.getElementById('map'), options);<br />

// Adding a marker to the map<br />

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

position: latLng,<br />

map: map<br />

});<br />

// Creating a InfoWindow<br />

var infoWindow = new google.maps.InfoWindow({<br />

234

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

Saved successfully!

Ooh no, something went wrong!