谷歌地图API V3:使圆半径搜索标记可点击
Google Maps API V3: Make the circle of the radius search markers clickable
我想让半径搜索函数的圆可以点击。我只想补充一点,当你点击圆圈时,地图的边界应该适合圆圈的位置。
因此,我做了以下更改,但没有成功:
- 使圆圈可点击:添加可点击:true
- 添加一个事件侦听器:google.maps.event.addListener(circle,'click',function(){map.fitBounds(circle.getBounds);})
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) {
side_bar_html = "";
map.setCenter(results[0].geometry.location);
var searchCenter = 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:searchCenter,
radius: radius,
fillOpacity: 0.35,
fillColor: "#FF0000",
clickable: true,
map: map});
var bounds = new google.maps.LatLngBounds();
var foundMarkers = 0;
for (var i=0; i<gmarkers.length;i++) {
if (google.maps.geometry.spherical.computeDistanceBetween(gmarkers[i].getPosition(),searchCenter) < radius) {
bounds.extend(gmarkers[i].getPosition())
gmarkers[i].setMap(map);
// add a line to the side_bar html
side_bar_html += '<a href="javascript:myclick(' + i + ')">' + gmarkers[i].title + '<'/a><br>';
foundMarkers++;
} else {
gmarkers[i].setMap(null);
}
}
// put the assembled side_bar_html contents into the side_bar div
document.getElementById("side_bar").innerHTML = side_bar_html;
if (foundMarkers > 0) {
map.fitBounds(bounds);
} else {
map.fitBounds(circle.getBounds());
}
// makeSidebar();
// google.maps.event.addListenerOnce(map, 'bounds_changed', makeSidebar);
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
google.maps.event.addListener(circle, 'click', function() {
map.fitBounds(circle.getBounds());
});
}
感谢您的帮助
所提供示例的主要问题是,当对象本身尚未初始化时,click
事件被附加到google.maps.Circle
对象。
解决方案是在对象初始化后附加事件处理程序:
geocoder.geocode({ 'address': address }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var side_bar_html = "";
map.setCenter(results[0].geometry.location);
var searchCenter = results[0].geometry.location;
if (circle) circle.setMap(null);
circle = new google.maps.Circle({
center: searchCenter,
radius: radius,
fillOpacity: 0.35,
fillColor: "#FF0000",
clickable: true,
map: map
});
//attach click event handler
google.maps.event.addListener(circle, 'click', function () {
map.fitBounds(circle.getBounds());
});
//remaing code goes here...
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
以下是修改后的示例
var geocoder;
var map;
var circle;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(-34.397, 150.644);
var mapOptions = {
zoom: 8,
center: latlng
}
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
}
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) {
var side_bar_html = "";
map.setCenter(results[0].geometry.location);
var searchCenter = results[0].geometry.location;
if (circle) circle.setMap(null);
circle = new google.maps.Circle({
center: searchCenter,
radius: radius,
fillOpacity: 0.35,
fillColor: "#FF0000",
clickable: true,
map: map
});
google.maps.event.addListener(circle, 'click', function () {
map.fitBounds(circle.getBounds());
document.getElementById('output').innerHTML += 'Clicked';
});
//the remaing code goes here...
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
html, body, #map-canvas {
height: 240px;
margin: 0px;
padding: 0px;
}
#panel {
position: absolute;
top: 5px;
left: 50%;
margin-left: -180px;
z-index: 5;
background-color: #fff;
padding: 5px;
border: 1px solid #999;
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry"></script>
<div id="panel">
<input id="address" type="textbox" value="Sydney, NSW">
<input id="radius" type="textbox" value="100">
<input type="button" value="Geocode" onclick="codeAddress()">
</div>
<div id="map-canvas"></div>
<pre id="output"></pre>
如果您有var作用域问题,请尝试以这种方式在函数外声明var圆
var circle;
function codeAddress() {
var address = document.getElementById('address').value;
.....
.....
并最终以另一种方式管理你的圆圈测试
//if (circle) circle.setMap(null);
相关文章:
- 如何限制谷歌地图API V3半径搜索仅限于圆圈内的标记
- YouTube API v3哈希标签搜索错误结果
- 谷歌地图 api v3 搜索多边形没有鼠标事件
- 搜索的地址与谷歌地图 api v3 上最近的现有地标之间的距离
- 谷歌地图API v3搜索框,带有可拖动的标记和输入框
- 谷歌地图 API V3 搜索 KML 文件
- 谷歌地图API v3放置搜索 - 将另一个参数传递给回调函数
- Google Maps API v3:为什么我的 Google Maps API V3 和 Google Maps 搜索
- 谷歌地图 API v3 位置搜索与 jQuery 自动完成插件
- YouTube API v3 按关键字 JavaScript 搜索
- 谷歌地图V3搜索问题
- 谷歌地图API V3:使圆半径搜索标记可点击
- 在Google Maps v3 API中按酒店名称搜索
- Google maps Places API V3自动完成搜索框中所选搜索词/地点之间的差异
- 使用Google Maps v3 API搜索MySQL数据
- Youtube V3频道搜索有结果,但不显示项目
- Google Maps API V3 -移动和“;To"标记并重新填充搜索字段
- Google Maps v3中的星型搜索算法
- 谷歌地图api v3覆盖图像与搜索栏
- Youtube数据API v3 -搜索视频使用逗号分隔的视频id