+ Start a Discussion
CSOLANOJRCSOLANOJR 

Mass Geocoding using Google maps API V3

Hello all I am still very new to SFDC so bear with me :smileyhappy: . So i have successfully made a visualforce page that will Geocode and capture the lon/lat values of a single account address(thanks to theses boards :) now i wanted to see is if there is a way to run this visualforce page for all accounts in one batch, I do understand that they have to be spaced out to avoid the over limit error I have seen this done before when i used and edited find nearby (thanks to Mr.Reid) but the way i implemented the geocoding is more so through javascript than through apex unlike find nearby.  Also I want to keep my code for Google maps API V3 i'm trying to avoid using V2. Heres my code and thanks in advance:

 

VF Page:

<apex:page standardController="Account" extensions="Monty" id="pg">




<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> 
<script type="text/javascript"> 

$(document).ready(function() {
  
  var myOptions = {
    zoom: 15,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    mapTypeControl: false
  }
  
  var map;
  var marker;
  
  var geocoder = new google.maps.Geocoder();
  var address = "{!Account.BillingStreet}, " + "{!Account.BillingCity}, " + "{!Account.BillingPostalCode}, " + "{!Account.BillingCountry}";
  
  var infowindow = new google.maps.InfoWindow({
    content: "<b>{!Account.Name}</b><br>{!Account.BillingStreet}<br>{!Account.BillingCity}, {!Account.BillingPostalCode}<br>{!Account.BillingCountry}"
  });

  geocoder.geocode( { address: address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK && results.length) {
    //alert( "latitude : " + results[0].geometry.location.lat() );
    //alert( "longitude : " + results[0].geometry.location.lng() );
     var latt =  results[0].geometry.location.lat();
      var lngg =  results[0].geometry.location.lng();
  document.getElementById('alat').value = latt;
   document.getElementById('alon').value = lngg;

      if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
      
        //create map
        map = new google.maps.Map(document.getElementById("map"), myOptions);
      
        //center map
        map.setCenter(results[0].geometry.location);
        
        //create marker
        marker = new google.maps.Marker({
            position: results[0].geometry.location,
            map: map,
            title: "{!Account.Name}"
        });
        
        //add listeners
        google.maps.event.addListener(marker, 'click', function() {
          infowindow.open(map,marker);
        });
        google.maps.event.addListener(infowindow, 'closeclick', function() {
          map.setCenter(marker.getPosition()); 
        });
        
      }
      
    } else {
      $('#map').css({'height' : '15px'});
      $('#map').html("Oops! {!Account.Name}'s billing address could not be found, please make sure the address is correct.");
      resizeIframe();
    }
  });
  
  function resizeIframe() {
    var me = window.name;
    if (me) {
      var iframes = parent.document.getElementsByName(me);
      if (iframes && iframes.length == 1) {
        height = document.body.offsetHeight;
        iframes[0].style.height = height + "px";
      }
    }
  }

});
  function setHidden()
    {
    var Longitude = document.getElementById('pg:form:hlon');
        var hiddenRep = document.getElementById('pg:form:hlat');
        hiddenRep.value = document.getElementById('alat').value;
        Longitude.value = document.getElementById('alon').value;
    }


</script>

<style>
#map {
  font-family: Arial;
  font-size:12px;
  line-height:normal !important;
  height:150px;
  width:300px;
  background:transparent;
}
</style>


 


<div id="map" style="font-size: 24px"><br/>There is a problem with the address verify that the address is correct</div> 

<apex:form id="form">

   <div>
    <label for="alat"><b>Latitude and Longitude: </b></label>
    <input id="alat"/>
    <input id="alon"/>
   </div>
<apex:actionFunction name="kill" action="{!change}"/>
    <apex:inputHidden id="hlat" value="{!alat}"/>
     <apex:inputHidden id="hlon" value="{!alon}"/>      
    
        
    

</apex:form>
<script>
    function save()
    {
kill();

}
    window.onload = setTimeout(setHidden,750)
    window.onload = setTimeout(save,1500)
    

</script>
</apex:page>

 Controller:

 

public  with sharing class Monty {

    public Monty(ApexPages.StandardController controller) {
account2 = [select id,BillingStreet, BillingCity, BillingPostalCode,BillingCountry,Latitude__c,Longitude__c, Name from Account where id = :ApexPages.currentPage().getParameters().get('id')];
    }

 
private  Account account2;


 public Account getAccount() {
            return account2;
      }

 public String alat{get;set;}
 public String alon{get;set;}
 

 
}
   
 public PageReference change(){

account2.Latitude__c = alat;
account2.Longitude__c = alon;
update account2;
PageReference reference=new PageReference('https://na12.salesforce.com/'+ account2.id);
        reference.setRedirect(true);
        return reference;
}



}

 PS I am sure there is some misc code still there that does belong anymore.

Thomas PanniThomas Panni
Hi all,
sorry for bothering you. I have a question in terms of generating the API key version 3 for maps. I created a google account, created a project and activated the Maps embed API. Is that the correct one? If yes, what kind of key do I have to generate? A browser or a server key? I just wanted to use the delivered component you can find in SFDC, change the key and integrate the map on Accounts. But it stayed empty. Where could be the error? No messages found.
thanks in advance...

best regards Thomas