Give Your Applications Mapping Capabilities, Part 3 : Page 2

Working with Markers The most-used feature of GM is its ability to show markers on a map. For example, you might show a marker to indicate a point of sale, a company address, or a location related to your business or application. You can refer to Parts 1 and 2 of this article to see other scenarios.

You create a marker using the GMarker object. The constructor requires a coordinate (a GLatLng instance); you can add the new GMarker object to your map object as an overlay. In fact, every object you add to a map is an overlay. To extend the previous example:

map.addOverlay(new GMarker(new GLatLng(myLatitude, myLongitude)));

The preceding code does several things. Reading from the inside parentheses outward, it first creates a GLatLng coordinate object using two decimal variables (myLatitude and myLongitude—the input parameter values were declared earlier), then it creates a new GMarker positioned at this coordinate, and finally adds the marker as an overlay to the previously created map.

You can repeat this operation as many times as you wish to create additional markers on your map.

By default, markers are relatively plain, but you can define your own marker icon if you don't want to use the standard GM one. To do that, you create a GIcon object, and then create a marker using that object:

In the preceding code, iconAnchor, shadowSize, and so on, are attributes that control how GM shows visual effects related to your icon. This can get quite complex, so I suggest you start from this simple example and make changes as needed, or refer to the complete GM API documentation.

Working with Lines To show a line, you simply use an array of points defined with GLatLng objects. The points define a connected set of line segments. GM will connect every pair of points with a direct line segment, so anything except a straight line will always require a sequence of linear segments. GM has no concept of curves—in other words, GM (like Google Earth) doesn't provide a way to draw a "real" curved line, but you can approximate curved lines with close-set points. The more points you provide, the better the approximation to a true curve.

Here's an example showing the JS code required to draw a line (termed a "PolyLine" in GM parlance):

In the preceding code, the PolyLine constructor takes an array of GLatLng objects, and draws a set of line segments connecting those coordinates. The PolyLine constructor also requires a line color and width (in pixels).

The PolyLine object exposes its own set of properties and methods. The most important are getLength(), which returns the length (in meters) of the line along the Earth's surface, and the hide() and show() methods that hide or show the line.