Newer Version Available
Creating the Custom Controller
To build the mapping application, we first need to create the custom controller referenced by the Visualforce page that displays the map and corresponding list of accounts. The controller retrieves the user's accounts with a rating of 'Hot' and builds a string array of delimited accounts for use in the mapping JavaScript routine on the Visualforce page. It also defines a getter method for the Maps API key, which is required in order to use Google Maps in our page.
The following Apex class is the controller for the Visualforce page that maps the user's hot accounts:
1swfobject.registerObject("clippy.codeblock-0", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public class mapController {
18
19 public String addrStr;
20 public User usr;
21 public String myKey;
22
23 public Account[] getMyAccts() {
24 String usrId = UserInfo.getUserId();
25 Account[] accts = [Select Id, Name, Rating, CustomerPriority__c,
26 OwnerId, BillingStreet, BillingCity, BillingState,
27 BillingPostalCode
28 From Account
29 where Rating = 'Hot'
30 And OwnerId =: usrId ];
31
32 for(Account acct : accts) {
33 addrStr = addrStr + acct.Name + ' : '
34 + acct.CustomerPriority__c + ':'
35 + acct.Id + '~:~'+ acct.BillingStreet + '~:~'
36 + acct.BillingCity + '~:~' + acct.BillingState + '~:~'
37 + acct.BillingPostalCode + '~::~';
38 }
39
40 return accts;
41 }
42
43 public String getmyKey() { // Set up google maps api key
44 myKey = 'http://maps.google.com/maps?file=api&v=2&';
45
46 // In the following line, enter your google maps key
47 // to get an api key, visit the Google Maps API site
48 // http://code.google.com/apis/maps/signup.html
49 myKey = myKey + 'key=<insert_google_maps_api_key_here>';
50
51 return myKey;
52 }
53
54 public String getAddrArStr(){
55 addrStr = '';
56 Account[] theRecs = getMyAccts();
57
58 return addrStr;
59 }
60
61}
62