谷歌地图切换层开始

时间:2014-12-10 02:13:14

标签: javascript html google-maps-api-3 toggle

http://deepfrogphoto.com/Brett-Pelletier-Photography/Links/maps/wasatch/index07.html

您好希望这个网站上的所有专家都能提出一个简单的问题...我上面的页面工作正常,但我真的很喜欢它,如果黑色和白色地图kml图层将开始强制用户切换它。我有很多关于这张地图的事情,我很遗憾所有的代码。如果您转到我的站点并单击图层按钮,则第一个选项是BW MAP切换。我只想让页面加载BW MAP kml图层。我已经尝试了我在这个网站上找到的几个选项,但仍然只是试错而没有运气....我认为这是你可能需要帮助的大部分代码......谢谢。

编辑 - 可能更容易“不接受”地理定位,因此您可以看到我所处的位置。地图将在没有启用的区域加载....

var map, GeoMarker;

function HomeControl(controlDiv, map) {
controlDiv.style.padding = '5px';
var controlUI = document.createElement('div');
controlUI.style.backgroundColor = 'white';
controlUI.style.borderStyle = 'solid';
controlUI.style.borderWidth = '1px';
controlUI.style.cursor = 'pointer';
controlUI.style.textAlign = 'center';
controlUI.title = 'Click to set the map to GPS Center';
controlDiv.appendChild(controlUI);
var controlText = document.createElement('div');
controlText.style.fontFamily = 'Arial,sans-serif';
controlText.style.fontSize = '11px';
controlText.style.paddingLeft = '4px';
controlText.style.paddingRight = '4px';
controlText.innerHTML = '<b>Home</b>';
controlUI.appendChild(controlText);

google.maps.event.addDomListener(controlUI, 'click', function() {
recenterMapOnGeoLoc()
});
}

function toggleLayer(this_layer) {
if (this_layer.getMap()) {
this_layer.setMap(null)
} else {
this_layer.setMap(map);
}
}

function recenterMapOnGeoLoc() {
map.setCenter(GeoMarker.getPosition());
}

function initialize() {
var mapDiv = document.getElementById('map_canvas');
var mapOptions = {
zoom: 20,
center: new google.maps.LatLng(40.563855, -111.675426),
mapTypeId: google.maps.MapTypeId.TERRAIN
};

map = new google.maps.Map(mapDiv, mapOptions);

GeoMarker = new GeolocationMarker();
GeoMarker.setCircleOptions({
fillColor: '#EBF4FA'
});

google.maps.event.addListenerOnce(GeoMarker, 'position_changed', function() {
map.setCenter(this.getPosition());
map.fitBounds(this.getBounds());
});

google.maps.event.addListener(GeoMarker, 'geolocation_error', function(e) {
alert('There was an error obtaining your position. Message: ' + e.message);
});

GeoMarker.setMap(map);

trafficLayer = new google.maps.TrafficLayer();
trafficLayer.setMap(map);

layer1 = new google.maps.FusionTablesLayer({
map: map,
heatmap: {
enabled: false
},
query: {
select: "skilines",
from: "1R5pCEyNN74N8Dt9MkfNXA6A9D1HbQESzOR1fYFa7",
 where: ""
 },
options: {
styleId: 2,
templateId: 2
}
});


layer2 = new google.maps.FusionTablesLayer({
map: map,
heatmap: {
enabled: false
},
query: {
from: "19iu58FDFcBIZZ-wU1iZcf89AI5ABDJ7YTv355su5",
where: ""
},
options: {
styleId: 2,
templateId: 2
}
});

layer3 = new google.maps.FusionTablesLayer({
map: map,
heatmap: {
enabled: false
},
query: {
select: "summerhiking",
from: "1t1XNnG7J7Zu1p5mIUpm6qIGVYwzhkCgPy_je0IKr",
where: ""
},
options: {
styleId: 2,
templateId: 2
}
});


layer5 = new google.maps.KmlLayer('http://deepfrogphoto.com/Brett-Pelletier-            Photography/Links/maps/kml/topobwkml.kml',
              {
                  suppressInfoWindows: true,
                  map: map,
                  preserveViewport: true
              });  

                                // Create the DIV to hold the control and
  // call the HomeControl() constructor passing
  // in this DIV.
  var homeControlDiv = document.createElement('div');
  var homeControl = new HomeControl(homeControlDiv, map);

  homeControlDiv.index = 1;
  map.controls[google.maps.ControlPosition.TOP_RIGHT].push(homeControlDiv);     

}

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

if (!navigator.geolocation) {
alert('Your browser does not support geolocation');
}

  //////////////And the menu code is....

  <li><a><input checked="checked" id="show_hide_layer5" onchange="toggleLayer(layer5)"      type="checkbox" value="ON" /> B+W Map</a> </li>
            <li><a><input checked="checked" id="show_hide_layer1" onchange="toggleLayer(layer1)"   type="checkbox" value="ON" /> Backcountry Ski Lines</a> </li>
            <li><a><input checked="checked" id="show_hide_layer2" onchange="toggleLayer(layer2)" type="checkbox" value="ON" /> Resort Lifts</a> </li>
                            <li><a><input checked="checked" id="show_hide_layer3" onchange="toggleLayer(layer3)" type="checkbox" value="ON" /> Summer Trails</a> </li>
                            <li><a><input checked="checked" id="show_hide_layer4" onchange="toggleLayer(layer4)" type="checkbox" value="ON" /> Snow Stations</a> </li>
                            <li><a><input checked="checked" id="show_hide_layer4" onchange="toggleLayer(trafficLayer)" type="checkbox" value="ON" /> Traffic</a> </li>"

