LoginSignup
6
10

More than 5 years have passed since last update.

google map上にgeojsonデータを表示させる

Posted at

スクリーンショット 2014-12-13 12.42.52.png

こんな感じでgoogle map上に位置情報をプロットできるようにする。

mapData.json
{"type":"FeatureCollection",
   "features":[{"type":"Feature",
         "geometry":{"type":"Point", "coordinates":[139.6934361947,35.7065873901]},
         "properties":{"prop0":"value0"}
      },{"type":"Feature",
         "geometry":{"type":"Point", "coordinates":[139.6940198,35.65529179]},
         "properties":{"prop0":"value0"}
      },{"type":"Feature",
         "geometry":{"type":"Point", "coordinates":[139.6899114,35.64338274]},
         "properties":{"prop0":"value0"}
      }  
   ]
}

coordinatesの中身が緯度経度だが、[経度,緯度]である点に注意。さらにいえば[北緯,東経]である。
あとはhtmlを下記のように作ってやればok。

map.html
<!DOCTYPE html>
<html>
  <head>
    <title>Data Layer: Simple</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
      html, body, #map-canvas {
        height: 100%;
        margin: 0px;
        padding: 0px 
      }   
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
    <script>
var map;
function initialize() {
  // Create a simple map.
  map = new google.maps.Map(document.getElementById('map-canvas'), {
    zoom: 4,
    center: {lat: 36.3894816, lng: 139.0634281}
  });

  // Load a GeoJSON from the same server as our demo.
  //map.data.loadGeoJson('https://storage.googleapis.com/maps-devrel/google.json');
  map.data.loadGeoJson('http://xxxxx.com/mapData.json');
}

google.maps.event.addDomListener(window, 'load', initialize);

    </script>
  </head>
  <body>
    <div id="map-canvas"></div>
  </body>
</html>

実際の挙動は、
1. htmlレスポンスを受け取る
2. htmlのローディング(google mapの描画)がはじまる
3. jsonデータを取りに行く
4. マップデータ表示

という感じ。

6
10
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
6
10