Leafletで背景にGoogleMapを表示するためには下記のように記述します。
index.html
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>sample</title> <link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css" /> <script src="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script> <script src="http://maps.google.com/maps/api/js?v=3.2&sensor=false"></script> <script src="http://matchingnotes.com/javascripts/leaflet-google.js"></script> <link href="stylesheet.css" rel="stylesheet" /> </head> <body> <div id="map"></div> <script src="script.js"></script> </body> </html>
stylesheet.css
html, body { width: 100%; height: 100%; } #map { width: 100%; height: 100%; }
script.js
var map = L.map('map'); var googlemap = new L.Google('ROADMAP'); map.addLayer(googlemap); map.setView([35.681382, 139.766084], 15);
index.htmlを実行すると下記のようにブラウザで表示されます。
読み込む地図の種類を変えたい時:
L.Google~の部分の記述を変更します。
GoogleMapオルソ
var googlemap = new L.Google('SATELLITE'); map.addLayer(googlemap);
GoogleMapハイブリッド
var googlemap = new L.Google('HYBRID'); map.addLayer(googlemap);
初期表示位置・縮尺を変えたい時:
map.setView~の部分の記述を変更します。
日本全体表示
map.setView([35.999887, 138.75], 5);
- 参考文献 –
bl.ocks.org