Skip to Content
Author's profile photo Robert Witt

Implementing a Geocoder for Google Maps in SAP CRM

One important aspect and target of a good Customer Relationship Management system is to manage acurate customer address and contact information. SAP integrates a framework in SAP CRM to gather and store geolocation information about a customer’s location, i. e. the latitude and longitude of an address. This is especially critical in scenarios where the exact customer location is required (i. e. for weather information services).

You as a customer can enhance the framework and hook your own geocoding service into it. By standard the geocoding service in SAP CRM determines the geocoordinates based on country and region when an address is saved. Of course for most of the use cases, this is much too unprecise.

In this blog I describe how to use Google Maps as geocoding service in SAP CRM.

The Geocoding API in Google Maps is an HTTP based API where you can pass in an address and get geographical coordinates in return:

https://maps.googleapis.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=YOUR_API_KEY

Check out the API documentation for more details.

To integrate the Google Maps API in SAP CRM, start by creating a Geocoder class. That is a generic ABAP class implementing interface IF_GEOCODING_TOOL. Activate the class only now, it will be implemented later.

Image_01.png

This class must be registered in customizing (SPRO: SAP NetWeaver -> General settings -> Set Geocoding) as geocoder source.

Image_02.png

Next step is to assign the relevant address field to the new geocoder source.

Image_03.png

As the quality of geocoding solutions might vary between countries, you can define a geocoder per country. Here I defined the Google API for German addresses only.

Image_04.png

Now let’s implement the geocoder class and method GEOCODE of interface IF_GEOCODING_TOOL. It is called everytime an address is updated in CRM.

The methods gets a table with addresses and some other information. It should geocode each of the addresses and put the results (latitude and longitude) in the CONTAINERS parameter.

Image_05.png

The custom submethod GEOCODE_ADDRESS invokes another custom class that calls the Google Maps Geocoding API.

Image_06.png

The Geocoding API is an HTTP-based web service API where this class sends GET requests to and consums the result.

Image_07.png

The request URL is created by simply concatenating the base URL and the current address. Please note that a response in XML is requested.

Image_08.png

SAP offers an XML library called iXML to read and parse XML content (see SAP help here). I haven’t found a similar version for JSON with the same capabilities.

Image_09.png

Now if you’re create an account and maintain a German address, our Google Maps geocoder is called and the Geocoding API is asked for the coordinates.

Image_11.png

The geograhical coordinates are stored in table GEOLOC.

Image_12.png

Please share your comments and thoughts. I would really appreciate your feedback.

Assigned Tags

      6 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Anandh Kannusamy
      Anandh Kannusamy

      Dear Robert,

      Thanks for writing this article.

      Please inform us with your experience about Google Geo-coding API license implications.

      Is it commercially free for Production environment?

      Regards,

      K.Anandh

      Author's profile photo Robert Witt
      Robert Witt
      Blog Post Author

      Hi K Anandh,

      In my experience you need a business account in order to use the API commercially. The standard version works for non-commercial and development scenarios. However, in the usage limits I cannot see any restrictions on that, only the number of service requests per day is restricted.

      Please check Google's Terms of Service.

      Regards,

      Robert

      Author's profile photo Dietmar Schmidt
      Dietmar Schmidt

      Hi Robert,

      looks great to me. Thanks for the article.

      The link does not work any more: https://maps.googleapi.com....

      Regards

      Dietmar

      Author's profile photo Robert Witt
      Robert Witt
      Blog Post Author

      Hi Dietmar,

      Thanks for your feedback.

      The base URL for requests is https://maps.googleapis.com/maps/api/geocode/xml?address=AN_ADDRESS&key=YOUR_API_KEY.

      You need to replace AN_ADDRESS with the address you're want the geocoordinates for and YOUR_API_KEY with your API key. You get a key after you have registered. For demo cases you could also send requests without the key parameter.

      Hope this solves your issue.

      Regards,

      Robert

      Author's profile photo Dietmar Schmidt
      Dietmar Schmidt

      Yep, thanks a lot....  🙂

      Author's profile photo Amit Hiremath
      Amit Hiremath

      Hello Robert,

      thanks a lot for this post, we have a similar requirement to determine geocodes while creating BP in ERP and in S/4. I tried to read through your code lines and see if this help, i am missing some information on the declaration for es_geolocation and few methods. Possible to help me out with the complete dump