1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Google map APIで特定の周辺の詳細データを取得のやり方

Last updated at Posted at 2022-03-17

1.環境

ブラウザ : Google Chrom
言語   : GoogleAppsScript

2.前提

Google mapのAPIをキーを取得していること

3.東京駅周辺のレストランの情報を取得

function map_data() {

  //東京駅の周辺の緯度と軽度を検索を取得する
  var geocoder = Maps.newGeocoder();
  geocoder.setLanguage('ja');
  var response = geocoder.geocode('東京駅')

  // 経度
  var lat = response['results'][0]['geometry']['location']['lat'];
  // 緯度
  var lng = response['results'][0]['geometry']['location']['lng'];

  var URL = '';

  //GoogleのAPIキー
  var KEY = 'key=' + '';
  //GETで取得する
  var URL_1 = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?'
  // 緯度と経度を設定する
  var LOCATION = '&location=' + String(lat) + ',' + String(lng); 
  // 緯度と経度から半径500メートルに設定
  var RADIUS = '&radius=' + '500';
  // 検索する情報を料理店を設定
  var TYPE = '&type=' + 'restaurant';
  //関連情報を設定する
  var KEYWORD = '&keyword=' + '東京駅';
  // 取得情報を日本語のみに設定する
  var LANG = '&language=' + 'ja';

  var URL = URL_1+KEY+LOCATION+RADIUS+TYPE+KEYWORD+LANG;
  var res = UrlFetchApp.fetch(URL);
  // jsonデータで取得する
  var json = JSON.parse(res.getContentText());

  //取得情報を入れる配列
  var c = [];

  
  for(let i = 0; i < json['results'].length; i++){
    c.push([
        // 店名
      json['results'][i]['name'],
      // 県
      json['results'][i]['plus_code']['compound_code'].split('、')[1],
      // 住所
      json['results'][i]['vicinity']
    ]);
  }
  
  // 次のページのトークンを取得する
  var NEXT_PAGETOKEN = json['next_page_token'];

  
  while(true){
    
    if (typeof NEXT_PAGETOKEN == "undefined" || NEXT_PAGETOKEN == '') {
      break;
    }

    var URL = '';
    var URL_2 = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?'
    var PAGETOKEN = '&pagetoken=' + NEXT_PAGETOKEN;

    var URL = URL_2+KEY+PAGETOKEN;
    var res = UrlFetchApp.fetch(URL);
    var json = JSON.parse(res.getContentText());
  
    for(let i = 0; i < json['results'].length; i++){
      c.push([
          // 店名
        json['results'][i]['name'],
        // 県
        json['results'][i]['plus_code']['compound_code'].split('、')[1],
        // 住所
        json['results'][i]['vicinity']
      ]);
    }
    NEXT_PAGETOKEN = json['next_page_token'];
  }


  Logger.log(c)
}

店名と県と住所をコンソールに表示する。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?