CHAPTER |
|
7 |
Markers: Identifying Locations on the Map |
InfoWindow
object and it’s customizable. You can control the width and the contents of the info window, but you can’t change its pointer, nor make the Close button with the X mark disappear. As for the contents of the info window, it can host an HTML document with formatted text, hyperlinks, and images.Markers with InfoWindows.html
sample application.google.maps.Marker
object and you set the two required properties: the marker’s location and the map on which the marker will be placed. Here’s the statement that places a marker at the Space Needle monument in Seattle:Marker
object’s constructor are its location and the map to which it belongs. If you place this statement in the page’s initialize()
function, the marker will be placed on the map as soon as the map is loaded. If you place the preceding statement in a button’s click
event, the marker will be placed at the same location when the button is clicked. If the user keeps clicking the same button, however, a new instance of the marker will be placed exactly on top of the previous one(s). Clearly, you need a mechanism to detect the presence of a marker and remove it from the map before adding a new instance of the same marker.setMap()
method with the null argument:Marker
object is the title
property, which is set to the text you want to appear in a tooltip box when the mouse is hovered over the marker. Modify the preceding marker by setting one more property, the title
property:draggable
property to true
.animation
property to apply a visual effect to your markers. This property can be set either to DROP
or to BOUNCE
. The two keywords are members of the google.maps.animation
class and you must use their fully qualified names in your code (i.e., google.maps.animation.DROP or google.maps.animation.BOUNCE
).DROP
setting causes the marker to drop from the top of the map and bounce for a few moments before reaching its final position. The BOUNCE
effect makes the marker bounce until you stop the animation.DROP
setting results in an effect that lasts for less than a second but is very noticeable. It’s also easy to use; you specify the animation type in the marker’s constructor:BOUNCE
effect is more complicated because you must provide a mechanism to end the animation. If you just set the animation
property to BOUNCE
in the constructor, you’ll end up with an annoying marker that keeps bouncing. To make a marker bounce, you must call the setAnimation()
method, passing the BOUNCE
effect as argument and provide a function to end the animation after a number of milliseconds. Here’s how you can animate a marker on the map that will stop bouncing after 1750 milliseconds:marker
variable. As soon as the animation starts, the second statement calls the setTimout()
method. The setTimeout()
function accepts two arguments: a function and a time interval. After the expiration of the time interval, the function specified by the first argument is executed.setAnimation()
function is discussed in detail in Chapter 18, where you will learn how to use this method to animate items on the map. You will find some examples of real animation techniques in this chapter. Here, we use the setTimeout()
function to just end an animation effect initiated by the setAnimation()
method of the Marker
object.Map with Many Markers.html
page. This page contains the locations of nearly 9,000 airports around the world and displays only the ones that fit in the current viewport. Below the map, there’s a table of the airports in the current viewport; users can click an airport name to view it on the map. The selected airport’s marker bounces for a second or so when its link in the table is clicked. You can open the project and view the implementation of the HighlightMarker()
function, which uses the technique just described.InfoWindow
object, and it displays additional information about the selected marker. The InfoWindow
may contain plain text or an HTML fragment with formatted text.InfoWindow
object exposes many properties, the most important being the content
property. This property is mandatory and all others are optional. Here’s a typical constructor of an InfoWindow
variable (which would appear on clicking a marker over New York):InfoWindow
object, sets its contents, but doesn’t place it on the map. To display the info window, you must call the InfoWindow
object's open()
method, passing two arguments: the map on which it will appear and the marker to which it applies. When displayed, the arrow of the info window will point at the marker specified in the open()
method. To display the preceding info window, you must call the open()
method as follows:NYMarker
is a Marker
object that has already been placed on the map.open()
method must be called in the marker’s click
event listener. The following statements create a marker located in Manhattan and associate an InfoWindow
object with it:addListener()
method. The first instance of the NYMarker
keyword is an argument to the addListener
method, and the second instance is an argument to the InfoWindow
object’s open()
method. It’s actually possible to click one marker and display the info window of another marker! Just make sure this doesn’t happen in your application.<div>
element and apply a style to this element. The info window will be sized accordingly, while at the same time you can display a small HTML fragment on the info window.<div>
element on your page and update its contents from within the marker’s click
event listener. This element can be anywhere on the page outside the map’s <div>
element, but it can also be a floating element over the map. This technique is demonstrated with an example in the last section of the chapter.icon
property, which allows you to specify your own icon for the marker This property can be set to the URL of an alternate icon or to a new Icon
object. There are many custom icons you can use with Google Maps on the Web and some of them are provided by Google. The best aspect of Google’s icons is that you can use them directly from Google’s servers. Most third-party markers must be copied to your web server before they’re used. The reason for this is that not too many people want to service so many requests. It goes without saying that you must also pay attention to the icon designer’s license terms. Not all icons are free!icon
property. The following is the URL of an airplane icon, which is used by the sample page Markers with Custom InfoWindows.html
, to indicate the locations of a few airports in Germany: http://google-maps-icons.googlecode.com/files/airport.png.Markers with Custom InfoWindows.html
web page, shown in Figure 7-3, demonstrates the most straightforward technique for placing multiple markers on the map. Each marker, which represents an airport in Germany, is represented by a Marker
object with a custom icon, and it has its own associated InfoWindow
object. As you can see in Figure 7-3, you can open all info windows at once; the application leaves it to the user to close the info windows they don’t need on the map. It takes some extra programming effort to close any open info window before opening another one; you will adjust the application’s code to display a single info window at any time.Marker
object that represents the marker of the Aachen airport. The marker’s position
and title
properties are set in the Marker
object’s constructor. It also sets the map
property so that the marker is displayed immediately on the map.InfoWindow
object to the marker so that users can click the marker and view additional information about the selected landmark. If you don’t care about the appearance of the text in the info window, you can display plain text. Plain text is too rough even for a sample application like this, so you should use HTML code to format your text. It’s common to embed the text into a <span>
or <div>
element, as in the following statement:click
event with a JavaScript function that opens the info window:currentWindow
, which points to the current info window. This variable must be declared at the script level, outside the function(s) that use it. The function that’s executed when the user clicks a marker must be modified a little, as shown here:currentWindow
variable to point to the info window that was just displayed. The next time the user clicks a marker, the InfoWindow
object pointed to by the currentWindow
variable will be closed before the new marker’s info window is opened.InfoWindow
object has a minimum size of 200 pixels, which makes info windows inappropriate for displaying a short description, or for use with a small font size. Another problem with the InfoWindow
object is that you can’t make the Close button disappear; there’s no option to remove the X icon from an info window.InfoWindow
object: the RichMarker
object. The RichMarker
object is rendered on the map as a rectangle that can display HTML fragments and has no arrow by default. You can create an arrow as a bitmap, but rich markers work best for placing labels on the map. The labels describe a rather large area of the map, as shown in Figure 7-4, rather than very specific locations. The application used to place the annotations on the map of Figure 7-4 is discussed in the next section.RichMarker
object’s basic properties are its contents and its location on the map. The contents are assigned to the content
property, and the exact location is determined by the position
property, which is a LatLng
object. The RichMarker’s anchor
property determines the edge of the rich marker that will be placed at the location specified by the position
property. The valid settings for the anchor
property have self-explanatory names as follows:RichMarker
object is implemented by the script in the following file, which you must reference in your script with the following <script>
directive (it must appear on a single line in your code):<div>
element that holds the label’s content. The following statement assigns an HTML fragment to the div1
variable. The text is a short description of an airport, but the div1
variable holds a lot of HTML code:RichMarker
object and set the label’s content
property to the variable with the label’s content and its position
property to the position
property of an existing marker:marker1
marker is clicked, set the RichMarker
object’s map
property from within the marker’s click
event handler:currentLabel
variable represents the RichMarker
object that’s currently active. This instance must be closed before you open a new one, just as you did with the InfoWindow
objects earlier in the chapter.RichMarker
object has no arrow and it’s used as a label to annotate maps. However, it can be used in the place of an info window to display additional information about the selected marker. The sample application RichMarker Demo.html
, shown in Figure 7-5, is identical to the Markers with InfoWindows.html
application, but instead of info windows it uses rich markers to display the same information. The application displays only one rich marker at a time, not multiple ones, as shown in Figure 7-5. The advantage of using the RichMarker
object is that the additional information is displayed exactly next to the marker. It has no Close button and you can control its appearance from within your script.<div>
element transparent by omitting the background-color
style attribute. The end result is to print the text right on the map. You must use a bold typeface and a color that stands out so the users will notice your custom labels. Clearly, this technique shouldn’t be used in areas of a map that are crowded with feature names. Even better, apply a custom style to the entire map, which will make your custom labels stand out.RichMarkers.html
application, shown in Figure 7-6, allows users to place rich markers on the map and set their contents through an interactive interface. This is the application used to generate the page shown in Figure 7-4. The buttons at the top of the page allow you to place a new caption on the map, edit or remove an existing caption, and finally create a new static web page that contains the map and the annotations, but not the editing buttons. The contents of the new web page that displays the same map and the annotations are shown in a <div>
element at the bottom of the page. Just copy the code and paste it into a new HTML file. The process of converting annotated maps to stand-alone web pages is known as map generation, and it’s a standard feature of any GIS application. In later chapters, you will see more examples of scripts that generate map-enabled web pages. The auto-generated maps can be hosted to a web server so that other users can view, but not edit, them.RichMarker
object with a default appearance and places it on the map.<div>
element in code and applies a style to it. The code sets the label’s initial appearance and places the new label at the center of the map. The user is allowed to change the attributes of the style on a pop-up window, and the new settings are applied to the <div>
element’s style, changing the appearance of the label on the map.<div>
element, whose visibility is turned on and off depending on the application’s status. The <div>
element that acts as a pop-up window is implemented with straight HTML code, which is shown in Listing 7-2.<div>
element that’s used as a pop-up window in an HTML applicationoverlay()
function, shown in Listing 7-3, populates the pop-up window with the selected label’s attributes and then displays it on top of the map. If the pop-up window is already visible, the script reads the user-supplied data from the various elements on the pop-up window and applies them to the currently selected label.overlay()
function applies the user-supplied values ot the mapoverlay()
function implements the pop-up window for editing the current marker by examining the visibility of the <div>
element. If the element is visible, it changes the settings of the RichMarker
object on the map. If not, it reads the settings of the same object and populates the pop-up window with the object's properties. The role of this function is to transfer the data that controls the appearance of the selected label into the pop-up window and the values supplied by the user on this window back to the selected label and change its appearance.TextArea
control below the map, as you can see in Figure 7-6. You can copy the text, paste it into a new text file, and save it with the .html extension to create a stand-alone web page with the same content. The Autogenerated Map.html
page in this chapter’s support material was generated by the RichMarkers.html
sample page.generateMap()
function in the RichMarkers project to see how it generates the static page with the annotated map. Of course, the page need not be static. You can embed code to generate listeners for the labels’ click
events and display additional data about the selected labels. You can make the page as interactive as you wish—just don’t give users the tools to keep customizing the map. You can store the auto-generated pages into a folder of your web server with meaningful filenames and let the users discover the pages they’re interested in.<div>
element and it’s not as elegant in appearance. The sample application shown in Figure 7-7 uses a tabular layout for the data, but any HTML code you can apply to this window goes. There are also jQuery components that you can use in your HTML pages to achieve this functionality, including shaded boxes with rounded corners, but this book isn’t about third-party components or HTML design. Moreover, the <div>
element that implements the floating window was designed manually, to avoid long listings with embedded styles. Actually, the page uses a jQuery component to make the window draggable, but this component is fairly easy to use in your script (it's also optional).Map with data window.html
application, included in this chapter’s support material. The data window is implemented with the following <div>
element:<div>
elements, one that contains the window title and the Minimize/Restore links, and another one that contains the contents of the window. Both elements are nested under another <div>
element, which is made draggable. The container element, the one with the ID window
, is made draggable with the following statement, which must appear at the script level, to be executed as soon as the page is loaded:<div>
element, so you must also embed the following jQuery scripts to your page:click
event:showInfo()
function, shown in Listing 7-3, populates the custom floating window with data regarding the selected city:InfoWindow()
function populates the floating data windowobjInfo
property. In addition to setting the main section of the floating window, this function also highlights the selected marker by assigning the icon of a red pin to the marker. The selected marker is stored in the currentMarker
script variable so that it can be reset to a white pin when another marker is selected. This highlighting technique will not work if you’re using differently colored pins to indicate another property, such as the city’s population.minimizeDataWindow()
function:maximizeDataWindow()
function with similar statements, which set the properties of the window to their original values.onchange
event. Use this technique to allow your application’s users to filter the markers on the map based on certain attributes. Such attributes could be the population of a city, the category of an airport (international, military, and so on), or any other classification scheme appropriate for your application. Another interesting idea would be to maintain a list of the features and make its items react to the mousemove
event so that as users move the mouse over the list, a different feature is selected on the map.Polyline
and Polygon
objects, which can be used to annotate maps with shapes. Later in this book, you will see how to draw lines and polygons on the map interactively using the same objects.18.217.199.122