因此,我目前将 Fusion 表设置为带有 KML 区域的地图。我也有按地址搜索。我需要能够输入地址、搜索并确定该点所在的“区域”。这可能吗?提前致谢。
问问题
1327 次
1 回答
3
你可以用一点 JavaScript 代码来做到这一点。首先,听起来您的搜索框正在工作。您需要对输入到地址搜索中的地址进行地理编码。然后,您可以使用结果的纬度/经度坐标执行相交查询,以查找 Fusion Table 中位于输入地址非常小的半径(例如 0.0001 米)内的所有要素。下面的示例代码:
<html>
<head>
<script type="text/javascript"
src="http://maps.google.com/maps/api/js?v=3.2&sensor=false®ion=US">
</script>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
var map, layer;
var geocoder = new google.maps.Geocoder();
var tableid = 297050;
google.load('visualization', '1');
function initialize() {
var options = {
center: new google.maps.LatLng(37.5,-122.23),
zoom: 10,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map_canvas'), options);
layer = new google.maps.FusionTablesLayer({
query: {
select: "'Delivery Zone'",
from: tableid
},
map: map
});
window.onkeypress = enterSubmit;
}
function enterSubmit() {
if(event.keyCode==13) {
geocode();
}
}
function geocode() {
geocoder.geocode({address: document.getElementById('address').value }, findStore);
}
function findStore(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var coordinate = results[0].geometry.location;
marker = new google.maps.Marker({
map: map,
layer: layer,
animation: google.maps.Animation.DROP,
position: coordinate
});
var queryText = encodeURIComponent("SELECT 'Store Name' FROM " + tableid +
" WHERE ST_INTERSECTS('Delivery Zone', CIRCLE(LATLNG(" +
coordinate.lat() + "," + coordinate.lng() + "), 0.001))");
var query = new google.visualization.Query(
'http://www.google.com/fusiontables/gvizdata?tq=' + queryText);
query.send(showStoreName);
}
}
function showStoreName(response) {
if(response.getDataTable().getNumberOfRows()) {
var name = response.getDataTable().getValue(0, 0);
alert('Store name: ' + name);
}
}
</script>
</head>
<body onload="initialize()">
<input type="text" value="Palo Alto, CA" id="address">
<input type="button" onclick="geocode()" value="Go">
<div id="map_canvas" style="width:940; height:800"></div>
</body>
</html>
请注意,如果圆与 2 个多边形相交,您可能会得到 2 个结果,或者您可能会得到误报,因为半径不是 0。
于 2012-02-01T22:54:47.750 回答