import java.util.ArrayList; import java.util.List; /** *

* This class represents the data points for a given set of contacts. * Specifically it is used to generate the Javascript code representing an array * of GLatLng objects for use with the Google Maps API. *

* @author Mark Woehrer; edited by Wayne Smith, 4/2009 * @version 1a5 */ public class ContactBook { /** The list of contacts */ List listOfContacts; public ContactBook() { super(); listOfContacts = new ArrayList(); } public void addContact(LatLng contact) { listOfContacts.add(contact); } /** Create a Javascript array by iterating through each contact list. * The string must be surrounded with square brackets to form a valid * Javascript array.
*
* For example:
*
* [ new GLatLng(10.8, -35.5), new GLatLng(25.6, -67.0), new GLatLng(29.2, -91.3) ] *

* @return A String suitable for use with Javascript */ public String toString() { String string = new String(); string += ""; for(LatLng point : listOfContacts) { string += " map.addOverlay(new GMarker(new GLatLng(_point_)));\n"; string = string.replaceAll("_point_", point.toString()); } string += " "; return string; } /** * This is the test method for the class. * @param args */ public static void main(String[] args) { // Create a new ContactBook object ContactBook contacts = new ContactBook(); // Add a few test data points from Andrew 1992 contacts.addContact(new LatLng(10.8, -35.5)); contacts.addContact(new LatLng(25.6, -67.0)); contacts.addContact(new LatLng(29.2, -91.3)); // Test the toString() method System.out.println(contacts); } }