LoginSignup
3
3

More than 5 years have passed since last update.

Google Maps API でロード時にユーザーの現在地(GeoIP風)にしたい

Last updated at Posted at 2016-06-26

Google Maps API で地図ロード時に表示する場所、迷いませんかね。
そうでもないですかね。

サーバーサイドでやるなら Maxmind の GeoIP 入れたり freegeoip 使ったり(これは実態はMaxmind)が必要だけど、Google Maps API に限っては良い方法があったのでメモ。

条件としては Google Loader を使うこと。
このローダーに google.loader.ClientLocation という GeoIP ぽい値が入ってたので、それを使う。

まずはロード。headタグ内が良いと思う
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
// ※ 2016/06/25 からはAPIキーが必須に
google.load('maps', '3', {
  other_params: 'key=XXXXXXXXXXXXXXXXXXXXXXXXXX'
});
</script>
マップ表示部分
var map = new google.maps.Map(document.getElementById('map'), {
  center: new google.maps.LatLng( // ↓ここで使用
    google.loader.ClientLocation.latitude,
    google.loader.ClientLocation.longitude
  ),
  zoom: 14
});
3
3
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
3
3