非常感谢任何帮助......谢谢!!

1 个答案:

答案 0 :(得分:0)

如果您在创建图层时不想显示图层,请不要将图层添加到地图中(删除{map: map}图标(并且不要将复选框设置为& #34;检查&#34;。)

&#13;
&#13;
var map, GeoMarker, layer1, layer2, layer3, layer4, layer5;

function HomeControl(controlDiv, map) {
  controlDiv.style.padding = '5px';
  var controlUI = document.createElement('div');
  controlUI.style.backgroundColor = 'white';
  controlUI.style.borderStyle = 'solid';
  controlUI.style.borderWidth = '1px';
  controlUI.style.cursor = 'pointer';
  controlUI.style.textAlign = 'center';
  controlUI.title = 'Click to set the map to GPS Center';
  controlDiv.appendChild(controlUI);
  var controlText = document.createElement('div');
  controlText.style.fontFamily = 'Arial,sans-serif';
  controlText.style.fontSize = '11px';
  controlText.style.paddingLeft = '4px';
  controlText.style.paddingRight = '4px';
  controlText.innerHTML = '<b>Home</b>';
  controlUI.appendChild(controlText);

  google.maps.event.addDomListener(controlUI, 'click', function() {
    recenterMapOnGeoLoc()
  });
}

function toggleLayer(this_layer) {
  if (this_layer.getMap()) {
    this_layer.setMap(null)
  } else {
    this_layer.setMap(map);
  }
}

function recenterMapOnGeoLoc() {
  map.setCenter(GeoMarker.getPosition());
}

function initialize() {
  var mapDiv = document.getElementById('map_canvas');
  var mapOptions = {
    zoom: 20,
    center: new google.maps.LatLng(40.563855, -111.675426),
    mapTypeId: google.maps.MapTypeId.TERRAIN
  };

  map = new google.maps.Map(mapDiv, mapOptions);

  GeoMarker = new GeolocationMarker();
  GeoMarker.setCircleOptions({
    fillColor: '#EBF4FA'
  });

  google.maps.event.addListenerOnce(GeoMarker, 'position_changed', function() {
    map.setCenter(this.getPosition());
    map.fitBounds(this.getBounds());
  });

  google.maps.event.addListener(GeoMarker, 'geolocation_error', function(e) {
    alert('There was an error obtaining your position. Message: ' + e.message);
  });

  GeoMarker.setMap(map);

  trafficLayer = new google.maps.TrafficLayer();
//  trafficLayer.setMap(map);

  layer1 = new google.maps.FusionTablesLayer({
//    map: map,
    heatmap: {
      enabled: false
    },
    query: {
      select: "skilines",
      from: "1R5pCEyNN74N8Dt9MkfNXA6A9D1HbQESzOR1fYFa7",
      where: ""
    },
    options: {
      styleId: 2,
      templateId: 2
    }
  });


  layer2 = new google.maps.FusionTablesLayer({
//    map: map,
    heatmap: {
      enabled: false
    },
    query: {
      from: "19iu58FDFcBIZZ-wU1iZcf89AI5ABDJ7YTv355su5",
      where: ""
    },
    options: {
      styleId: 2,
      templateId: 2
    }
  });

  layer3 = new google.maps.FusionTablesLayer({
//    map: map,
    heatmap: {
      enabled: false
    },
    query: {
      select: "summerhiking",
      from: "1t1XNnG7J7Zu1p5mIUpm6qIGVYwzhkCgPy_je0IKr",
      where: ""
    },
    options: {
      styleId: 2,
      templateId: 2
    }
  });


  layer5 = new google.maps.KmlLayer('http://deepfrogphoto.com/Brett-Pelletier-            Photography/Links/maps/kml/topobwkml.kml', {
    suppressInfoWindows: true,
//    map: map,
    preserveViewport: true
  });

  // Create the DIV to hold the control and
  // call the HomeControl() constructor passing
  // in this DIV.
  var homeControlDiv = document.createElement('div');
  var homeControl = new HomeControl(homeControlDiv, map);

  homeControlDiv.index = 1;
  map.controls[google.maps.ControlPosition.TOP_RIGHT].push(homeControlDiv);

}

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

if (!navigator.geolocation) {
  alert('Your browser does not support geolocation');
}
&#13;
<script src="https://maps.googleapis.com/maps/api/js"></script>
<script src="https://cdn.rawgit.com/googlemaps/v3-utility-library/master/geolocationmarker/src/geolocationmarker.js"></script>
<li>
  <a>
    <input id="show_hide_layer5" onchange="toggleLayer(layer5)" type="checkbox" value="ON" />B+W Map</a>
</li>
<li>
  <a>
    <input id="show_hide_layer1" onchange="toggleLayer(layer1)" type="checkbox" value="ON" />Backcountry Ski Lines</a>
</li>
<li>
  <a>
    <input id="show_hide_layer2" onchange="toggleLayer(layer2)" type="checkbox" value="ON" />Resort Lifts</a>
</li>
<li>
  <a>
    <input id="show_hide_layer3" onchange="toggleLayer(layer3)" type="checkbox" value="ON" />Summer Trails</a>
</li>
<li>
  <a>
    <input id="show_hide_layer4" onchange="toggleLayer(layer4)" type="checkbox" value="ON" />Snow Stations</a>
</li>
<li>
  <a>
    <input id="show_hide_layer4" onchange="toggleLayer(trafficLayer)" type="checkbox" value="ON" />Traffic</a>
</li>
<div id="map_canvas" style="height:500px; width:500px;"></div>
&#13;
&#13;
&#13;