Skip to content

Instantly share code, notes, and snippets.

@dschach
Forked from joshbirk/geocontact.page
Created October 9, 2012 16:29

Revisions

  1. @joshbirk joshbirk created this gist Oct 8, 2012.
    46 changes: 46 additions & 0 deletions geocontact.page
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    <apex:page StandardController="Contact" showHeader="true" sidebar="false">

    <script>
    var pos = {};
    function success(position) {
    pos = position.coords;
    console.log(pos);
    }

    function error(msg) {
    console.log(msg);
    }

    if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(success, error);
    } else {
    error('not supported');
    }

    function setPos() {
    var inputs = document.getElementsByTagName('input');
    for(var x = 0; x < inputs.length; x++) {
    if(inputs[x].id.indexOf('contactlat') >= 0) { inputs[x].value = pos.latitude; }
    if(inputs[x].id.indexOf('contactlong') >= 0) { inputs[x].value = pos.longitude; }
    }
    }

    </script>

    <apex:form >

    <apex:pageBlock>
    <apex:pageBlockButtons>
    <apex:commandButton action="{!quicksave}" value="Save"/>
    <button onClick="setPos(); return false;" id="setPosBtn">Set Position</button>
    </apex:pageBlockButtons>
    <apex:pageBlockSection title="My Contact" columns="2">
    <apex:inputField value="{!Contact.FirstName}" />
    <apex:inputField value="{!Contact.LastName}" />
    <apex:inputField value="{!Contact.Location__Latitude__s}" id="contactlat" />
    <apex:inputField value="{!Contact.Location__Longitude__s}" id="contactlong" />
    </apex:pageBlockSection>
    </apex:pageBlock>
    </apex:form>

    </apex:page>
    18 changes: 18 additions & 0 deletions geolocation.cls
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    public with sharing class GeoContactController {

    public GeoContactController() {

    }

    public GeoContactController(ApexPages.StandardController stdc) {

    }

    @RemoteAction
    public static List<Contact> getNearbyContacts(Decimal clat, Decimal clong) {
    String q = 'SELECT Name FROM Contact WHERE DISTANCE(Location__c, GEOLOCATION('+String.valueOf(clat)+','+String.valueOf(clong)+'), "mi") < 10';
    List<Contact> contacts = Database.query(q);
    return contacts;
    }

    }