因此,我有一個谷歌地圖,從JSON文件中提取約185個公司地址和信息我已經擁有了所有工作,但我想要做的是在您過濾結果「勾選」單選按鈕或從選擇框中選擇一個選項。谷歌地圖 - 基於JavaScript對象的篩選結果
這裏是一個工作plunker - https://plnkr.co/edit/jHCuVVhGDLwgjNw4bcLr
這裏是我的地圖代碼:
var map;
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(58, 16),
zoom: 2,
mapTypeId: google.maps.MapTypeId.TERRAIN
};
map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
}
var seg = {
1:'invesCastProd',
2:'forged',
3: 'airframe',
5: 'corp',
6: 'structurals'
}
var comp = {
1:'structurals',
2:'airfoils',
3: 'airfoils',
4: 'wyman',
5: 'energy',
6: 'strucComp',
7: 'mechHard',
8: 'engineProd',
9: 'corp',
10: 'aero',
12: 'timet',
13: 'specMetals'
}
$.getJSON("locations.json", function(json1) {
$.each(json1, function(key, data) {
var latLng = new google.maps.LatLng(data.latitude, data.longitude);
// Creating a marker and putting it on the map
var marker = new google.maps.Marker({
position: latLng
});
marker.setMap(map);
var infoWindow = new google.maps.InfoWindow();
google.maps.event.addListener(marker, 'click', function() {
if (infoWindow) {infoWindow.close();}
infoWindow = new google.maps.InfoWindow({
content: "<h5>" + data.display_name + "</h5>" +
"<div>" + data.street+ "</div>" +
"<div>" + data.city + ", " + data.state + " " + data.postal_code + "</div>" +
"<div><strong>" + seg[data.segment_id] + "</strong></div>" +
"<div><strong>" + comp[data.component_id] + "</strong></div>" +
"<div class='mapPhoneNum'>" + data.telephone + "</div>" +
"<a href=" + data.web_url + ">Website</a>"
});
infoWindow.open(map, marker);
map.setZoom(15);
map.panTo(this.getPosition());
});
//############################################
// I don't know what to add here
//############################################
filterMarkers = function(){
}
});
});
// Init map
initialize();
這裏是HTML:
<div class="mapWrap">
<div id="map-canvas"></div>
<div class="investCast">
<select id="pccLoc" onchange="filterMarkers(this.value);">
<option selected="selected" disabled="disabled" value="">Please select category</option>
<option class="optGroup" value="corp">PCC Corporate</option>
<option class="optGroup" value="invesCastProd">Investment Cast Products</option>
<option class="optChild" value="structurals"> - PCC Structurals</option>
<option class="optChild" value="airfoils"> - PCC Airfoils</option>
<option class="optGroup" value="forged">Forged Products</option>
<option class="optChild" value="wyman"> - Wyman-Gordon</option>
<option class="optChild" value="energy"> - PCC Energy Group</option>
<option class="optChild" value="timet"> - Titanium Metals Corp. (TIMET)</option>
<option class="optChild" value="specMetals"> - Special Metals Corp. (SMC)</option>
<option class="optGroup" value="airProd">Airframe Products</option>
<option class="optChild" value="fasteners"> - PCC Fasteners</option>
<option class="optChild" value="aero"> - PCC Aerostructures</option>
</select>
</div>
</div>
<div class="mapSelWrap">
<div class="corpSel optGroup"><input onclick="filterMarkers(this.value);" type="radio" name="loc" value="corp" /> PCC Corporate Headquarters</div>
<div class="selInnerWrap">
<div class="selInner">
<div class="optGroup"><input onclick="filterMarkers(this.value);" type="radio" name="loc" value="invesCastProd" /> Investment Cast Products</div>
<div class="optChild"><input onclick="filterMarkers(this.value);" type="radio" name="loc" value="structurals" /> PCC Structurals</div>
<div class="optChild"><input onclick="filterMarkers(this.value);" type="radio" name="loc" value="airfoils" /> PCC Airfoils</div>
</div>
</div>
<div class="selInnerWrap">
<div class="selInner">
<div class="optGroup"><input onclick="filterMarkers(this.value);" type="radio" name="loc" value="forged" /> Forged Products</div>
<div class="optChild"><input onclick="filterMarkers(this.value);" type="radio" name="loc" value="wyman" /> Wyman-Gordon</div>
<div class="optChild"><input onclick="filterMarkers(this.value);" type="radio" name="loc" value="energy" /> PCC Energy Group</div>
<div class="optChild"><input onclick="filterMarkers(this.value);" type="radio" name="loc" value="timet" /> Titanium Metals Corp.</div>
<div class="optChild"><input onclick="filterMarkers(this.value);" type="radio" name="loc" value="specMetals" /> Special Metals Corp.</div>
</div>
</div>
<div class="selInnerWrap">
<div class="selInner">
<div class="optGroup"><input onclick="filterMarkers(this.value);" type="radio" name="loc" value="airframe" /> Airframe Products</div>
<div class="optChild"><input onclick="filterMarkers(this.value);" type="radio" name="loc" value="fasteners" /> PCC Fasteners</div>
<div class="optChild"><input onclick="filterMarkers(this.value);" type="radio" name="loc" value="aero" /> PCC Aerostructures</div>
</div>
</div>
</div>
我所試圖做的是建立一個「 filterMarkers「功能,當你選擇和選擇或者勾選一個單選按鈕來過濾基於segment_id和component_id的引腳(這只是你可以看到的數字e的JSON文件)。你也可以在我的plunk中看到(https://plnkr.co/edit/jHCuVVhGDLwgjNw4bcLr)JavaScript文件中,我用我的「過濾器」創建了兩個對象(var = seg {},var = comp {}),我已經匹配了JSON文件中的數字。它的工作原理是,當你點擊一個pin並彈出信息窗口時,粗體文本是與位置關聯的過濾器。而且我還將相應的值設置爲HTML元素中的過濾器。
那麼如何基於對象變量過濾這些結果?我非常喜歡這個想法,而我只是停留在過濾部分。
我只是不真正瞭解如何加入到過濾功能:
filterMarkers = function(){
}
感謝您的幫助!
HI,你可以和我一起在[分叉Plunker(https://plnkr.co/edit/dACBG6uwIZ82n12GDjm1?p=gitter)? –
@LouysPatriceBessette我現在在那裏 – agon024
我不習慣它...你不能跳進Plunker聊天嗎?我想我是在。 –