【发布时间】:2014-02-12 08:19:07
【问题描述】:
我想创建一个页面,用户输入我一个地方,我搜索这个地方,我搜索我的数据库中属于那个地方的所有对象,我还搜索所有那些在那个地方一英里内的人.我已经创建了允许我使用以下代码查找具有自动完成功能的地点并在地图上指向的结构:
function initialize() {
var myCoordsLenght = 6;
var mapOptions = {
center: new google.maps.LatLng(46.018151,8.956521),
zoom: 17
};
var map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
var input = /** @type {HTMLInputElement} */(
document.getElementById('pac-input'));
var autocomplete = new google.maps.places.Autocomplete(input);
autocomplete.bindTo('bounds', map);
var infowindow = new google.maps.InfoWindow();
var marker = new google.maps.Marker({
map: map,
draggable: true
});
var circle = new google.maps.Circle({
map: map,
radius: 16093, // 10 miles in metres
fillColor: '#AA0000'
});
circle.bindTo('center', marker, 'position');
google.maps.event.addListener(marker, 'dragend', function(evt){
document.getElementById('latitude').value= evt.latLng.lat().toFixed(myCoordsLenght);
document.getElementById('longitude').value = evt.latLng.lng().toFixed(myCoordsLenght);
});
google.maps.event.addListener(autocomplete, 'place_changed', function() {
infowindow.close();
marker.setVisible(false);
var place = autocomplete.getPlace();
if (!place.geometry) {
return;
}
// If the place has a geometry, then present it on a map.
if (place.geometry.viewport) {
map.fitBounds(place.geometry.viewport);
} else {
map.setCenter(place.geometry.location);
map.setZoom(17); // Why 17? Because it looks good.
}
marker.setIcon(/** @type {google.maps.Icon} */({
url: place.icon,
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(35, 35),
}));
marker.setPosition(place.geometry.location);
marker.setVisible(true);
document.getElementById('latitude').value= place.geometry.location.lat();
document.getElementById('longitude').value = place.geometry.location.lng();
var address = '';
if (place.address_components) {
address = [
(place.address_components[0] && place.address_components[0].short_name || ''),
(place.address_components[1] && place.address_components[1].short_name || ''),
(place.address_components[2] && place.address_components[2].short_name || '')
].join(' ');
}
infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address);
infowindow.open(map, marker);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<input id="pac-input" class="controls" type="text"
placeholder="Enter a location">
<div id="type-selector" class="controls">
<label for="latitude">Latitude:</label>
<input id="latitude" type="text" value="" />
<label for="longitude">Longitude:</label>
<input id="longitude" type="text" value="" />
</div>
<div id="map-canvas" style="width: 500px; height: 400px;"></div>
</body>
</html>
但是在这一点上,我该如何运行一个查询来返回我想要的对象并更新地图?
谢谢大家
【问题讨论】:
-
请定义搜索我的数据库中属于那个地方的所有对象。要查询您的数据库以查找特定范围内的地点,请在此处查看我的答案stackoverflow.com/a/21043061/1238965 您的问题还应该使用您的后端语言和您使用的数据库进行标记。
标签: ajax google-maps search google-maps-api-3