function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
abhishek_pandey1989@yahoo.comabhishek_pandey1989@yahoo.com 

Havin problem with javascript for repeating the list values in it

The error that i am getting is 

 

 

Error: Unknown property 'AccountStandardController.i'

 

 

For the code  posted below

 

 

 

function codeAddress()
{

for(i=0;i<"{quan}";i++)       (quan is integer with get set value of 2 )
{

var address3="{!conaddlst[i]}";       (Error occurs on this line , "conaddlst" is the string type list , it works fine with standard                object value like"{!account.billingcity}" and gives the desired result)  

 

geocoder.geocode( { 'address': address3}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK)
{
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,

 

 

 

 

 

Thanks for help in advance

Best Answer chosen by Admin (Salesforce Developers) 
Avidev9Avidev9
Try doing jsConlist.push("{!JSENCODE(con)}") instead of jsConlist.push("{!con}");

All Answers

Avidev9Avidev9

Well you cant mix apex variable(conaddlst) with js variable(i).

What you can do is either switch to js remoting or convert your list into  js array and storing them in a global variable

by doing something like this

<script>
  var jsConlist = [];     
    <apex:repeat value="{!conaddlst}" var="con">
      jsConlist.push("{!con}");
    </apex:repeat>
</script>

 

abhishek_pandey1989@yahoo.comabhishek_pandey1989@yahoo.com

Made those changes as described above but poblem remains the same still having the error on same point putting the  code again the changes i made. This time i am putting the whole code to elaborate the exact picture .

Thanks for the help 

Overall suggestions on code are welcome too In which i am trying to put multilple markers by iterating the list .Did it in italics 

I mean the changes .

 

 

<script>
var map;
var geocoder;
var i;
function initialize() {
geocoder = new google.maps.Geocoder();

map = new google.maps.Map(document.getElementById("map-canvas"), {
center: new google.maps.LatLng(37.4419, -122.1419),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
google.maps.event.addListenerOnce(map, 'tilesloaded', codeAddress);
}


var jsConlist = [];
<apex:repeat value="{!conaddlst}" var="con">
jsConlist.push("{!con}");
</apex:repeat>
function codeAddress()
{

for(i=0;i<{!quan};i++)
{
var address3="{!con[i]}";

 


geocoder.geocode( { 'address': address3}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK)
{
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location,animation:google.maps.Animation.BOUNCE
});
var lat = results[0].geometry.location.lat();
var lng = results[0].geometry.location.lng();
}
else
{
alert("Geocode was not successful for the following reason: " + status);
}

});
}
}


</script>

Avidev9Avidev9
<script>
    var map;
    var geocoder;
    var i;

    function initialize() {
        geocoder = new google.maps.Geocoder();

        map = new google.maps.Map(document.getElementById("map-canvas"), {
                center: new google.maps.LatLng(37.4419, -122.1419),
                zoom: 13,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            });
        google.maps.event.addListenerOnce(map, 'tilesloaded', codeAddress);
    }

    var jsConlist = []; 
< apex: repeat value = "{!conaddlst}" var = "con" > 
  jsConlist.push("{!con}"); 
< /apex:repeat>
function codeAddress() 
{

for(i=0;i<{!quan};i++)
{
var address3=jsConlist[i];
 

geocoder.geocode( { 'address': address3}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK)
{
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location,animation:google.maps.Animation.BOUNCE
});
var lat = results[0].geometry.location.lat();
var lng = results[0].geometry.location.lng();
}
else
{
alert("Geocode was not successful for the following reason: " + status);
}

});
}
}
</script>

 Catch was : You cant mix JS variable with Apex/Visualforce Variable

abhishek_pandey1989@yahoo.comabhishek_pandey1989@yahoo.com

Hey Ravin ,thanks for quick replies and correcting my pity mistakes . But fproblem is compiler is not even reading the apex:repeat as i placed several alerts to catch the compilation step by step ;

 

SO I m putting the whole code of my vf . This vf 's job is to print multiple markers on vf page using google geocoding service .

 

None of the alert is responding .Please have look if u can suggest on the whole concept.

 

Thank U .

