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
sivaextsivaext 

Over query limit in google maps return

Hi, 

We have created VF page to show google maps on for different routes. It is working fine yesterday but now we are facing "Over query limit" and "Zero_results". 

please help me

Thanks Advance.
 
Gaurav NirwalGaurav Nirwal
Below is the class using which I make callouts

public Class LocationCallOuts {
   
    /**
     *  DESCRIPTION   :    This method will create Address to be passed to get Latitude and Longitude of Address provided.
     *                     This method will also check that street name is not null, If street name is null then request is not made
     */
    public static void checkAddress(List<Risk_Location__c> riskLocationList) {
        List<Risk_Location__c> locationList = new List<Risk_Location__c>();
        List<Risk_Location__c> addressNotFound = new List<Risk_Location__c>();
        String streetValue;
        LocationCallOuts riskLocCall = new LocationCallOuts();
        for(Risk_Location__c riskLoc : riskLocationList) {
           
            streetValue = riskLocCall.GetStreetName(riskLoc);
            if(streetValue != null && streetValue != '') {
                locationList.add(riskLoc);
            } else {
                riskLoc.Address_Not_Found__c = true;
                addressNotFound.add(riskLoc);
            }
        }
        if( addressNotFound.size() > 0) {
           
            update addressNotFound;
        }
        if( locationList.size() >0 ) {
            riskLocCall.GetLocation(locationList);
        }
    }
   
    /**
     *  DESCRIPTION :       This method will be called to return Street Number.
     */
    @TestVisible private String GetStreetNumber (Risk_Location__c locStreetNumber) {
        String streetNumber = '';
        if(locStreetNumber.Street_Number__c != null && locStreetNumber.Street_Number__c != '') {
            streetNumber += locStreetNumber.Street_Number__c + ',';
        }
        return streetNumber;
    }
   
    /**
     *  DESCRIPTION :       This method will be called to return Street Name.
     */
    @TestVisible private String GetStreetName (Risk_Location__c locStreetName) {
        String streetName ='';
        if(locStreetName.Location__c != null && locStreetName.Location__c != '') {
            streetName += locStreetName.Location__c + ',';
        }
        return streetName;
    }
   
    /**
     *  DESCRIPTION :       This method will be called to return Suburb.
     */
    @TestVisible private String GetSuburb (Risk_Location__c locSuburb) {
        String suburbName = '';
        if(locSuburb.Suburb__c != null && locSuburb.Suburb__c != '') {
            suburbName += locSuburb.Suburb__c + ',';
        }
        return suburbName;
    }
   
    /**
     *  DESCRIPTION :       This method will be called to return State.
     */
    @TestVisible private String GetState (Risk_Location__c locState) {
        String stateName = '';
        if(locState.State__c != null && locState.State__c != '') {
            stateName += locState.State__c + ',';
        }
        return stateName;
    }
   
    /**
     *  DESCRIPTION :       This method will be called to return Country.
     */
    @TestVisible private String GetCountry (Risk_Location__c locCountry) {
        String countryName = '';
        if(locCountry.Country__c != null && locCountry.Country__c != '') {
            countryName += locCountry.Country__c + '}';
        }
        return countryName;
    }
   
    /**
     *  DESCRIPTION   :    This method is called to make request to Google with the address and get the Latitude and Longitude and update Risk Location records
     */
     @TestVisible private void GetLocation(List<Risk_Location__c> riskLocationList) {
         double lat = null;
         double lng = null;
         String address = '';
         String formattedAddress = '';
         List<String> splitAddress;
         Risk_Location__c riskLocation = new Risk_Location__c();
        
         for( Risk_Location__c riskLoc : riskLocationList ) {
             splitAddress = new List<String>();
            
             address += '{';
             address += GetStreetNumber(riskLoc);
             address += GetStreetName(riskLoc);
             address += GetSuburb(riskLoc);
             address += GetState(riskLoc);
             address += GetCountry(riskLoc);
            
             Http h = new Http();
             HttpRequest req = new HttpRequest();
             req.setEndpoint('http://maps.googleapis.com/maps/api/geocode/json?address='+address+'&sensor=true');
             req.setMethod('GET');
            
             try {
                 HttpResponse res = h.send(req);
                
                 JSONParser parser = JSON.createParser(res.getBody());
                 system.debug('********* Resquest Body ********' + res.getBody());
                 Boolean addressFlag = false;
                 Boolean latlngFlag = false;
                 while(parser.nextToken() != null ) {
                    
                     if((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'formatted_address') && addressFlag == false ) {
                        parser.nextToken();
                        formattedAddress = parser.getText();
                        addressFlag = true;
                     }
                     if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'location') && latlngFlag == false ) {
                           parser.nextToken();
                           while (parser.nextToken() != JSONToken.END_OBJECT) {
                               String txt = parser.getText();
                               parser.nextToken();
                               if (txt == 'lat')
                                   lat = parser.getDoubleValue();
                               else if (txt == 'lng')
                                   lng = parser.getDoubleValue();
                               if(lat != null && lng != null)
                                    latlngFlag = true;
                           } // End of Inner While Loop
                    }
                 }// End of while Loop
                
                 splitAddress = address.split(',');
                 // If Lat and Lng is not Null and Formatted address is same as Provided, populate Google Address, Address_Not_Found__c and Approximate_Address__c on Risk Location
                 if( lat != null && lng != null ) {
                     // If Formatted address from Google is same as Address on Risk Location, populate Lat, Lng, Google Address and Uncheck Approximate_Address__c and Address_Not_Found__c
                     if((formattedAddress.toUpperCase()).contains((splitAddress[1].toUpperCase())) && (formattedAddress.toUpperCase()).contains((splitAddress[2].toUpperCase())) &&
                        ((formattedAddress.toUpperCase()).contains(('AU')) || (formattedAddress.toUpperCase()).contains(('AUS')) || (formattedAddress.toUpperCase()).contains(('AUSTRALIA')))) {
                        riskLoc.Geolocation__Latitude__s = lat;
                        riskLoc.Geolocation__Longitude__s = lng;
                        riskLoc.Google_Address__c = formattedAddress;
                        riskLoc.Approximate_Address__c = false;
                        riskLoc.Address_Not_Found__c = false;
                     }
                     // If Formatted Address is not same as Address on Risk Location then Polpulate Lat,Lng,Google Address and Check Approximate_Address true and Address_Not_Found = false
                     else {
                        riskLoc.Geolocation__Latitude__s = lat;
                        riskLoc.Geolocation__Longitude__s = lng;
                        riskLoc.Approximate_Address__c = true;
                        riskLoc.Address_Not_Found__c = false;
                        riskLoc.Google_Address__c = formattedAddress;
                     }
                 }
                 // If Lat Lng is Null then Clear Google Address, Lat, Lng and Check Address_Not_Found = true and Approximate_Address = false
                 else {
                    riskLoc.Geolocation__Latitude__s = lat;
                    riskLoc.Geolocation__Longitude__s = lng;
                    riskLoc.Google_Address__c = '';
                    riskLoc.Approximate_Address__c = false;
                    riskLoc.Address_Not_Found__c = true;
                 }
             } catch( Exception e) {
                 system.debug('== Exception Occured ====' + e.getMessage());
             }
             address = '';
         } // End of For Loop
       
         update riskLocationList;
     } // End of Method
    
}
U JayU Jay
Hi sivaext,
               I hope you resolved the issue? I have same exception 'OVER_QUERY_LIMIT' in the google map in our professional edition. CAn you get me the resolution ? Thanks in advance.