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: "" 

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=""></script>
<script type="text/javascript" src="/soap/ajax/29.0/connection.js"></script>
<apex:includeScript value="/soap/ajax/28.0/apex.js"/>
    //Session Id
    sforce.connection.sessionId = "{!$Api.Session_ID}";
    //Standard Event Object to update
    var eventtoupdate = new sforce.SObject("Event"); 

    //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); 

   var geocoder; 

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

    navigator.geolocation.getCurrentPosition(function(position) { 

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

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

    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 

                //Update the Location in event 
                eventtoupdate.Check_In_Location__c = add;
                //city data 
                //alert(city.short_name + " " + city.long_name) 

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


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