Ale*_*nel 0 jquery google-maps google-maps-api-3 google-places-api
我正在使用自动填充进行地图搜索,一旦选择了新地址,当前标记将被删除并替换为用户搜索的标记.要做到这一点,我使用的是searchBox,但有些结果会显示在自动完成功能上,而这些功能无法正常工作,例如"shopping"或"audi"会为地图上的每个地方结果添加一个标记.我试图使这些结果只过滤到地址,但似乎无法使其工作.我一直看到文档中的类型:地址属性,但似乎无法弄清楚在这种情况下它会发生什么.真的很感激任何帮助!! 这是我的代码:
function initAutocomplete() {
var map = new google.maps.Map(document.getElementById('gmap_canvas'), {
zoom: 14,
center: new google.maps.LatLng(39.711383, -105.018230),
mapTypeId: google.maps.MapTypeId.ROADMAP,
zoomControl: true,
streetViewControl: true,
mapTypeControl: false
});
var myLatLng = {lat: 39.711383, lng: -105.018230};
var icon = {
url: "../../../images/gold-delivery-128.png",
size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(64, 64)
};
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
icon: icon
});
// Create the search box and link it to the UI element.
var input = document.getElementById('locationTextField');
var searchBox = new google.maps.places.SearchBox(input);
// Bias the SearchBox results towards current map's viewport.
map.addListener('bounds_changed', function() {
searchBox.setBounds(map.getBounds());
});
var markers = [];
// Listen for the event fired when the user selects a prediction and retrieve
// more details for that place.
searchBox.addListener('places_changed', function() {
var places = searchBox.getPlaces();
if (places.length == 0) {
return;
}
// Clear out the old markers.
markers.forEach(function(marker) {
marker.setMap(null);
});
markers = [];
marker.setVisible(false);
// For each place, get the icon, name and location.
var bounds = new google.maps.LatLngBounds();
places.forEach(function(place) {
var icon = {
url: "../../../images/gold-delivery-128.png",
size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(64, 64)
};
// Create a marker for each place.
markers.push(new google.maps.Marker({
map: map,
icon: icon,
title: place.name,
position: place.geometry.location
}));
if (place.geometry.viewport) {
// Only geocodes have viewport.
bounds.union(place.geometry.viewport);
} else {
bounds.extend(place.geometry.location);
}
});
map.fitBounds(bounds);
});
}
Run Code Online (Sandbox Code Playgroud)
SearchBox不允许任何过滤(它唯一的选项是a google.maps.LatLngBounds
).另一个选项是Places Autocomplete,它接受一个type
选项.您可以看到其中一个是否支持您的用例(可能geocode
或establishment
将起作用).
归档时间: |
|
查看次数: |
2969 次 |
最近记录: |