[JAVA] [Rails] How to get location information using Geolocation API

Development environment

・ Ruby: 2.5.7 Rails: 5.2.4 ・ Vagrant: 2.2.7 -VirtualBox: 6.1 ・ OS: macOS Catalina

Premise

The following has been implemented.

Slim introductionGoogle Map display

Enable Geolocation API

Please enable the Geolocation API by following the same procedure as" Enabling the Geocoding API "in the article below.

How to calculate latitude / longitude using Geolocation API

Implementation

slim:~.html.slim


#map style='height: 500px; width: 500px;'

- google_api = "https://maps.googleapis.com/maps/api/js?key=#{ ENV['GOOGLE_MAP_API'] }&callback=initMap".html_safe
script{ async src=google_api }

javascript:

  let map;

  function initMap() {
    //Get location information
    navigator.geolocation.getCurrentPosition(function (position) {
    LatLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);

      //Display mainly the acquired location information
      map = new google.maps.Map(document.getElementById('map'), {
        center: LatLng,
        zoom: 15
      });
    });
  }

Caution

If you do not disable turbolinks, the map will not switch, so be sure to disable it.

How to disable turbolinks

Recommended Posts

[Rails] How to get location information using Geolocation API
How to get article data using Qiita API
How to get followers and followers from python using the Mastodon API
[Python] I tried to get various information using YouTube Data API!
[Rails] google maps api How to post and display including map information
How to get a sample report from a hash value using VirusTotal's API
[Rails] How to detect radical images by analyzing images using Cloud Vision API
How to display Map using Google Map API (Android)
How to use folium (visualization of location information)
Use the MediaWiki API to get Wiki information
How to analyze with Google Colaboratory using Kaggle API
Get information with zabbix api
Get GitHub information using PyGithub
I tried to get the location information of Odakyu Bus
How to reset password via API using Django rest framework
I tried to get Web information using "Requests" and "lxml"
How to get temperature from switchBot thermo-hygrometer using raspberry Pi
I tried to get various information from the codeforces API
Output product information to csv using Rakuten product search API [Python]
Call github api in python to get pull request information
How to install python using anaconda
[Rails] How to display Google Map
Get Salesforce data using REST API
How to get the Python version
How to get started with Scrapy
How to get started with Python
Get Amazon data using Keep API # 1 Get data
How to get started with Django
How to use OpenPose's Python API
Try to get statistics using e-Stat
How to use bing search api
[Python] How to use Typetalk API
[Yahoo! Weather Replacement Version] How to get weather information with LINE Notify + Python
How to get a value from a parameter store in lambda (using python)
I tried to get the movie information of TMDb API with Python
How to search using python's astroquery and get fits images with skyview
[Rails] How to introduce Google Analytics [Easy]
How to get parent id with sqlalchemy
How to get rid of long comprehensions
How to draw a graph using Matplotlib
Bulk posting to Qiita: Team using Qiita API
How to get IP when Tornado + nginx
How to set up SVM using Optuna
Get mail using Gmail API in Java
How to install a package using a repository
How to set xg boost using Optuna
How to get a stacktrace in python
Get Youtube data in Python using Youtube Data API
How to get colored output to the console
How to download youtube videos using pytube3
[Python] Get Python package information with PyPI API
How to use GCP's Cloud Vision API
How to get started with laravel (Linux)
Get coincheck virtual currency information with API ♪
[Rails] How to calculate latitude and longitude with high accuracy using Geocoding API and display it on Google Map
How to get the notebook name you are currently using in Google Colab
How to code a drone using image recognition
How to implement Rails helper-like functionality in Django
Procedure to use TeamGant's WEB API (using python)
How to set up Random forest using Optuna
[Python] Get all comments using Youtube Data API