当前页面: 开发资料首页 → Netbeans 专题 → Using the Ajax Map Viewer Component
摘要: This tutorial shows you how to use NetBeans Visual Web Pack to build a JavaServer Faces web application that uses the sample Ajax Map Viewer component. You import the Java BluePrints AJAX components into the IDE, then use Google's free map service API to create a custom map. The map includes marker text and a flag that pinpoints an address entered by the user
The sample Java BluePrints AJAX components are provided to help you learn about Ajax and to show how Ajax fits with the NetBeans Visual Web Pack.
This tutorial works with the following technologies and resources
JavaServer Faces Components/ Java EE Platform |
![]() ![]() | |||
Travel Database | ![]() |
|||
BluePrints AJAX Component Library | ![]() |
* As of the date this tutorial was published, only the Sun Java System Application Server supported Java EE 5.
This tutorial has been tailored for use with the Sun Java Application Server PE 9.0 Update Release 1 and with Tomcat 5.5.17. If you are using a different server, consult the Release Notes and FAQs for known problems and workarounds. For detailed information about the supported servers and Java EE platform, see the Release Notes.
In your web browser, go to http://www.google.com/apis/maps/.
The Google map key signup page opens in your web browser.Click Sign up for a Google Maps API key.
The Google map key license agreement page opens in your web browser.Review and accept the agreement and enter the URL for your web site.
To find the URL for your web site, deploy an application and look at the server name and port number. For example, http://localhost:29080/
.
Click Generate API Key.
Google generates your map key.Create a visual web application project and name it AjaxMapViewer
.
In the Projects window, right-click the AjaxMapViewer > Component Libraries node and choose Add Component Library from the pop-up menu. Select BluePrints AJAX Components and click Add Component Library.
The IDE copies the component library into the project and adds the components and support beans to the Palette.In the Palette, expand the BluePrints AJAX Components node if it is not already expanded, then drag the Map Viewer component and drop it onto Page1.
A map image appears on Page1 and the components mapViewer1
and mapViewer1_center
appear in the Outline window, similar to the following figure.
![]() Figure 1: Map Viewer Component (click to enlarge) |
In the Properties window, find the Map Viewer's key
property and enter the Google map key that you saved earlier, as shown in the following figure.
![]() Figure 2: Map Key Property |
To see what you have so far, click the green arrow in the main toolbar, or choose Run > Run Main Project, to build and run the application.
The AjaxMapViewer project opens in your web browser as shown in the following figure.
![]() Figure 3: Map Viewer Application, Take 1 |
If the map does not display in the web page, check the following:
The latitude
and longitude
properties of the mapViewer1_center
component determine the map location. In this section, you add a page that takes a text address as input and uses the Ajax Support Bean Geocoding Service Object
component to convert the address into
latitude and longitude coordinates. You add latitude and longitude properties to the Request Bean and use these properties to set the
coordinates on the mapViewer1_center
component.
Designing the Map Coordinates Input Page
In the New Page dialog box, type the file name GetCoordinates
, and click Finish.
The IDE opens the GetCoordinates page in the Visual Designer. The following figure shows the page that you will design.
![]() |
Enter an address:
required
property to True.text
property to Get Map
and the id
property to getMap
.Right-click anywhere in the Visual Designer and choose Page Navigation from the pop-up menu.
The Page Navigator window opens in the IDE.Click the GetCoordinates.jsp icon and drag a connector from the getMap
button's connector port to Page1.jsp as shown in the following figure. See the section Navigating Between Two Pages in the tutorial Setting Up Page Navigation for an example.
![]() |
In the Projects window, right-click the AjaxMapViewer > Web Pages > GetCoordinates.jsp node and choose Set As Start Page from the pop-up menu.
A green arrow appears next to the GetCoordinates.jsp node, as shown in the following figure.
![]() |
From the BluePrints AJAX Support Beans section of the Palette, drag the Geocoding Service Object component and drop it anywhere on the GetCoordinates page.
The geoCoder1 component appears in the Outline window, as shown in the following figure.
![]() |
If you are working behind a firewall, configure the proxy settings on the geoCoder1 component because it does not retrieve the proxy settings from the IDE. If you are not working behind a firewall, skip this step.
proxyHost
property to the appropriate proxy for your location.proxyPort
property to the appropriate port for your location.In the Outline window, right-click RequestBean1 and choose Add > Property from the pop-up menu.
The New Property Pattern dialog box opens.longitude
in the Name field and double
(lower-case) in the Type field, and then click OK.latitude
for the Name and double
(lower-case) for the Type, and then click OK.Double-click the Get Map button in the Visual Designer.
The IDE opens the GetCoordinates page in the Java editor at thegetMap_action()
method.Add the following code (shown in bold) to the getMap_action()
method.
Code Sample 1: GetMap Button Code |
public String getMap_action() {
GeoPoint points[] = geoCoder1.geoCode((String)textField1.getText());
if (points.length < 1) {
error("Sorry, cannot find that location; please be more specific.");
return null;
}
getRequestBean1().setLatitude(points[0].getLatitude());
getRequestBean1().setLongitude(points[0].getLongitude());
return "case1";
} |
In the Java Editor for Page1, scroll down to the prerender()
method and add the following code shown in bold.
Code Sample 2: prerender Method |
public void prerender() {
mapViewer1_center.setLatitude(getRequestBean1().getLatitude());
mapViewer1_center.setLongitude(getRequestBean1().getLongitude());
} |
Build and run the application. AjaxMapViewer opens in your web browser. Enter a physical address, such as 15 Network Circle, Menlo Park, CA
, and click the Get Map button. A map showing the location you entered opens in your web browser, similar to the following figure.
![]() |
In the Outline window, select the page1 > mapMarker1 node as shown in the following figure.
![]() |
In the Properties window for mapMarker1, set the markup
property to This is the place!
.
In the Property Bindings dialog box, bind mapViewer1's info property to Page1's mapMarker1
property, as shown in the following figure. Click Apply, then Close.
![]() info Property to mapMarker1 |
Scroll to the prerender()
method and add the following two lines shown in bold.
Code Sample 3: prerender() Method With Map Marker Support |
public void prerender() {
mapViewer1_center.setLatitude(getRequestBean1().getLatitude());
mapViewer1_center.setLongitude(getRequestBean1().getLongitude());
mapMarker1.setLatitude(mapViewer1_center.getLatitude());
mapMarker1.setLongitude(mapViewer1_center.getLongitude());
} |
Build and run the application. When the AjaxMapViewer application opens in your web browser, enter an address such as 15 Network Circle, Menlo Park, CA
, and then click the Get Map button. The map opens in your web browser with a map marker pointing to the location, similar to the following figure.
![]() |
In the Java Editor for Page1, find the public class Page1 extends AbstractPageBean
declaration. Add the following code (shown in bold) to this class.
Code Sample 4: Adding a MapMarker Array to Page1 |
public class Page1 extends AbstractPageBean {
private MapMarker[] markers;
public MapMarker[] getMarkers(){
return markers;
} |
Scroll to the prerender()
method and add the following two lines of code shown in bold.
Code Sample 5: prerender() Method With Map Marker Flag Code |
public void prerender() {
mapViewer1_center.setLatitude(getRequestBean1().getLatitude());
mapViewer1_center.setLongitude(getRequestBean1().getLongitude());
mapMarker1.setLatitude(mapViewer1_center.getLatitude());
mapMarker1.setLongitude(mapViewer1_center.getLongitude());
markers = new MapMarker[1];
markers[0] = mapMarker1;
}
|
In the Property Bindings dialog box, bind mapViewer1's markers
property to Page1's markers
array, as shown in the following figure. Click Apply, then Close.
![]() |
Build and run the application. Enter an address and click the Get Map button. The map opens in your web browser with a map marker and flag pointing to the location, similar to the following figure.
![]() |
Try It. In the section Adding a Map Marker Flag, you added a MapMarker
array to contain the map marker and marker flag. Try using the array to place multiple markers on the map.
Try It. This tutorial shows how to store the MapMarker array at page scope. Try storing the MapMarker array at session scope and implement an "add a marker" page to add markers to the map. Ensure that the extra addresses you provide appear on the map at the scale factor you are using. The default scale factor is 4.
Try It. This tutorial shows how to use one Java BluePrints AJAX map component to create one custom map. Try building an application that creates more than one map. Rather than store the Google map service API key as a property on each map component that you use, create a context initialization parameter named com.sun.j2ee.blueprints.ui.mapviewer.KEY
in your web.xml
file and store the Google map service API key value there.
Try It. For information on the Google Maps JavaScript API, see Google Maps API Version 2 Documentation. For the ability to create custom map controls, such as built-in pan and zoom controls, see the section titled Custom Map Controls.
See Also: