1. Home
  2. Computing & Technology
  3. JavaScript

Google Maps

Latitude and Longitude

There isn't much point in displaying Google Maps on your web page if all it displays is the area around the Google head office. To be useful your map needs to display the part of the world that you want to have displayed.

Google provide a number of different ways of specifying what the map is to display and each method works around defining which location is to appear at the exact centre of the map (so that the centre of the map doesn't move as you zoom in and out).

The simplest way to define a position (but perhaps the most difficult in working out what values to use is to define the centre location in terms of its lattitude and longitude. This of course requires that you know the exact latitude and longitude of what you want to display which is the hard part in using this method (although not perhaps as hard as it might appear to be).

Google have centred their example map on their headquarters building by specifying the latitude and longitude of the building in degrees to four decimal places. This means that even small adjustments to the map can be made.

The reason that setting up a map using the lattitude and longitude isn't as difficult as it looks is because you can always test your map page and make minor alterations to those numbers until you get the map centred on exactly the location that you want (it becomes even easier to fdo this if you place a marker in the centre of the map as we will look at doing in the next tutorial).

Let's say that instead of having our map centred on the Google headquarters that we want it centred on my house instead. By experimenting with the latitude and longitude figures I end up with the appropriate line in the code reading:

map.setCenter(new GLatLng((-33.9417, 150.9473), 10);

and the map now looks like this:

Showing exactly where on the map we are referring to is made easier if we add a Marker.

Explore JavaScript
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. JavaScript

©2009 About.com, a part of The New York Times Company.

All rights reserved.