dayjournal memo

Total 975 articles!!

Leaflet #016 – 背景地図(GoogleMap)の表示2

Yasunori Kirimoto's avatar

Leaflet #002 – 背景地図(GoogleMap)の表示」とは別の方法を記載します。

この方法の方が描画が早い気がします。


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&amp;sensor=false"></script>
    <script src="./plugin/leaflet-plugins-master/layer/tile/Google.js"></script>
    <link href="./css/stylesheet.css" rel="stylesheet" />

</head>

<body>

    <div id="map"></div>

    <script src="./js/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);


book

Q&A