Javascript 使用PHP在Google地图上按标题搜索标记
我正在使用AJAX方法从web服务检索标记,并希望使用标记属性标题在地图上搜索它们。可能吗 这是我的函数,它返回标记:Javascript 使用PHP在Google地图上按标题搜索标记,javascript,php,google-maps,search,google-maps-api-3,Javascript,Php,Google Maps,Search,Google Maps Api 3,我正在使用AJAX方法从web服务检索标记,并希望使用标记属性标题在地图上搜索它们。可能吗 这是我的函数,它返回标记: function displayLocation(location) { var content = '<strong>' + location.name + '</strong>'; var position = new google.maps.LatLng(parseFloat(location
function displayLocation(location) {
var content = '<strong>' + location.name + '</strong>';
var position = new google.maps.LatLng(parseFloat(location.coordinate[0]), parseFloat(location.coordinate[1]));
var marker = new google.maps.Marker({
map: map,
position: position,
title: location.name,
url: 'https://www.google.com.br/#q=' + location.name,
icon: 'img/restaurant_pin.png'
});
var label = new Label({
map: map
});
label.bindTo('position', marker);
label.bindTo('text', marker, 'title');
label.bindTo('visible', marker);
label.bindTo('clickable', marker);
label.bindTo('zIndex', marker);
google.maps.event.addListener(marker, 'click', function() {
window.location.href = marker.url;
});
}
功能显示位置(位置){
var content=''+location.name+'';
var position=new google.maps.LatLng(parseFloat(location.coordinate[0]),parseFloat(location.coordinate[1]);
var marker=new google.maps.marker({
地图:地图,
职位:职位,,
标题:location.name,
网址:'https://www.google.com.br/#q='+location.name,
图标:“img/restaurant_pin.png”
});
变量标签=新标签({
地图:地图
});
标签。绑定到(“位置”,标记);
label.bindTo('text',marker,'title');
标签。绑定到(“可见”,标记);
label.bindTo('clickable',marker);
标签.bindTo('zIndex',标记);
google.maps.event.addListener(标记'click',函数(){
window.location.href=marker.url;
});
}
假设标记
是您的标记数组,您的搜索短语
是您正在搜索的内容:
for (var i=0; i<markers.length; i++) {
if (markers[i].title == yourSearchPhrase) {
// this marker's title equals your search phrase
}
}
用于(var i=0;将所有标记添加到一个数组中。在数组中循环,查看当前标记标题是否与搜索匹配。它们位于一个数组中。但我正在尝试这样做,但它不起作用。您是否有开始编写标记的想法?请参阅下面的答案。@Danilo Brizola您找到解决方案了吗?它会在m中搜索标记标题吗ap并将位置定向到搜索的标题?map.setCenter(标记[i].getPosition())
如果您在上述代码的if
条件中添加此项,则地图将在匹配标记上居中。我们可以使用自动完成建议功能执行相同的功能吗?意味着如果我在搜索框中键入,我将在下拉列表中获得建议,其中包含来自标记数组的所有数据?如果您知道任何建议,您可以向我发送链接或其他信息吗?什么Complete?Places Autocomplete用于在Google Places中搜索。如果您想在自己的数据中搜索,则需要另一种解决方案。