LoginSignup
1
0

wslでelixir その139

Posted at

概要

wsl(wsl2じゃない)で、elixirやってみた。
練習問題やってみた。

練習問題

Livebookで、グローバルIPから緯度経度を求めて、地図表示せよ。

セットアップ

Mix.install([
  {:req, "~> 0.3.0"},
  {:floki, "~> 0.35.2"},
  {:kino_maplibre, "~> 0.1.10"}
])

サンプルコード

html = Req.get!("http://checkip.dyndns.org").body

ip_address =
  html
  |> Floki.parse_document!()
  |> Floki.find("body")
  |> Floki.text()
  |> String.replace("Current IP Address:", "")
  |> String.trim()

ip_info = Req.get!("https://ipinfo.io/#{ip_address}/geo").body

coords =
  ip_info
  |> Map.get("loc")
  |> String.split(",")
  |> Enum.map(fn x ->
    {value, _} = Float.parse(x)
    value
  end)
  |> Enum.reverse()
  |> List.to_tuple()

MapLibre.new(style: :street, center: coords, zoom: 10)

以上。

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