<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Arif Nurdian</title>
    <description>The latest articles on DEV Community by Arif Nurdian (@arif_nurdian_1845d6e837d0).</description>
    <link>https://dev.to/arif_nurdian_1845d6e837d0</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3173724%2Fb971ce30-2b4a-4150-81e7-832c7f3569e1.jpg</url>
      <title>DEV Community: Arif Nurdian</title>
      <link>https://dev.to/arif_nurdian_1845d6e837d0</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/arif_nurdian_1845d6e837d0"/>
    <language>en</language>
    <item>
      <title>How to find the closest destination from one origin to multiple destination options using Google Maps</title>
      <dc:creator>Arif Nurdian</dc:creator>
      <pubDate>Sat, 17 May 2025 14:11:54 +0000</pubDate>
      <link>https://dev.to/arif_nurdian_1845d6e837d0/how-to-find-the-closest-destination-from-one-origin-to-multiple-destination-options-using-google-1bnp</link>
      <guid>https://dev.to/arif_nurdian_1845d6e837d0/how-to-find-the-closest-destination-from-one-origin-to-multiple-destination-options-using-google-1bnp</guid>
      <description>&lt;p&gt;To find the closest destination from one origin to multiple destination options using Google Maps, you should use the Google Distance Matrix API.&lt;/p&gt;

&lt;p&gt;Here’s a complete guide using Next.js + @react-google-maps/api to find the nearest destination:&lt;/p&gt;

&lt;h2&gt;
  
  
  Overview:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Use the Distance Matrix API to get travel distances between one origin and multiple destinations.&lt;/li&gt;
&lt;li&gt;Compare the distances and pick the closest one.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step-by-step Implementation:
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Enable Distance Matrix API
&lt;/h3&gt;

&lt;p&gt;Go to: &lt;a href="https://console.cloud.google.com/apis/library" rel="noopener noreferrer"&gt;https://console.cloud.google.com/apis/library&lt;/a&gt;&lt;br&gt;
Search for Distance Matrix API, and click Enable.&lt;/p&gt;
&lt;h3&gt;
  
  
  2. Add destinations list
&lt;/h3&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const destinations = [
  { name: 'Location A', address: 'Jl. Sudirman, Jakarta' },
  { name: 'Location B', address: 'Jl. Thamrin, Jakarta' },
  { name: 'Location C', address: 'Jl. Merdeka, Jakarta' },
];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Code Sample in React (with Next.js)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'use client';

import React, { useRef, useState } from 'react';
import { useJsApiLoader, Autocomplete } from '@react-google-maps/api';

const libraries = ['places'];

export default function FindClosestLocation() {
  const { isLoaded } = useJsApiLoader({
    googleMapsApiKey: process.env.NEXT_PUBLIC_GOOGLE_MAPS_API_KEY,
    libraries,
  });

  const originRef = useRef();
  const [closest, setClosest] = useState(null);
  const [loading, setLoading] = useState(false);

  const destinations = [
    { name: 'Location A', address: 'Jl. Sudirman, Jakarta' },
    { name: 'Location B', address: 'Jl. Thamrin, Jakarta' },
    { name: 'Location C', address: 'Jl. Merdeka, Jakarta' },
  ];

  const findClosest = () =&amp;gt; {
    const origin = originRef.current?.value;
    if (!origin) return alert('Please enter an origin');

    const service = new window.google.maps.DistanceMatrixService();

    setLoading(true);
    service.getDistanceMatrix(
      {
        origins: [origin],
        destinations: destinations.map((d) =&amp;gt; d.address),
        travelMode: window.google.maps.TravelMode.DRIVING,
        unitSystem: window.google.maps.UnitSystem.METRIC,
      },
      (response, status) =&amp;gt; {
        setLoading(false);
        if (status !== 'OK') return alert('Error: ' + status);

        const distances = response.rows[0].elements;

        let minIndex = 0;
        for (let i = 1; i &amp;lt; distances.length; i++) {
          if (distances[i].distance.value &amp;lt; distances[minIndex].distance.value) {
            minIndex = i;
          }
        }

        setClosest({
          destination: destinations[minIndex],
          distance: distances[minIndex].distance.text,
          duration: distances[minIndex].duration.text,
        });
      }
    );
  };

  if (!isLoaded) return &amp;lt;p&amp;gt;Loading...&amp;lt;/p&amp;gt;;

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h2&amp;gt;Find Closest Location&amp;lt;/h2&amp;gt;

      &amp;lt;Autocomplete&amp;gt;
        &amp;lt;input
          type="text"
          ref={originRef}
          placeholder="Enter your origin"
          style={{ width: '300px', padding: '0.5rem', marginRight: '1rem' }}
        /&amp;gt;
      &amp;lt;/Autocomplete&amp;gt;

      &amp;lt;button onClick={findClosest} disabled={loading} style={{ padding: '0.5rem 1rem' }}&amp;gt;
        {loading ? 'Finding...' : 'Find Closest'}
      &amp;lt;/button&amp;gt;

      {closest &amp;amp;&amp;amp; (
        &amp;lt;div style={{ marginTop: '1rem' }}&amp;gt;
          &amp;lt;h3&amp;gt;Closest: {closest.destination.name}&amp;lt;/h3&amp;gt;
          &amp;lt;p&amp;gt;Address: {closest.destination.address}&amp;lt;/p&amp;gt;
          &amp;lt;p&amp;gt;Distance: {closest.distance}&amp;lt;/p&amp;gt;
          &amp;lt;p&amp;gt;Duration: {closest.duration}&amp;lt;/p&amp;gt;
        &amp;lt;/div&amp;gt;
      )}
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
  </channel>
</rss>
