DEV Community

Vratislav Kopp for Superface

Posted on

5 2

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

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

Top comments (0)

Billboard image

Try REST API Generation for MS SQL Server.

DevOps for Private APIs. With DreamFactory API Generation, you get:

  • Auto-generated live APIs mapped from database schema
  • Interactive Swagger API documentation
  • Scripting engine to customize your API
  • Built-in role-based access control

Learn more