i have displayed google map for accounts(lets say 10 accounts)
I have a accounts differentiated by segments. i want to filter out accounts using segment, and based on the result i want to rerender the map again.
somehow, my javascript function does not have the updated result string(string of addresses).
Hence, the same result is displayed again and again even if the accounts are filtered.
How to pass the updated filtered result again to javascript function. Any help will be highly appreciated :(
Visualforce page
sforce.connection.sessionId='{!GETSESSIONID()}';
$(document).ready(function() {
renderMap() ;
});
function renderMap() { var locationString = {!locationsJson}; var selectedSegment = '{!accountSegment}'; var delay = 100; var infowindow = new google.maps.InfoWindow(); var latlng = new google.maps.LatLng(39,-8); var mapOptions = { center: new google.maps.LatLng(54, -2), zoom: 10, mapTypeId: google.maps.MapTypeId.ROADMAP }
var geocoder = new google.maps.Geocoder(); var map = new google.maps.Map(document.getElementById("map"), mapOptions); var bounds = new google.maps.LatLngBounds();
function createMarker(add,lat,lng) { var contentString = add; var marker = new google.maps.Marker({ position: new google.maps.LatLng(lat,lng), map: map, });
google.maps.event.addListener(marker, 'click', function() { infowindow.setContent(contentString); infowindow.open(map,marker); });
bounds.extend(marker.position);
}
for (var i = 0; i < locationString.length; i++) {
//setTimeout('geocodeAddress("'+locationString[nextAddress]+'",theNext)', delay);
alert(locationString[i]);
geocoder.geocode( { 'address': locationString[i]}, addMarker());
}
function addMarker() {
return function(results,status) {
alert(status+'------'+selectedSegment);
if (status == google.maps.GeocoderStatus.OK) {
var icon = "";
switch (selectedSegment) {
case "A":
icon = "red";
break;
case "B":
icon = "blue";
break;
case "C":
icon = "Yellow";
break;
case "D":
icon = "green";
break;
case "N":
icon = "orange";
break;
default :
icon = "pink";
break;
}
icon = "https://maps.google.com/mapfiles/ms/icons/" + icon + ".png";
var marker = new google.maps.Marker({
map: map,
title: 'My Map',
position: results[0].geometry.location,
icon: new google.maps.MarkerImage(icon)
});
} // if end
} // return end
} // fucntion end
function geocodeAddress(address, next) {
geocoder.geocode({address:address}, function (results,status)
{
if (status == google.maps.GeocoderStatus.OK) {
alert(status);
var p = results[0].geometry.location;
var lat=p.lat();
var lng=p.lng();
createMarker(address,lat,lng);
}
else {
if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
nextAddress--;
delay++;
} else {
}
}
next();
}
);
}
}
#map { font-family: Arial; font-size:12px; line-height:normal !important; height:470px; width: 1500px; background:transparent; }
#map bld{
line-height:1.35;
overflow:hidden;
white-space:nowrap;
}
<apex:pageBlock id="pageBlockID">
<apex:pageMessages >
</apex:pageMessages>
<apex:pageBlockSection title="Account Details" columns="1" collapsible="false" id="pageTableID">
<apex:pageBlocktable value="{!lstCAR}" var="car" id="accountTable" >
<apex:column value="{!car.Account__r.Name}" />
<apex:column value="{!car.Account__r.SFA_Account_Street__c}" />
<apex:column value="{!car.Account__r.SFA_Account_ZIP_Code__c}" />
<apex:column value="{!car.Account__r.SFA_Account_City__c}" />
<apex:column value="{!car.Date__c}" />
<apex:column value="{!car.Account__r.SFA_Account_Segment__c}" />
</apex:pageBlocktable>
</apex:pageBlockSection>
<apex:outputLabel value="Account Segment:" for="chooseSegment"/>
<apex:selectList id="chooseSegment" value="{!accountSegment}" size="1">
<apex:selectOption itemValue="All" itemLabel="All" />
<apex:selectOption itemValue="A" itemLabel="A" />
<apex:selectOption itemValue="B" itemLabel="B" />
<apex:selectOption itemValue="C" itemLabel="C" />
<apex:selectOption itemValue="D" itemLabel="D" />
<apex:selectOption itemValue="N" itemLabel="N" />
</apex:pageBlock>
Apex Class
public class MapAccountExtension {
public String[] locations {get; set;}
public List<SFA_Customer_Activity_Report__c> lstCAR {get; set;}
public Boolean accountSegmentFilterFlag {get; set;}
public String locationsJson{get;set;}
public String accountSegment{get; set;}
public MapAccountExtension() {
accountSegmentFilterFlag = false;
}
public void init() {
filterAccount();
}
public void filterAccount() {
System.debug('accSegment-->'+accountSegment);
locations = new String[] {};
String[] locations = new String[] {};
Id salesRepID = UserInfo.getUserId();
Id deRecordTypeID = Schema.SObjectType.SFA_Customer_Activity_Report__c.getRecordTypeInfosByName().get('SFA DE Customer Activity Reports').getRecordTypeId();
Date today = System.today();
String whereClause;
String query = 'select Name, Sales_Rep__r.name, Account__c, status__c, Account__r.Id, Account__r.Name, Account__r.SFA_Account_Segment__c, Account__r.SFA_Account_Street__c,'+
'Account__r.SFA_Account_City__c, Account__r.SFA_BrickId__c, Account__r.SFA_Account_ZIP_Code__c, Date__c, Contact__c from SFA_Customer_Activity_Report__c'+
' WHERE RecordType.Name = \'SFA DE Customer Activity Reports\' and Status__c = \'Planned\' and Type__c= \'Visit\' and Sales_Rep__c =: salesRepID and Date__c =: today';
//if accounts are filtered using account segment
if(accountSegment!=null && accountSegment!= 'All') {
whereClause = ' and Account__r.SFA_Account_Segment__c =: accountSegment';
accountSegmentFilterFlag = true;
}
if (whereClause != null) {
query = query + whereClause;
}
lstCAR = Database.query(query);
for (SFA_Customer_Activity_Report__c car : lstCAR ) {
locations.add(car.Account__r.Name
+ ' , ' + car.Account__r.SFA_Account_Street__c
+ ' , ' + car.Account__r.SFA_Account_City__c
+ ' , ' + car.Account__r.SFA_Account_ZIP_Code__c
);
}
System.debug('accountSegmentFilterFlag--->'+accountSegmentFilterFlag);
if(accountSegmentFilterFlag) {
Set<ID> brickId = new Set<ID>();
for (SFA_Customer_Activity_Report__c car : lstCAR) {
brickId.add(car.Account__r.SFA_BrickId__c);
}
List<Account> accsOfBricks = [select id, SFA_Account_City__c, Name, SFA_Account_Segment__c,
SFA_BrickId__c, SFA_Account_ZIP_Code__c, SFA_Account_Street__c
from account where SFA_BrickId__c IN : brickId
and SFA_Account_Inactive__c = False
and RecordType.Name IN ('SFA DE Accounts')
and SFA_Account_Segment__c =: accountSegment];
for(Account acc : accsOfBricks ) {
locations.add(acc.Name
+ ' , ' + acc.SFA_Account_Street__c == null ? '' : acc.SFA_Account_Street__c
+ ' , ' + acc.SFA_Account_City__c == null ? '' : acc.SFA_Account_City__c
+ ' , ' + acc.SFA_Account_ZIP_Code__c == null ? '' : acc.SFA_Account_ZIP_Code__c
);
}
}
locationsJson = JSON.serialize(locations);
accountSegment = JSON.serialize(accountSegment );
system.debug('locationsJson--->'+locationsJson);
}
}
Here, var locationString = {!locationsJson}; locationsJson is string of addresses passed from apex controller, which is not getting updated in javascript function.
Please help.