Recipes by Category

App Distribution (2) Bundle logic, interface and services for distribution. App Logic (37) The Apex programming language, workflow and formulas for logic. Collaboration (5) The Salesforce Chatter collaboration platform. Database (29) Data persistence, reporting and analytics. Integration (33) Web Service APIs and toolkits for integration. Security (9) Platform, application and data security. Tools (4) tooling User Interface (36) Visualforce MVC and metadata-drive user interfaces. Web Sites (12) Public web sites and apps with optional user registration and login.
Beta Feedback
Cookbook Home » Retrieving a User's Location from a GPS-enabled Phone

Retrieving a User's Location from a GPS-enabled Phone

Post by Developer Force  (2010-07-16)

Status: Certified
Level: novice


You want to capture the location where your mobile users enter data by retrieving the GPS coordinates from a BlackBerry smartphone or iPhone when users save a record.


Write a Visualforce page that sales representatives can use when logging sales visits. The Visualforce page contains JavaScript that captures the device's longitude and latitude when the record is saved. After writing the Visualforce page, we'll create a tab for the page and add the tab to the mobile application.

Before starting this recipe, make sure you complete the following prerequisites:
  • Mobile Licenses:All existing Developer Edition organizations have mobile licenses. Visit Developer Force and click Getting Started to sign up for a new Developer Edition organization.
  • Mobile Devices: Verify that your BlackBerry smartphone or iPhone can use Salesforce Mobile. See “Supported Devices for Mobile” in the online help.
  • GPS Receiver: Verify that your BlackBerry smartphone has an internal GPS receiver or an external Bluetooth GPS receiver, and that the receiver is enabled. To find out if your BlackBerry smartphone's GPS receiver is on, select Options | Advanced | GPS or Options | Advanced | Location Based Services. If your device does not meet these requirements, download and install the BlackBerry simulator package.

    GPS is unavailable in first-generation iPhones. The earliest iPhone model with GPS support is the iPhone 3G.

First, create a custom object named Sales Visit. This object allows sales representatives to enter information in after making a sales visit.

  1. Click Setup | Create | Objects | New Custom Object.
  2. Enter the following information:
    • Label: Sales Visit
    • Plural Label: Sales Visits
    • Description: Include a brief description so other developers know what this object does.
  3. Accept the remaining defaults and click Save.
  4. In the Custom Fields & Relationships related list, click New and define a field with the following attributes:
    • Data Type: Text Area
    • Field Label: Description
    • Field Name: Description
    • Field-Level Security: Visible for all profiles
    • Add Related List: Select for all page layouts
  5. In the Custom Fields & Relationships related list, click New and define a second field with the following attributes:
    • Data Type: Number
    • Field Label: Longitude
    • Length: 3
    • Decimal Places: 10
    • Field Name: Longitude
    • Field-Level Security: Visible for all profiles
    • Add Related List: Select for all page layouts
  6. In the Custom Fields & Relationships related list, click New and define a third field with the following attributes:
    • Data Type: Number
    • Field Label: Latitude
    • Length: 3
    • Decimal Places: 10
    • Field Name: Latitude
    • Field-Level Security: Visible for all profiles
    • Add Related List: Select for all page layouts
