2

I am currently trying to add some customer markers to a map. I can add a single marker to the map but when I move that same logic inside of an Apex:repeat I no longer have access to the map variable.

My Javascript:

<script type="text/javascript">

    function initialize() {
      var mapProp = {
        center:new google.maps.LatLng(52.634092,-0.148315),
        zoom:10,
        mapTypeId:google.maps.MapTypeId.ROADMAP
      };         
        var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);       
    }
    google.maps.event.addDomListener(window, 'load', initialize);
</script>

My VP page:

   <apex:repeat value="{!Locations}" var="local" id="mapMarkers" >
    <script type="text/javascript">

        var lat = '{!Locations[0].Geolocation__Latitude__s}';
        var lng = '{!Locations[0].Geolocation__Longitude__s}';
        var marker=new google.maps.Marker({
            position: new google.maps.LatLng(lat, lng)   

        });          
        marker.setMap(map); 
    </script>
</apex:repeat>

The !Locations list is in my controller and just pulls the latitude and longitude from my Tree__c object:

   public List<Tree__c> getLocations() {
    List<Tree__c> locations = Database.query('SELECT Geolocation__Latitude__s, Geolocation__Longitude__s FROM Tree__c WHERE Geolocation__Latitude__s !=null AND Geolocation__Longitude__s !=null ');
    return locations;
}

1 Answer 1

3

The way you are using your apex:repeat is not correct.

<script type="text/javascript">
    //rest of your javascript...
    <apex:repeat value="{!Locations}" var="local">
        var lat = '{!local.Geolocation__Latitude__s}';
        var lng = '{!local.Geolocation__Longitude__s}';
        var marker=new google.maps.Marker({
            position: new google.maps.LatLng(lat, lng)   

        });          
        marker.setMap(map); 
    </apex:repeat>
</script>
1
  • quite annoyed I didn't spot that! Thanks for the help :) Commented Jun 1, 2015 at 10:14

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.