2

As of API 32.0, it's easy to create a map in Visualforce with <apex:map> and <apex:mapMarker> even takes string addresses as a position and will geocode the address automatically. You can see an example in the dev guide.

I'm using the map to show several addresses on a custom object:

<apex:map width="600px" height="400px" mapType="roadmap"
center="{!Account.BillingStreet},{!Account.BillingCity},{!Account.BillingState}">

    <apex:repeat value="{! MyObj__c.Addresses }" var="addr">
      <apex:mapMarker title="{! addr.Name }"
        position="{!addr.MailingStreet},{!addr.MailingCity},{!addr.MailingState}"
      />
    </apex:repeat>

 </apex:map>

This works all well and good, as the page geocodes the address and plops a marker on the map. However, we want to do some DISTANCE based SOQL queries with those locations. There are third-party APIs and plugins to geocode arbitrary addresses in Salesforce, but we'd rather stick with platform features. And indeed, the platform is capable of geocoding up to 10 arbitrary locations in a page request.

Is there ANY WAY to get the lat/long that resulted in this mapping that can be sent back to the server for storage? How can we cache this very useful data?

0

1 Answer 1

1

Winter '16 release notes - Automatically Get Geocode Information for Addresses

In Spring '16, we plan to add geocode information to all your accounts, contacts, and leads.


I don't think there is currently any native geocoding currently available with that control.

By all accounts, the apex:map control is using a third party service via JavaScript to create the map and perform address to lat/long geocoding.

It would be a hack, but you could try and interact with the resulting JavaScript and DOM to extract the lat/log results. It would probably be fragile. Maybe do the geocoding directly with a third party service?


There is the idea Spring 15 Maps Feature should populate lat and lon on the address fields., which includes details about the Salesforce Summer '15 Geocoding Pilot.

Got geocodes? In the Salesforce Summer '15 release, we are piloting a new geocoding capability where we add geocode information to your standard addresses in Salesforce.

Geocode information, which includes latitude and longitude for addresses, can be used to assign territories, segment markets, target specific areas, and analyze demographics. The pilot will focus on the first step - populating the latitude, longitude, and accuracy fields for standard addresses in accounts, contacts, and/or leads.

Follow this link to learn more! http://goo.gl/forms/qNRLdcTphw

4
  • Hi Daniel, standard addresses are indeed geocoded, I'm looking at custom addresses unfortunately. I've tried to reach through the JS to get to the lat/long (indeed, I actually found where it is stored in Chrome dev tools!), but due to same origin policy and Visualforce iFrame-ing, I cannot. I think I'm going to have to go 3rd party. Commented Jul 21, 2015 at 3:27
  • @ChristianCarter if you are facing issues with cross domain communication you can use PortHole JS framework. This small JS library is intended to resolve cross domain issues.
    – Saroj Bera
    Commented Jul 21, 2015 at 8:38
  • @SarojBera This looks good, but I can't figure out how to execute any javascript in the map iframe. It runs from a different domain than my visualforce and a different domain from Aloha. Have any ideas in a way that doesn't require any changes to the iframe? Commented Jul 21, 2015 at 14:58
  • 1
    @ChristianCarter, Yes, for custom addresses you are likely going to need to use your own third party to geocode the addresses. Makes me wonder if you could create a temporary Lead with the standard address you want to geocode. Then once you have the lat/long, delete the lead. Commented Jul 22, 2015 at 2:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .