DEV Community

Vratislav Kopp for Superface

Posted on

Using a different geocode service for different countries

When we want to convert an address into latitude and longitude, we usually have to pick the best provider we can and deal with the varying levels of quality. For instance, sometimes one provider is better than another in a specific area of the world. The first article in this series shows how to access multiple geocoding providers with a single implementation using Superface OneSDK. Let’s now take a look at how can different providers be used based on the country to get the best quality.

Let’s say we do some research and determine that Google Maps work better in the US than other providers (this may not be true). And let’s say that we decide to use Google Maps only for US addresses, but Here.com for everywhere else.

We can write a bit of JavaScript to capture this decision.

const providerName = addressCountry === 'US' ? 'google-apis-maps' : 'here';
Enter fullscreen mode Exit fullscreen mode

Normally, after we sign up for accounts for these services, we’d have to find their API documentation, learn how their API works, install their SDKs or build our own integration code, then manage all the differences between them over time. Instead, we can use our OneSDK to make this simpler.

const provider = await sdk.getProvider(providerName);
const result = await profile.getUseCase('Geocode').perform(
  {
      addressCountry: 'United States',
        addressLocality: 'Manhattan',
        postalCode: 'NY 10036',
        streetAddress: 'Times Square',
  },
  { provider }
);
Enter fullscreen mode Exit fullscreen mode

We’re able to interact with both Google Maps and Here.com with less than 10 lines of code, and we can jump between them based on any criteria we choose. In this case, we chose based on the country, but maybe for your case it needs to be something different.

Check out our geocode page to see learn more about using Superface for geocoding.

Read next

This post was originally written by @smizell

Top comments (0)