wow*_*mer 8 google-maps google-maps-api-3 google-maps-markers google-places-api
我使用API v3设置了Google Map.地图上有许多带有信息框的标记.我希望在地图外部设置一个搜索框,供用户输入地址,然后根据距离(例如半径搜索)返回最近的标记.
从API文档中我认为我需要使用Places服务.谁能指出我正确的方向?
geo*_*zip 20
要使用API执行半径搜索,请使用几何库 google.maps.geometry.spherical.computeDistanceBetween方法计算每个标记与地址的地理编码结果之间的距离.如果该距离小于请求的半径,则显示标记,否则将其隐藏.
代码假定:
google.maps.Map对象名为map
function codeAddress() {
var address = document.getElementById('address').value;
var radius = parseInt(document.getElementById('radius').value, 10)*1000;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
if (circle) circle.setMap(null);
circle = new google.maps.Circle({center:marker.getPosition(),
radius: radius,
fillOpacity: 0.35,
fillColor: "#FF0000",
map: map});
var bounds = new google.maps.LatLngBounds();
for (var i=0; i<gmarkers.length;i++) {
if (google.maps.geometry.spherical.computeDistanceBetween(gmarkers[i].getPosition(),marker.getPosition()) < radius) {
bounds.extend(gmarkers[i].getPosition())
gmarkers[i].setMap(map);
} else {
gmarkers[i].setMap(null);
}
}
map.fitBounds(bounds);
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
Run Code Online (Sandbox Code Playgroud) 归档时间: |
|
查看次数: |
30262 次 |
最近记录: |