サイト説明

サイトの目的

本サイトは、佐世保市内の在宅医療に対応可能な薬局を検索し、医療機関や患者家族が適切な薬局を選ぶための支援を目的としています。地域の薬局と医療機関の連携を強化し、在宅医療の質の向上を目指します。

主な機能

情報の表示方法

本サイトは、Googleスプレッドシートから薬局の情報を取得し、以下のように表示しています:

薬局の緯度・経度自動入力

薬局登録フォームで入力された住所(例:「長崎県佐世保市天神1丁目」)から、自動で緯度・経度を取得し、スプレッドシートの列AJ(緯度)、列AK(経度)に保存する方法を以下に説明します。Google Apps Script (GAS) を使用し、Google Maps サービス(APIキー不要)または Google Maps Geocoding API(APIキー必要)を活用します。

手順

  1. Googleフォームとスプレッドシートの確認
    • フォーム(薬局登録フォーム)に住所入力欄を設定。
    • スプレッドシートの列Eに住所、列AJ(緯度)、列AK(経度)が対応。
  2. Google Apps Script の設定
    • スプレッドシートを開き、「拡張機能」→「Apps Script」を選択。
    • 以下のコードを貼り付け、プロジェクトを保存。
  3. トリガーの設定
    • Apps Script エディタで「トリガー」→「トリガーの追加」。
    • イベント:フォーム送信時、関数:onFormSubmit
  4. テスト
    • フォームで住所(例:「長崎県佐世保市天神1丁目」)を送信。
    • スプレッドシートの列AJ(例:33.1685)、列AK(例:129.7250)を確認。

コード例(Google Maps サービス:APIキー不要)

以下のコードは、Google Apps Script の Maps サービスを使用し、住所から緯度・経度を取得します。小規模な利用(例:月50件)に適しています。

function onFormSubmit(e) {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  const lastRow = sheet.getLastRow();
  const address = sheet.getRange(lastRow, 5).getValue(); // 列E(住所)
  if (address) {
    try {
      const response = Maps.newGeocoder().geocode(address);
      if (response.results.length > 0) {
        const { lat, lng } = response.results[0].geometry.location;
        sheet.getRange(lastRow, 36).setValue(lat); // 列AJ(緯度)
        sheet.getRange(lastRow, 37).setValue(lng); // 列AK(経度)
      } else {
        sheet.getRange(lastRow, 36).setValue('取得失敗');
        sheet.getRange(lastRow, 37).setValue('取得失敗');
      }
    } catch (error) {
      sheet.getRange(lastRow, 36).setValue('エラー');
      sheet.getRange(lastRow, 37).setValue(error.message);
    }
  }
}

コード例(Google Maps Geocoding API:APIキー必要)

高頻度または高精度が必要な場合、Google Maps Geocoding API を使用します。Google Cloud Console で API キーを取得してください(詳細)。コスト:$5/1000リクエスト、無料枠 $200/月。

function onFormSubmit(e) {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  const lastRow = sheet.getLastRow();
  const address = sheet.getRange(lastRow, 5).getValue(); // 列E(住所)
  const apiKey = 'YOUR_API_KEY'; // Google Cloud Console で取得
  const url = `https://maps.googleapis.com/maps/api/geocode/json?address=${encodeURIComponent(address)}&key=${apiKey}`;
  try {
    const response = UrlFetchApp.fetch(url);
    const json = JSON.parse(response.getContentText());
    if (json.status === 'OK' && json.results.length > 0) {
      const { lat, lng } = json.results[0].geometry.location;
      sheet.getRange(lastRow, 36).setValue(lat); // 列AJ(緯度)
      sheet.getRange(lastRow, 37).setValue(lng); // 列AK(経度)
    } else {
      sheet.getRange(lastRow, 36).setValue('取得失敗');
      sheet.getRange(lastRow, 37).setValue(json.status);
    }
  } catch (error) {
    sheet.getRange(lastRow, 36).setValue('エラー');
    sheet.getRange(lastRow, 37).setValue(error.message);
  }
}

代替手段:Nominatim(OpenStreetMap)

無料で利用可能ですが、1秒1リクエストの制限があります。以下は Nominatim を使用したコード例です。

function onFormSubmit(e) {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  const lastRow = sheet.getLastRow();
  const address = sheet.getRange(lastRow, 5).getValue(); // 列E(住所)
  const url = `https://nominatim.openstreetmap.org/search?format=json&q=${encodeURIComponent(address)}&countrycodes=JP&limit=1`;
  try {
    const response = UrlFetchApp.fetch(url, { headers: { 'User-Agent': 'SaseboZaitakuPharmacyMatch/1.0' } });
    const data = JSON.parse(response.getContentText());
    if (data.length > 0) {
      const { lat, lon } = data[0];
      sheet.getRange(lastRow, 36).setValue(lat); // 列AJ(緯度)
      sheet.getRange(lastRow, 37).setValue(lon); // 列AK(経度)
    } else {
      sheet.getRange(lastRow, 36).setValue('取得失敗');
      sheet.getRange(lastRow, 37).setValue('取得失敗');
    }
    Utilities.sleep(1000); // 1秒1リクエスト制限
  } catch (error) {
    sheet.getRange(lastRow, 36).setValue('エラー');
    sheet.getRange(lastRow, 37).setValue(error.message);
  }
}

注意点

詳細は Google Maps Platform または Nominatim を参照してください。

運用について

本サイトは、Googleスプレッドシートでデータを管理し、薬局情報の更新を容易にしています。薬局登録は以下のフォームから行えます:

薬局登録フォーム