Salesforce & Bing Maps Integration

Aryakaran Gupta
3 min readFeb 20, 2021

Bing Maps REST API can easily be integrated within salesforce. In this article we will see how we can use different APIs of Bing and use it in our salesforce application. For this we will be creating a custom lightning component to test various APIs provided by Bing.

Authentication?

There is no specific type of authentication used. All we need is to set up a Bing maps key. You can request a free developer key from the maps portal.

Create a new basic key from the Application tab. There are two types of keys you can request: Basic or Enterprise. Basic is only used for testing and evaluation purposes and Enterprise is used for large scale business application which also comes with a support package. More information on keys and usage can be found here. For this article we will be using a Basic key.

Integration

Before we start writing code firstly we need to understand all the possible prerequisites. In the previous section we mentioned about setting a key. Store this key somewhere in a custom setting or metadata so that it is configurable. This example will have a direct place holder for the key reference in the apex class. Secondly we need to understand the endpoint URL and types of parameter which are mandatory. For this integration we will be using their standard rest endpoint:

https://dev.virtualearth.net/REST/v1/

Note: To perform any callouts with external system, The endpoint URL need to be added in Remote Sites. Please don’t forget to add the above URL on the remote sites!

So once we are all set. You have your key and the endpoint setup. Lets begin coding!

Below class contains callouts to 3 of the Bing REST API types. We will be using the Locations and Distance API:

Find Locations by Point:

To fetch the address by coordinates use this endpoint. Example request will look something like this:

http://dev.virtualearth.net/REST/v1/Locations/{point}?includeEntityTypes={entityTypes}&includeNeighborhood={includeNeighborhood}&include={includeValue}&key={BingMapsKey}

Details of the API Parameters can be found in the official documentation

Find Locations by Address:

Use this to fetch the coordinates if you know the address:

http://dev.virtualearth.net/REST/v1/Locations?countryRegion={countryRegion}&adminDistrict={adminDistrict}&locality={locality}&postalCode={postalCode}&addressLine={addressLine}&userLocation={userLocation}&userIp={userIp}&usermapView={usermapView}&includeNeighborhood={includeNeighborhood}&maxResults={maxResults}&key={BingMapsKey}

Details of the API Parameters can be found in the official documentation

Calculate Driving Distance:

To calculate distance between two points. Use the below request:

http://dev.virtualearth.net/REST/v1/Routes?wayPoint.1={wayPoint1}&viaWaypoint.2={viaWaypoint2}&waypoint.3={waypoint3}&wayPoint.n={waypointN}&heading={heading}&optimize={optimize}&avoid={avoid}&distanceBeforeFirstTurn={distanceBeforeFirstTurn}&routeAttributes={routeAttributes}&timeType={timeType}&dateTime={dateTime}&maxSolutions={maxSolutions}&tolerances={tolerances}&distanceUnit={distanceUnit}&key={BingMapsKey}

Details of the API Parameters can be found in the official documentation

Once the class is set we create our custom lightning component to the test the APIs:

HTML File for LWC
JS File for LWC
XML File for LWC

Once we are done with the components lets begin some tests. Following is the screenshot of the API tests done from the lightning component for each of the above API mentioned:

Easy to integrate isn’t it? There are other variations on the various types of the API you can check. Full list here. Next time we will be integrating their Map UI on a custom page with cool interactive features.

Till then Happy Integrating!

--

--