从选择菜单中选择新位置时,如何从谷歌地图中删除图钉?



我创建了一个带有下拉列表的简单地图,允许用户选择大学校园位置。现在,我正在尝试在选择新位置时清除以前的选择。对此的任何建议都很棒。

我知道必须有更好的方法来做到这一点,而不是创建多个变量和函数,但它只需要在这一点上起作用。

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" />
<link rel="stylesheet" href="styles.css" />
<script src="https://maps.googleapis.com/maps/api/js?key=
AIzaSyDCnqMp9UXHgRIu0A9vWtn2MFiO1Ze-sq8
&callback=initMap"></script>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
<body>
<header>
  <div class="ui-body ui-body-a ui-corner-all">
<h3>BCIT Campuses</h3>
      <p>This app is designed to show you the various BCIT Campus locations</p>
</div>
</header>
<form>
<div class="ui-field-contain">
    <label for="select-native-1">Select a campus</label>
    <select id="location" name="location">
<option disabled selected> -- select a location -- </option>
        <option value="DTN" onClick="downtownPin()">Downtown Campus</option>
        <option id="BBY" name="burnaby" value="BBY" onClick="burnabyPin()">Burnaby Campus</option>
<option id="ANN" value="ANN" onClick="annacisPin()">Annacis Campus</option>  
</select>
</div>
</form>
<div data-role="page" id="map-page" data-url="map-page">
<div data-role="header" data-theme="a">
<h1>Maps</h1>
</div>
<div role="main" class="ui-content" id="map-canvas">
<!-- map loads here... -->
</div>
</div>
<footer>
 <div class="ui-body ui-body-a ui-corner-all">
<h4>Wade Barrie</h4>
  
</div>
</footer>
</body>
<script>
/*
* Google Maps documentation: http://code.google.com/apis/maps/documentation/javascript/basics.html
* Geolocation documentation: http://dev.w3.org/geo/api/spec-source.html
*/
var map;
function myMap() {
var mapCanvas = document.getElementById("map-canvas");
var mapOptions = {
center: new google.maps.LatLng(49.2595139, -123.1133546),
zoom: 10
};
map = new google.maps.Map(mapCanvas, mapOptions);
}
var myLatlng = new google.maps.LatLng(49.283333, -123.115556);
var downtownCampus = new google.maps.LatLng(49.283333, -123.115556);
var annacisCampus = new google.maps.LatLng(49.1636275, -122.9702631);
var burnabyCampus = new google.maps.LatLng(49.2505981, -123.0020511);
var mapOptions = {
zoom: 10,
center: myLatlng
}
var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
var downtownCampus = new google.maps.Marker({
position: downtownCampus,
title: "Downtown Campus"
});
var annacisCampus = new google.maps.Marker({
position: annacisCampus,
title: "Annacis Island"
});
var burnabyCampus = new google.maps.Marker({
position: burnabyCampus,
title: "Burnaby Campus"
});
// functions that actually place the pin on the map when selected
function annacisPin(reload) {
annacisCampus.setMap(map);
}
function downtownPin() {
downtownCampus.setMap(map);
}
function burnabyPin() {
burnabyCampus.setMap(map);
}
</script>
</html>

您可以使用yourmarker.setMap(null)从地图中删除标记,然后再显示下一个标记。 setMap(map( 向地图添加一个标记。 setMap(null( 将其删除。

相关内容

  • 没有找到相关文章