Javascript 如何在谷歌地图API上画一个圆圈并在圆圈内搜索?
在我的网页上,我已经有了一个带有搜索栏的GoogleMapsAPI(见下面的代码),用户首先会精确定位一个地址。然后用户就可以用搜索栏搜索这一点附近的企业。在搜索完成之前,我想在这个地址周围画一个圆圈,比如距离15公里。搜索应仅显示此圆圈内的结果。 如果你也能移动这个圆就好了。。。 我如何使用谷歌地图做到这一点Javascript 如何在谷歌地图API上画一个圆圈并在圆圈内搜索?,javascript,google-maps,geometry,Javascript,Google Maps,Geometry,在我的网页上,我已经有了一个带有搜索栏的GoogleMapsAPI(见下面的代码),用户首先会精确定位一个地址。然后用户就可以用搜索栏搜索这一点附近的企业。在搜索完成之前,我想在这个地址周围画一个圆圈,比如距离15公里。搜索应仅显示此圆圈内的结果。 如果你也能移动这个圆就好了。。。 我如何使用谷歌地图做到这一点 <script type="text/javascript"> var map = null; var geocoder = null;
<script type="text/javascript">
var map = null;
var geocoder = null;
function initialize() {
/* Initialize the Google Maps */
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map"));
map.setCenter(new GLatLng(50.786916, 6.101360), 16);
var customUI = map.getDefaultUI();
customUI.controls.scalecontrol = false;
map.setUI(customUI);
var options = {
onSearchCompleteCallback:function(searcher){
var resultcontent = '';
if (searcher.results && searcher.results.length > 0) {
$("#ResultGrid").clearGridData(true);
for (var i = 0; i < searcher.results.length; i++) {
var result = searcher.results[i];
// Split address-lines to get Zipcode
TempString = result.addressLines[1];
var ZipCode = TempString.split(/\b[^0-9]/);
// construct the data array
var InputData = {Firma:result.titleNoFormatting, Adresse:result.addressLines[0], Postleitzahl:ZipCode[0], Ort:result.city, Telefonnummer:result.phoneNumbers[0].number};
// Apply data to grid
jQuery("#ResultGrid").addRowData(i, InputData);
$("#Result").show("slow");
}
} else {
$("#Dialog").html("<p><span class=\"ui-icon ui-icon-info\" style=\"float:left; margin:0 7px 20px 0;\"></span>Kein Suchergebnis!</p>");
$("#Dialog").dialog("option", "title", "Hinweis:");
$("#Dialog").dialog("open");
$("#Dialog").html("Keine Ergebnisse gefunden!");
$("#Dialog").dialog("option", "title", "Hinweis:");
$("#Dialog").dialog("open");
}
}
};
localSearch = new google.maps.LocalSearch(options);
map.addControl(localSearch);
map.removeControl(GScaleControl);
geocoder = new GClientGeocoder();
$("#map").hide("fast");
$("#Result").hide("fast");
}
}
function showAddress(address, CompleteAdd) {
if (geocoder) {
geocoder.getLatLng(
address,
function(point) {
if (!point) {
$("#Dialog").html("<p><span class=\"ui-icon ui-icon-info\" style=\"float:left; margin:0 7px 20px 0;\"></span>"+address+" nicht gefunden</p>");
$("#Dialog").dialog("option", "title", "Hinweis:");
$("#Dialog").dialog("open");
} else {
map.setCenter(point, 16);
var marker = new GMarker(point);
map.addOverlay(marker);
marker.openInfoWindowHtml(CompleteAdd);
}
}
);
}
$("#map").show("fast");
}
</script>
var-map=null;
var-geocoder=null;
函数初始化(){
/*初始化谷歌地图*/
if(GBrowserIsCompatible()){
map=新的GMap2(document.getElementById(“map”);
地图设置中心(新格拉特林(50.786916,6.101360),16);
var customUI=map.getDefaultUI();
customUI.controls.scalecontrol=false;
map.setUI(customUI);
变量选项={
onSearchCompleteCallback:函数(搜索器){
var resultcontent='';
if(searcher.results&&searcher.results.length>0){
$(“#ResultGrid”).clearGridData(true);
for(var i=0;i”;
$(“#对话框”)。对话框(“选项”、“标题”、“欣维斯:”);
$(“对话框”)。对话框(“打开”);
$(“#Dialog”).html(“Keine Ergebnisse gefunden!”);
$(“#对话框”)。对话框(“选项”、“标题”、“欣维斯:”);
$(“对话框”)。对话框(“打开”);
}
}
};
localSearch=newgoogle.maps.localSearch(选项);
addControl(localSearch);
地图移除控制(GScaleControl);
geocoder=新的GClientGeocoder();
$(“地图”).hide(“快速”);
$(“#结果”)。隐藏(“快速”);
}
}
函数showAddress(地址,CompleteAdd){
if(地理编码器){
geocoder.getLatLng(
地址:,
功能(点){
如果(!点){
$(“#Dialog”).html(“”+地址+“nicht gefunden”);
$(“#对话框”)。对话框(“选项”、“标题”、“欣维斯:”);
$(“对话框”)。对话框(“打开”);
}否则{
地图设定中心(点16);
var标记=新的GMarker(点);
添加覆盖图(标记);
marker.openInfoWindowHtml(CompleteAdd);
}
}
);
}
$(“地图”)。显示(“快速”);
}
对话框文本
使用多段线绘制圆。你可以通过从0到2*PI(0..6.28)的循环来实现,其中步数定义了你的圆的“平滑度”。要绘制圆,多段线的点将是(radiusin(循环计数器)、RADIUScos(循环计数器))
您只需计算距离起点的距离即可在范围内搜索,距离=sqrt((x1-x2)^2+(y1-y2)^2),其中x1,y1是您的占有量,x2,y2是您检查的对象是否在范围内的位置。若该表达式的值小于X,则表示该对象在您要查找的范围(X)内。但这个范围的单位有点像地球上的度。要将其重新计算为公里,您需要将其乘以约67(例如,在谷歌地球上检查您所在区域的度数大约等于一公里或英里)
当然,地球并不完全是球体,但精确的函数是非常复杂的,上面的解决方案应该是可行的
编辑:要进行搜索,必须有一些数据可供搜索。假设它是一个数据库,其中保存的对象具有经度和高度值。现在,要找到从起始点X,Y范围内的对象,您必须通过计算到您的点的距离,将每个对象位置与您的X和Y进行比较,并检查距离是否小于或等于查找对象的范围。示例查询如下所示:
SELECT * FROM objects_database WHERE SQRT((googlex-$lat)*(googlex-$lat)+(googley-$lng)*(googley-$lng)) < $realthemax
必须记住,这些值取决于你们的位置,而地球并不是完美的球体,所以,这个表达式在地球的顶部或底部都不会特别明显。这种搜索方式类似于在地图上寻找完美圆中的对象(但不应该),您可以在此处看到与完美圆的差异:
谷歌地图V3有一个圆形覆盖选项,只需给它一个半径,一些样式,并将它绑定到一个标记上。如果标记是可拖动的,圆圈将跟随它。请看下面的演示:
我让它连接到一个带有onChange事件的jQueryUI滑块来设置圆的半径,并绑定到一个可移动的标记来设置位置。好的,通过从0到PI的循环绘制圆效果很好,但我不完全理解在范围内搜索的解决方案。。。也许你能举个例子吗?
SELECT * FROM objects_database WHERE SQRT((googlex-$lat)*(googlex-$lat)+(googley-$lng)*(googley-$lng)) < $realthemax
$realthemax = $themax/67.67;