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
Ketan ErandeKetan Erande 

I am working on nearby Projects code. The code working well on PC, but in salesforce one app it is taking too much of time to open the page.

Hi all,
I am working on nearby Projects  code. The code working well on PC, but in salesforce one app it is taking too much of time to open the page. Can you guide me to reduce the time or reasons why this could happen?

APEX Code:
global with sharing class FindNearby {
    public FindNearby(ApexPages.StandardSetController controller) { }
    @RemoteAction
    // Find Accounts nearest a geolocation
    global static List<MHC2__Project__c> getNearby(String lat, String lon) {
 
        // If geolocation isn't set, use Eindhoven (or any other city)
        // Put a default location latitue and longitude here, this could be where you are located the most
        // and will only be used as a backup if the browser can not get your location details
        if(lat == null || lon == null || lat.equals('') || lon.equals('')) {
            lat = '51.096214';
            lon = '3.683153';
        }
 
        // SOQL query to get the nearest accounts
        //you can change km (kilometers) into mi (miles)
        // < 20 means within a radius of 20 km or mi (you can change that)
        //limit 25 shows 25 records (you can adapt that too if you want)
        String queryString =
            'SELECT Id, Name, Project_Location__Latitude__s, Project_Location__Longitude__s, ' +
                'MHC2__MHC_City__c, MHC2__MHC_City_State_Zip__c, MHC2__MHC_Country__c ' +
            'FROM MHC2__Project__c ' +
            'WHERE DISTANCE(Project_Location__c, GEOLOCATION('+lat+','+lon+'), \'km\') < 200 ' +
            'ORDER BY DISTANCE(Project_Location__c, GEOLOCATION('+lat+','+lon+'), \'km\') ' +
            'LIMIT 25';
 
        // Run and return the query results
        return(database.Query(queryString));
    }
}
 
 
Visualforce Page code:
<apex:page sidebar="false" showheader="false" standardController="MHC2__Project__c" recordSetVar="accts" extensions="FindNearby">
   
    <!-- Include in Google's Maps API via JavaScript static resource -->
    <apex:includeScript value="{!$Resource.googleMapsAPI}" />
   
    <!-- Set this API key to fix JavaScript errors in production -->
    <!--http://salesforcesolutions.blogspot.com/2013/01/integration-of-salesforcecom-and-google.html-->
    <script type="text/javascript"
        src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCROH4OR9fzDhmprWPL1wGWfPT4uGUeMWg&sensor=false">
        </script>
       
    <!-- Setup the map to take up the whole window -->
    <style>
        html, body { height: 100%; }
        .page-map, .ui-content, #map-canvas { width: 100%; height:100%; padding: 0; }
        #map-canvas { height: min-height: 100%; }
    </style>
   
    <script>
        function initialize() {
            var lat, lon;
              directionsDisplay = new google.maps.DirectionsRenderer();
             // If we can, get the position of the user via device geolocation
             if (navigator.geolocation) {
                 navigator.geolocation.getCurrentPosition(function(position){
                     lat = position.coords.latitude;
                     lon = position.coords.longitude;                   
                     //lat=29.749801;
                     //lon=-95.352175;
                    
                     // Use Visualforce JavaScript Remoting to query for nearby accts     
                     Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.FindNearby.getNearby}', lat, lon,
                         function(result, event){
                             if (event.status) {
                                 console.log(result);
                                 createMap(lat, lon, result);          
                             } else if (event.type === 'exception') {
                                 //exception case code         
                             } else {
                                           
                             }
                          },
                          {escape: true}
                      );
                  });
              } else {
                  // Set default values for map if the device doesn't have geolocation capabilities
                    /** Eindhoven **/
                    lat = 51.096214;
                    lon = 3.683153;
                   
                    var result = [];
                    createMap(lat, lon, result);
              }
         
         }
   
         function createMap(lat, lon, accts){
            // Get the map div, and center the map at the proper geolocation
            var currentPosition = new google.maps.LatLng(lat,lon);
            var mapDiv = document.getElementById('map-canvas');
            var map = new google.maps.Map(mapDiv, {
                center: currentPosition,
                zoom: 13,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            });
           
            // Set a marker for the current location
            var positionMarker = new google.maps.Marker({
                map: map,
                position: currentPosition,
                icon: 'http://maps.google.com/mapfiles/ms/micons/green-dot.png'
            });
           
                       
            // Keep track of the map boundary that holds all markers
            var mapBoundary = new google.maps.LatLngBounds();
            mapBoundary.extend(currentPosition);
           
            // Set markers on the map from the @RemoteAction results
            var acct;
            for(var i=0; i<accts.length;i++){
                acct = accts[i];
                console.log(accts[i]);
                setupMarker();
            }
           
            // Resize map to neatly fit all of the markers
            map.fitBounds(mapBoundary);
 
           function setupMarker(){
                var acctNavUrl;
               
                // Determine if we are in Salesforce1 and set navigation link appropriately
                try{
                    if(sforce.one){
                        acctNavUrl =
                            'javascript:sforce.one.navigateToSObject(\'' + acct.Id + '\')';
                    }
                } catch(err) {
                    console.log(err);
                    acctNavUrl = '\\' + acct.Id;
                }
               
                var acctDetails =
                    '<a href="' + acctNavUrl + '">' +
                    acct.Name + '</a><br/>' +
                    acct.MHC2__MHC_City__c + '<br/>' +
                    acct.MHC2__MHC_City_State_Zip__c + '<br/>' +
                    acct.MHC2__MHC_Country__c;
              
               // Create the callout that will pop up on the marker    
               var infowindow = new google.maps.InfoWindow({
                   content: acctDetails
               });
              
               // Place the marker on the map  
               var marker = new google.maps.Marker({
                   map: map,
                   position: new google.maps.LatLng(
                                   acct.Project_Location__Latitude__s,
                                   acct.Project_Location__Longitude__s)
               });
               mapBoundary.extend(marker.getPosition());
              
               // Add the action to open up the panel when it's marker is clicked     
               google.maps.event.addListener(marker, 'click', function(){
                   infowindow.open(map, marker);
               });
           }
        }
       
        // Fire the initialize function when the window loads
        google.maps.event.addDomListener(window, 'load', initialize);
       
    </script>
   
   
    <body style="font-family: Arial; border: 0 none;">
      
   <!--  All content is rendered by the Google Maps code -->
    <!--  This minimal HTML justs provide a target for GMaps to write to -->
        <div id="map-canvas"></div>
    </body>
</apex:page>
 
 
Please suggest changes, that will help me to reduce time to open the page.
Let me know if any questions.
Thanks.