Blog post

Get your location using google API in Salesforce and assign date time value from JavaScript to Salesforce fields

Posted on Nov 26, 2016
by Piyush Singh
in Other
with 1 comments

Hi All,

Here i am giviing the sample of getting your location in Salesforce through google Map API. This code will give your current location (based on your browser) using latitude and logitude. To implement this process in salesforce, you need to follow given steps:

1. Create remote site setting with this url in salesforce and make Disable security protocal checkbox to true. URL: "https://maps.googleapis.com/maps/api/js?sensor=false" 

2. Then create a new visual force page and use the given source code. It will work in Classic Salesforce & Salesforce 1 App. To you it in Salesforce1 app, you need to call this page using Action not the button button in classic salesforce you can use button.

You can use this code directly in Javascript type button except html tag. 

This page also provides the code to assign current date time in Salesforce date time field from java script.

********************************************************* Here is the code ***************************************************************

<apex:page standardController="Event" standardStylesheets="false" sidebar="false" >
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="/soap/ajax/29.0/connection.js"></script>
<apex:includeScript value="/soap/ajax/28.0/apex.js"/>
<script>
    //Session Id
    sforce.connection.sessionId = "{!$Api.Session_ID}";
    //Standard Event Object to update
    var eventtoupdate = new sforce.SObject("Event"); 
    eventtoupdate.Id="{!Event.Id}"; 

    //INSERT TIME in Salesforce custom field
    var today = new Date(); 
    function fixTime(time){ 
    if(time < 10) {time = "0" + time}; 
    return time; 
    } 

    function fixDate(date){ 
    var Month = fixTime(date.getMonth() + 1); 
    var Day = fixTime(date.getDate()); 
    var UTC = date.toUTCString(); 
    var Time = UTC.substring(UTC.indexOf(":")-2, UTC.indexOf(":")+6); 
    var Minutes = fixTime(date.getMinutes()); 
    var Seconds = fixTime(date.getSeconds()); 
    return date.getFullYear() + "-" + Month + "-" + Day + "T" + Time; 
    } 
    
    //Assign current date time in date time field in event
    eventtoupdate.Check_In_Date_Time__c = fixDate(today); 

   //INSERT LOCATION 
   var geocoder; 

    if (navigator.geolocation) { 
    var location_timeout = setTimeout("geolocFail()", 10000); 

    navigator.geolocation.getCurrentPosition(function(position) { 
    clearTimeout(location_timeout); 

    var lat = position.coords.latitude; 
    var lng = position.coords.longitude; 
     
    
    initialize(); 
    geocodeLatLng(lat,lng); 
    }, function(error) { 
    alert("inside error "); 
    clearTimeout(location_timeout); 
    geolocFail(); 
    }); 
    } else { 
    // Fallback for no geolocation 
    geolocFail(); 
    } 

    function geolocFail(){ 
    alert("Turn on the location service to make the deposit"); 
    self.close();
    } 

    function initialize() { 
     geocoder = new google.maps.Geocoder(); 
    } 
    function geocodeLatLng(lat, lng) { 
        
        geocoder = new google.maps.Geocoder(); 
        var latlng = new google.maps.LatLng(lat, lng); 
        geocoder.geocode({"latLng": latlng}, function(results, status) { 
                
            if (status == google.maps.GeocoderStatus.OK) { 
                if (results[1]) { 
               
                //formatted address 
                var add= results[0].formatted_address 
                alert(add); 

                //Update the Location in event 
                eventtoupdate.Check_In_Location__c = add;
                 
                sforce.connection.update([eventtoupdate]);
                window.opener.location.href="/{!Event.Id}";
                self.close();
                //city data 
                //alert(city.short_name + " " + city.long_name) 

                } else { 
                alert("No results found"); 
                self.close();
                } 
            } else { 
            alert("Geocoder failed due to: " + status); 
            self.close();
            } 
        }); 
    }
</script>
<apex:outputPanel layout="Block"><apex:outputlink value="/{!Event.id}">Return to Event</apex:outputlink></apex:outputPanel>
</apex:page>


1

Leave your comment

    Pradip Shukla
    on Dec 09, 2016
  1. Really helpful and nice article for fetching Geo-Location from google API.

Leave your comment