Create an Apex class that mimics the Sales Visit object but changes the save behavior:
  1. Click Setup | Develop | Apex Classes, then click New.
  2. Enter the following code:
    public class visitController { 
        public Sales_Visit__c visit {get;set;}
        public visitController() {
            visit = new Sales_Visit__c();
        public PageReference save() {
            insert visit;
            visit = new Sales_Visit__c();
            return null;
    Normally after saving a record, the record's detail page displays, along with the tabs and sidebar. The Apex class suppresses all these screen elements—which would overload the mobile device's small screen—by displaying a blank Sales Visit record instead.Click Save. Create a Visualforce page that sends the user's GPS coordinates when the sales visit record is saved. Click Setup | Develop | Pages | New. In the Label field, enter Sales Visit Form. In the Name field, enter Sales_Visit_Form. Replace the generic markup with the following Visualforce component:
    <apex:page controller="visitController" showHeader="false" 
        sidebar="false" setup="true" standardStylesheets="false"
        <meta name="viewport" content="width = device-width"/>
        <script type="text/javascript"
        function updateLocation(lat,lon) {  
        function getLocation() {
            //work around required for BB
            if (window.blackberry)
                setInterval("getLocation()", 10000);
                return false;
    <apex:form id="form">
        <apex:pageBlock id="block">
        Sales Visit Name: <br />
        <apex:inputField value="{!}" /><br />
        Sales Visit Description: <br />
        <apex:inputField value="{!visit.Description__c}" /><br />
        Longitude: <br />
        <apex:inputField value="{!visit.Longitude__c}" 
            id="longitude" /><br />
        Latitude: <br />
        <apex:inputField value="{!visit.Latitude__c}" 
            id="latitude" /><br />
        <button type="button" value="GPS" 
            onclick="getLocation();"> Get location </button>
        <apex:commandButton action="{!save}" value="Save!" />
    Click Save. Create a tab for the Visualforce page. Click Setup | Create | Tabs | New. Select Sales_Visit_Form from the Visualforce Page drop-down. In the Tab Label field, enter Sales Visit. Select a tab style. Select the Mobile Ready checkbox. Click Next. Click Next again. Click Save. Create a mobile configuration and mobilize the new Visualforce page. <br/> Click Setup | Mobile Configurations | New Mobile Configuration. In the Name field, type Sales Representative. Select the Active checkbox. Select the Mobilize Recent Items checkbox. Select your name in the Available Members list box, and then click Add. Click Save. In the Data Sets related list, click Edit. Click Add.... Select Account and click OK. Click the Data Sets node in the tree and click Add.... Select Opportunity and click OK. Click the Data Sets node in the tree and click Add.... Select Lead and click OK. Click Done. In the Mobile Tabs related list, click Customize Tabs. Select the Account, Opportunity, Lead, and Sales Visit tabs and click Add. If the Sales Visit tab isn't in the Available Tabs list, you might not have selected the Mobile Ready checkbox when creating the Visualforce tab. Click Save. <br/>

    Test the Visualforce page in the mobile application.

    Install the mobile application on your device. For installation instructions, see “Installing the Mobile Application” in the online help. If you're using a BlackBerry simulator, the mobile application is already installed on the simulator device; just activate your Developer Edition account after launching Salesforce Mobile for the first time. <br/> If you already completed [recipe updating-salesforce_com-data-in-the-mobile-application], you don't need to reinstall the application. Simply open the mobile application and refresh the data on your device. To synchronize the data:On a BlackBerry smartphone, open the main menu, and select System Info. Open the menu and select Refresh All Data.On an iPhone, tap More, then tap App Info. Tap Sync Now on the toolbar, then tap Refresh All Data. In the mobile application, select the Sales Visit tab. Open the menu, and select New. In the Description field, enter a description of the sales visit. Open the menu and select Save. <br/> The record is saved and sent to along with the user's GPS location.The application permissions on the BlackBerry smartphone control whether or not the browser is allowed to send the GPS coordinates. If the browser is not permitted to send the information, the Visualforce page doesn't display. To ensure that all mobile users are able to properly view the page, the BlackBerry administrator should globally enable the application permissions on the BlackBerry Enterprise Server. To enable the permission on your BlackBerry smartphone:Select Options | Advanced Options | Applications.Highlight Browser, open the main menu, and select Edit Default Permissions.Change the value of Location (GPS) to Allow.


    The Visualforce markup in this recipe uses commands in a JavaScript library provided by to obtain the device's GPS coordinates. The JavaScript library contains commands that trigger actions in Salesforce Mobile, which helps provide a seamless user experience between Visualforce Mobile pages and the native client application. In your Visualforce pages, use the following static resource to point to the JavaScript library:
    <script type="application/x-javascript" src="/mobileclient/api/mobileforce.js"></script>
    External websites must include the instance name in the src parameter:
    <script type="application/x-javascript" src=""></script>

    The actions in the JavaScript library can be used in any Visualforce page. These commands work on JavaScript-enabled devices that support Visualforce. Currently, these devices include iPhones and BlackBerry smartphones. When using the JavaScript library for pages that display on BlackBerry smartphones, recommends that version 4.6 or later of the BlackBerry operating system is installed on the device.

    For more information, see “Using the JavaScript Library” in the Visualforce Developer's Guide.


Recipe Activity - Please Log in to write a comment

Someone knows how to do in android?

by Juan Pablo Gracia  (2012-08-06)


Good post. It works fine for iPhones.

I am wondering if is going to be available the same functionality for Android users.

Thanks in advance,
Rafael Ferrer

by Rafael Martins  (2012-03-22)


It works fine..

Good Start for newbies here!!! 

by Charan Vuyyuru  (2011-06-13)


Vote to Verify a Recipe

Verifying a recipe is a way to give feedback to others and broaden your own understanding of the capabilities on When you verify a recipe, please make sure the code runs, and the functionality solves the articulated problem as expected.

Please make sure:
  • All the necessary pieces are mentioned
  • You have tested the recipe in practice
  • Have sent any suggestions for improvements to the author

Please Log in to verify a recipe

You have voted to verify this recipe.