Source: https://developers.woosmap.com/js-samples/geolocation-nearby-stores/

> For clean Markdown of any page, append `.md` to the page URL.

> For a complete documentation index, see https://developers.woosmap.com/llms.txt

# Stores Nearby Geolocation



## Example

https://demo.woosmap.com/js-samples/samples/geolocation-nearby-stores/app/dist/
[](https://demo.woosmap.com/js-samples/samples/geolocation-nearby-stores/highlight/highlight.html "Open in new tab with highlighted code")
Try sample 

- [CodeSandbox](https://codesandbox.io/p/devbox/github/woosmap/js-samples/tree/master/dist/samples/geolocation-nearby-stores/app?file=index.ts)
- [JsFiddle](https://jsfiddle.net/gh/get/library/pure/woosmap/js-samples/tree/master/dist/samples/geolocation-nearby-stores/jsfiddle)
- [Clone on Github](https://github.com/Woosmap/js-samples/tree/sample/geolocation-nearby-stores)

```typescript
const woosmap_key = "YOUR_API_KEY";
let placeholder: HTMLElement;

const radiusMeters = 50000;
function distance(dist: number) {
  if (dist > 1000) {
    return `${Math.round(dist / 100) / 10} km`;
  }
  return `${Math.round(dist)} m`;
}

function renderHTML(properties) {
  let content = `<b>${properties.name}</b><br/>`;
  if (properties.address.lines.hasOwnProperty.call(0)) {
    content += `${properties.address.lines[0]}<br/>`;
  }
  content += `${properties.address.city}<br>`;
  content += `at <b>${distance(properties.distance)}</b><br/>`;

  return content;
}

function displayNearestStore(stores) {
  if (stores && stores.features && stores.features.length > 0) {
    const store = stores.features[0];
    const properties = store.properties;
    if (placeholder) {
      placeholder.innerHTML = renderHTML(properties);
    }
  } else {
    placeholder.innerHTML = "<p>No Stores returned... (insufficient accuracy)";
  }
}

document.addEventListener("DOMContentLoaded", () => {
  placeholder = document.getElementById(
    "recommendation-placeholder",
  ) as HTMLElement;
  fetchNearbyStores();
});
function fetchNearbyStores(): void {
  fetch(
    `https://api.woosmap.com/geolocation/stores/?key=${woosmap_key}&radius=${radiusMeters}`,
  )
    .then((result) => {
      return result.json();
    })
    .then(({ stores }) => {
      displayNearestStore(stores);
    });
}
```

```javascript
const woosmap_key = "YOUR_API_KEY";
let placeholder;
const radiusMeters = 50000;

function distance(dist) {
  if (dist > 1000) {
    return `${Math.round(dist / 100) / 10} km`;
  }
  return `${Math.round(dist)} m`;
}

function renderHTML(properties) {
  let content = `<b>${properties.name}</b><br/>`;

  if (properties.address.lines.hasOwnProperty.call(0)) {
    content += `${properties.address.lines[0]}<br/>`;
  }

  content += `${properties.address.city}<br>`;
  content += `at <b>${distance(properties.distance)}</b><br/>`;
  return content;
}

function displayNearestStore(stores) {
  if (stores && stores.features && stores.features.length > 0) {
    const store = stores.features[0];
    const properties = store.properties;

    if (placeholder) {
      placeholder.innerHTML = renderHTML(properties);
    }
  } else {
    placeholder.innerHTML = "<p>No Stores returned... (insufficient accuracy)";
  }
}

document.addEventListener("DOMContentLoaded", () => {
  placeholder = document.getElementById("recommendation-placeholder");
  fetchNearbyStores();
});

function fetchNearbyStores() {
  fetch(
    `https://api.woosmap.com/geolocation/stores/?key=${woosmap_key}&radius=${radiusMeters}`,
  )
    .then((result) => {
      return result.json();
    })
    .then(({ stores }) => {
      displayNearestStore(stores);
    });
}
```

```css
/*
 * Always set the map height explicitly to define the size of the div element
 * that contains the map.
 */
#map {
  height: 100%;
}

/*
 * Optional: Makes the sample page fill the window.
 */
html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Helvetica Neue, Arial, Noto Sans, sans-serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol, Noto Color Emoji;
}

.flex {
  display: flex;
  flex-direction: column;
}

.space-between {
  justify-content: space-between;
}
```

```html
<html>
  <head>
    <title>Stores Nearby Geolocation</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta charset="utf-8" />

    <link rel="stylesheet" type="text/css" href="./style.css" />
    <script type="module" src="./index.js"></script>
  </head>
  <body>
    <div
      class="flex space-between"
      style="
        background: url(&quot;https://developers.woosmap.com/assets/images/woman-coffee.jpg&quot;)
          no-repeat;
        background-size: contain;
        min-height: 250px;
      "
    >
      <div style="text-align: right; padding: 1em">
        <strong>Take a little Coffee Break</strong>
        <div>
          <div id="recommendation-placeholder">
            <img
              src="https://developers.woosmap.com/assets/images/loader.svg"
              alt="loader"
            />
          </div>
        </div>
      </div>
    </div>
  </body>
</html>
```

## Running the Sample Locally

Before you can run this sample on your local machine, you need to have Git and Node.js installed. If they’re not already installed, follow [these instructions](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) to get them set up.

Once you have Git and Node.js installed, you can run the sample by following these steps:

1. Clone the repository and navigate to the directory of the sample.

2. Install the necessary dependencies.

3. Start running the sample.

Here are the commands you can use in your terminal to do this:

```shell
git clone -b sample/geolocation-nearby-stores https://github.com/woosmap/js-samples.git
cd js-samples
npm i
npm start
```

You can experiment with other samples by switching to any branch that starts with the pattern `sample/SAMPLE_NAME`.

```shell
git checkout sample/SAMPLE_NAME
npm i
npm start
```