<apex:page standardcontroller="account" extensions="googlemapsseveralpostion" sidebar="false" showHeader="false">
<html>
    <style>
      html,  #map-canvas 
      {
        margin: 0;
        padding: 0;
        width: 100%;
        height: 90%;
      }
    </style>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDLomrU9TMlveuQZ3FHwhEAcnOLaybI2xY&sensor=false"></script>


<script>
    var map;
    var geocoder;
    var i;
    alert("Test1"); 
    var jsConlist = []; 
    alert("Test1"); 
    

    function initialize() 
    {
        geocoder = new google.maps.Geocoder();

        map = new google.maps.Map(document.getElementById("map-canvas"), {
                center: new google.maps.LatLng(37.4419, -122.1419),
                zoom: 13,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            });
        google.maps.event.addListenerOnce(map, 'tilesloaded', codeAddress);
    }
   
   function codeAddress() 
   {
     alert("test1");
     <apex:repeat value="{!conaddlst}" var="con" > 
         alert("Test"); 
         jsConlist.push("{!con}");
         alert(jsConlist); 
     </apex:repeat>
    
    for(i=0;i<"{!quan}";i++)
    {   
        var address3=jsConlist[i];
        alert(address3);
        geocoder.geocode( { 'address': address3}, function(results, status) {   
        if (status == google.maps.GeocoderStatus.OK)
        {
            map.setCenter(results[0].geometry.location);
            var marker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location,animation:google.maps.Animation.BOUNCE
        });
        var lat = results[0].geometry.location.lat();
        var lng = results[0].geometry.location.lng();
        }
        
        else        
        {
            alert("Geocode was not successful for the following reason: " + status);
        }

        });
    }
   }
</script>

    <body onload="initialize()">
        <div id="map-canvas" style="width: 1000px; height: 300px;"></div>
        <div></div> 
    </body>
    
</html>
</apex:page>

 -------------------------------------Controller--------------------------------------

public class googlemapsseveralpostion
{
 public contact con{get;set;}
 public list<string> conaddlst{get;set;}
 public Integer quan{get;set;}
 private Integer i;
    public googlemapsseveralpostion(ApexPages.StandardController controller) 
    {
      Integer quan;
      list<contact> con=new list<contact>();  
      conaddlst=new list<string>();
      con = [SELECT Id,MailingCity, MailingCountry, MailingState, MailingPostalCode, MailingStreet FROM contact WHERE AccountId = :ApexPages.currentPage().getParameters().get('id')];
      quan=con.size();
      if(con.size()>0)
      {
       system.debug('+++consize+++'+con.size());
        for(i=0;i<con.size();i++)
          {
            system.debug('+++MailingCity+++'+con[i].MailingCity);
            system.debug('+++MailingCountry+++'+con[i].MailingCountry);
            system.debug('+++MailingCity+++'+con[i].MailingCity);                                                                 
            conaddlst.add(con[i].MailingStreet);
          }
          
      }
      system.debug('++conaddlst++++++'+conaddlst[0]);
   }

}

 

 

Avidev9Avidev9
Steps to debug
1. Check the debug logs if the list conaddlst is populated
2. Check the browser console for any Js error
abhishek_pandey1989@yahoo.comabhishek_pandey1989@yahoo.com

jsConlist.push("620SW5thAvenueSuite400    (line from inspect element ,one of the error occurs on this line )

 

Uncaught SyntaxError: Unexpected token ILLEGAL(for above )

 

This problem  i have faced before with other functions too as i dont know why but javascript functions just dont work under google api once called ......

 

I need to look for an atenative to get values in the array u mentioned for repeat ..

 

Thank you

Avidev9Avidev9
Try doing jsConlist.push("{!JSENCODE(con)}") instead of jsConlist.push("{!con}");
This was selected as the best answer
abhishek_pandey1989@yahoo.comabhishek_pandey1989@yahoo.com

Thanks for ur precise help i have one more thing remaining and i m done

 

1, If u can put any url for help on JSENCODE thing where i can read  for more about it.

 

2. I am having a variable   "{!quan}" with Interger type in salesforce but its not getting any value on vf page  i checked in controller through debug its getting assigned value,but not emerging on the page "alert" as shown in above code .

 

Thank you

abhishek_pandey1989@yahoo.comabhishek_pandey1989@yahoo.com

Error: Incorrect argument type for function 'JSENCODE()'.

 

 alert("{!JSENCODE(quan)}"); (above error occurs)