var mymap = L.map("mymap").setView([38.914, 139.838], 15);
L.tileLayer('//{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
  attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(mymap);

var layers = {
  "滑り台": L.layerGroup(),
  "ブランコ": L.layerGroup(),
  "鉄棒": L.layerGroup(),
  "砂場": L.layerGroup(),
  "水道": L.layerGroup(),
  "水飲み場": L.layerGroup(),
  "ジャングルジム": L.layerGroup(),
  "シーソー": L.layerGroup(),
  "ボールリング": L.layerGroup()
};


var parks = [
  {
    name: "天王下公園",
    coords: [38.9155, 139.8375],
    yuugu: ["鉄棒", "滑り台", "ブランコ"],
    image: "images/tennoshita.jpg"
  },
  {
    name: "錦公園",
    coords: [38.9173, 139.8402],
    yuugu: ["鉄棒", "砂場", "滑り台", "ブランコ", "水飲み場"],
    image: "images/nishiki.jpg"
  },
  {
    name: "京田2丁目公園",
    coords: [38.9170, 139.8395],
    yuugu: ["水道", "ブランコ"],
    image: "images/kyoden2.jpg"
  },
  {
    name: "錦町南公園",
    coords: [38.9160, 139.8412],
    yuugu: ["砂場", "水道", "ブランコ", "ジャングルジム"],
    image: "images/nishikiminami.jpg"
  },
  {
    name: "錦町5丁目公園",
    coords: [38.9182, 139.8425],
    yuugu: ["ブランコ", "シーソー", "ボールリング"],
    image: "images/nishiki5.jpg"
  },
  {
    name: "京田4丁目公園",
    coords: [38.9150, 139.8358],
    yuugu: ["滑り台", "ブランコ"],
    image: "images/kyoden4.jpg"
  }
];

parks.forEach(function(park) {
  var popup = "<strong>" + park.name + "</strong><br>" +
              (park.image ? "<img class='park-photo' src='" + park.image + "' alt='" + park.name + "'>" : "") +
              "<br>遊具: " + park.yuugu.join(", ");
  var marker = L.marker(park.coords).bindPopup(popup);

  park.yuugu.forEach(function(y) {
    if (layers[y]) {
      marker.addTo(layers[y]);
    }
  });
});

for (var key in layers) {
  layers[key].addTo(mymap);
}

L.control.layers(null, layers).addTo(mymap);
