gpt4 book ai didi

javascript - 如何使用 knockout.js 将复选框绑定(bind)到 Google map 标记?

转载 作者:行者123 更新时间:2023-12-03 09:14:09 24 4
gpt4 key购买 nike

我正在创建一个应用程序,用于呈现城市的 Google map ,并带有附近位置的位置标记。我通过向 google.maps.places.PlacesService API 请求 800 单位半径内的位置来呈现这些位置。此外,我还有许多代表地点类型的复选框(例如公园、博物馆等)。当选中或取消选中这些框时,我希望 map 能够动态更新并显示或隐藏相应的 map 标记。我正在使用 Knockout.js 框架来帮助实现。

我的复选框是 knockout 可观察的,我已经成功创建了一个在 html 中动态更改的复选框数组。但是,即使数组发生变化, map 本身也不会发生变化,即使我已将 types 变量设置为存储复选框选择结果的位置 (data.Locations.arrLocations)。在这种情况下我该如何获得正确的绑定(bind)?

我在下面包含了我的 index.html 和 app.js 文件:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">

<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
}
</style>
</head>
<body>
<!-- This is a *view* - HTML markup that defines the appearance of your UI -->
<div id='searchBar'>
<p>Search: <strong data-bind="text: location"></strong></p>
<p><input type='checkbox' data-bind='checked: Locations' value='gym'> Gyms </p>
<p> <input type='checkbox' data-bind='checked: Locations' value='park'>Parks </p>
<p> <input type='checkbox' data-bind='checked: Locations' value='store'> Stores </p>
<p> <input type='checkbox' data-bind='checked: Locations' value='museum'> Museums </p>
<p> <input type='checkbox' data-bind='checked: Locations' value='zoo'> Zoos </p>

<p>Search: <input data-bind="value: location" /></p>
<div data-bind='text: ko.toJSON($data.Locations)'></div>


</div>


<div id='map'></div>
<div id="map"></div>
<script src="https://maps.googleapis.com/maps/api/js?signed_in=true&libraries=places&callback=initMap" async defer></script>


<script type='text/javascript' src='https://cdnjs.cloudflare.com/ajax/libs/knockout/3.3.0/knockout-min.js'></script>
<script type='text/javascript' src='js/app.js'></script>
</body>
</html>

app.js:

var data = {
'Locations' : {
'arrLocations': ['park', 'gym']
}
};


function AppViewModel() {
this.location = ko.observable("Barcelona, Spain");
this.Locations = ko.observableArray(data.Locations.arrLocations);





}


var map;
var infowindow;

function initMap() {
var barcelona = {lat: 41.383, lng: 2.183};

map = new google.maps.Map(document.getElementById('map'), {
center: barcelona,
zoom: 15
});

infowindow = new google.maps.InfoWindow();

var service = new google.maps.places.PlacesService(map);
service.nearbySearch({
location: barcelona,
radius: 800,
types: data.Locations.arrLocations,
}, callback);
}

function callback(results, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
createMarker(results[i]);
}
}
}

function createMarker(place) {
var placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location
});

google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(place.name);
infowindow.open(map, this);
});
}


// Activates knockout.js
ko.applyBindings(new AppViewModel());

最佳答案

您需要订阅位置中的更改并相应地调整您的标记。要清除不需要的标记,您需要跟踪已添加的标记。我使用 markers 数组来完成此操作。每次位置更改时,所有标记都会被删除,然后运行搜索并添加标记。

viewModel.Locations.subscribe(function (newValue) {
console.debug("Changing", newValue);
for (var i=0; i<markers.length; ++i) {
markers[i].setMap(null);
}
markers = [];
service.nearbySearch({
location: barcelona,
radius: 800,
types: newValue
}, callback);
});
viewModel.Locations.notifySubscribers();

notifySubscribers 调用会导致订阅在第一次运行时触发。

http://jsfiddle.net/53qfm5bz/1/

关于javascript - 如何使用 knockout.js 将复选框绑定(bind)到 Google map 标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32001879/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com