Help us understand the problem. What is going on with this article?

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

More than 5 years have passed since last update.

スクリーンショット 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. マップデータ表示

という感じ。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away