I've activated Geolocations in Salforce for Contacts. I have this code I found as a VisualForce mark Up (this is a continuation of another question I had asked earlier):

<apex:page standardController="Contact">
    <apex:map height="800" width="800">
        <apex:mapMarker position="{latitude: {!Contact.MailingLatitude}, longitude: {!Contact.MailingLongitude}}" />

I want to be able to see a map that shows contacts that are within a certain area (example 15 miles radius) from the contact record that I decide to go visit (the Contact record I'm on at the moment. I already have google maps as it's included under contacts and account. The above mark up shows me a map but does not capture the current contact's location nor any other contact that could be in the database in the whereabouts. I'm referring to nearby contacts from other accounts. How do I do this? Can you help me alter the code to achieve this? I would like to do this for contacts first but I would then also want to do similar for Accounts. I don't know programming so any concrete coding example to add onto my above code would be great and also to know where to include the code.

2 Answers 2


There is a query in help & training: https://help.salesforce.com/HTViewSolution?id=000230955&language=en_US

SELECT Name, Email, MailingAddress, MailingCity, MailingState, MailingCountry, MailingLatitude, MailingLongitude
FROM Contact 
WHERE distance(MailingAddress, geolocation(37.775, -122.418), 'mi') < 50 
ORDER BY distance(MailingAddress, geolocation(37.775, -122.418), 'mi')

Use that and substitute the field references for the lat/lon numbers. And change from contact to account fields.

  • Thank you for this. The only thing is that changing the lat/long nunbers doesn''t help. This needs to be dynamic in the sense that it's should be based on the address of the Contact or Account record that the user is currently on and not based on a fixed one time address. The user can't be changing a VisualForce lat/long.
    – Andrea
    Dec 6, 2016 at 23:31
  • Exactly. So use a Standard Controller or a Custom Controller with this query and put in, say, contact.MailingLatitude, etc. Dec 6, 2016 at 23:32
  • Unfortunately, not being a developer, I'm missing some knowledge to know how to set up the code in the Controller and how to call it inside the Visualforce page. Can you help me with a cding example to get the dynamic Contact's nearby locations?
    – Andrea
    Dec 7, 2016 at 3:11
  • Would you have a sample on how to put your above code inside a Controller and how to insert the Controller call from VisualForce?
    – Andrea
    Dec 7, 2016 at 17:12
  • Any additional tips for my above question?
    – Andrea
    Dec 9, 2016 at 20:21

To do this in Lightning, use the code from this Trailhead module: https://trailhead.salesforce.com/project/workshop-lightning-restaurant-locator/

  • Link only answers will break if the link breaks. If this link answers the question, can you please include a relevant excerpt here? It looks like you may have intended to leave this as a comment?
    – Adrian Larson
    Dec 6, 2016 at 23:35
  • It's a link to a trailhead project. There isn't an excerpt to show - just go to the module. I'm not going to copy/paste the entire Trailhead project! Dec 6, 2016 at 23:36
  • Then maybe it doesn't make sense as an answer?
    – Adrian Larson
    Dec 6, 2016 at 23:37
  • Seriously, it's great as an answer. Did you even click the link? Instead of just criticizing that I put a link to some code that would help, take the time to click the link. Dec 6, 2016 at 23:38
  • I did click the link, I just don't understand why you answered twice. You could also just include it as part of your other answer. Why post 2 separately?
    – Adrian Larson
    Dec 6, 2016 at 23:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.