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
Tiffany Parker 9Tiffany Parker 9 

Any known issues w/ Google Maps Distance Matrix post Winter 16 Release?

Hello, We have had random disruptions and issues with our Google Maps API call this week. Does anyone know of issues with this call and wether or not this could be a Winter 16 release issue. Our org was updated over the weekend and the issues began on Monday. What we have is an object in SFDC built to collect Origin Zip Code and Destination Zip Code and the call goes to GMDM and returns the distance in meters. It has returned too many zeros this week ocasionally returning results with a refresh or clone and working some hours of the day, some hours not.... Help please! 
Tiffany Parker 9Tiffany Parker 9
APEX Detail = 

public class ZipGeoCode{
 
     @future (callout=true)  // future method needed to run callouts from Triggers
      static public void getDistance(id carquoteId){
        // gather account info
        CarQuotes__c a = [SELECT Open_Carrier__c, Origin_Zip__c, Origin_Street__c, Origin_City__c, Origin_State__c, Origin_Country__c, Destination_Zip__c, Destination_Street__c, Destination_City__c, Destination_State__c, Destination_Country__c, NeedDistance__c FROM CarQuotes__c WHERE id =: carquoteId];
        
        if(a.NeedDistance__c == True){  
            // build callout
            String ostreet = ' ';
            String ocity = ' ';
            String ostate = ' ';
            String ozip = ' ';
            String ocountry = ' ';
            String dstreet = ' ';
            String dcity = ' ';
            String dstate = ' ';
            String dzip = ' ';
            String dcountry = ' ';
            if(a.Origin_Street__c != null){ostreet = a.Origin_Street__c;}
            if(a.Origin_City__c != null){ocity = a.Origin_City__c;}
            if(a.Origin_State__c != null){ostate = a.Origin_State__c;}
            if(a.Origin_Country__c != null){ocountry = a.Origin_Country__c;}
            if(a.Origin_Zip__c != null){ozip = a.Origin_Zip__c;}
            if(a.Destination_Street__c != null){dstreet = a.Destination_Street__c;}
            if(a.Destination_City__c != null){dcity = a.Destination_City__c;}
            if(a.Destination_State__c != null){dstate = a.Destination_State__c;}
            if(a.Destination_Country__c != null){dcountry = a.Destination_Country__c;}
            if(a.Destination_Zip__c != null){dzip = a.Destination_Zip__c;}
            String originaddress = EncodingUtil.urlEncode(ostreet + ' ' + ocity + ' ' + ostate + ' ' + ocountry + ' ' + ozip, 'UTF-8');
            String destaddress = EncodingUtil.urlEncode(dstreet + ' ' + dcity + ' ' + dstate + ' ' + dcountry + ' ' + dzip, 'UTF-8');
            Http h = new Http();
            HttpRequest req = new HttpRequest();
            //req.setEndpoint('http://maps.googleapis.com/maps/api/distancematrix/json?origins=' + a.Origin_Zip__c + '&destinations=' + a.Destination_Zip__c + '&mode=driving&units=imperial&sensor=false');
            req.setEndpoint('http://maps.googleapis.com/maps/api/distancematrix/json?origins='+ originaddress +'&destinations=' + destaddress +'&mode=driving&units=imperial&sensor=false');
            //req.setEndpoint(EncodingUtil.urlEncode('http://maps.googleapis.com/maps/api/distancematrix/json?origins=' + a.Origin_Street__c + '+' + a.Origin_City__c + '+' + a.Origin_State__c + '+' + a.Origin_Zip__c + '&destinations=' + a.Destination_Street__c + '+' + a.Destination_City__c + '+' + a.Destination_State__c + '+' + a.Destination_Zip__c + '&mode=driving&units=imperial&sensor=false', 'UTF-8'));
            //req.setEndpoint(GEOCODING_URI_BASE + EncodingUtil.urlEncode(address, 'UTF-8'));
            req.setMethod('GET');
            req.setTimeout(60000);
            integer distance = null;
            string tdistance = null;
            //HttpResponse res = h.send(req);
            // callout
            HttpResponse res = h.send(req);
            System.debug(res.getBody());
            // parse coordinates from response
            JSONParser parser = JSON.createParser(res.getBody());
            while (parser.nextToken() != null) {
                if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
                    (parser.getText() == 'distance')){
                       parser.nextToken(); // object start
                       while (parser.nextToken() != JSONToken.END_OBJECT){
                           String txt = parser.getText();
                           parser.nextToken();
                           if (txt == 'text')
                               tdistance = parser.getText();
                           else if (txt == 'value')
                               distance = parser.getIntegerValue();
                       }
                }
            }
 
            // update coordinates if we get back
            if (distance != null){
                a.Distance_Meters__c = distance;
                //a.tdistance__c = tdistance;
                a.NeedDistance__c = False;
                //a.Description1__c = req.getEndpoint();
                //a.Description2__c = res.getBody();
                //String originaddress = a.Origin_Street__c != null ? a.Origin_Street__c + ' ' : '' +
                //a.Origin_City__c != null ? a.Origin_City__c + ' ' : '' +
                //a.Origin_State__c != null ? a.Origin_State__c + ' ' : '' +
                //a.Origin_Zip__c != null ? a.Origin_Zip__c : '';
                //String originaddress = EncodingUtil.urlEncode(a.Origin_Street__c + ' ' + a.Origin_City__c + ' ' + a.Origin_State__c + ' ' + a.Origin_Zip__c, 'UTF-8');
                a.Description1__c = 'http://maps.googleapis.com/maps/api/distancematrix/json?origins=' + originaddress + '&destinations=' + destaddress + '&mode=driving&units=imperial&sensor=false';
                //a.Description1__c = EncodingUtil.urlEncode(originaddress, 'UTF-8') + '---' + originaddress;
                update a;
            }
        } 
          Else {
              a.Description1__c = 'test';
              a.NeedDistance__c = True;
              update a;
          }
      }
}
Tiffany Parker 9Tiffany Parker 9
Code is back up and running again today as of 1:00 P.M. MST, It seems to function again in the afternoon. 
Tiffany Parker 9Tiffany Parker 9
And back down again by 3 P.M. MST.....
Alex Calder 10Alex Calder 10
Hey, I am trying to do this same thing, I'd love to speak with you about this sometime.  I cannot figure this out.