我正在尝试找出一种方法来获取以下用户输入:
- 邮政编码
- 距离(例如:1、5、10)[英里]
并查询包含以下内容的列表:
- 地点名称
- 邮政编码
- 纬度
- 经度
筛选后的列表应仅返回输入邮政编码半径 (x) 英里内的位置并计算每个位置的距离(例如:1.2 英里、0.5 英里等)。有什么建议吗?
提前致谢!
最佳答案
我们使用 Google Geocoding API在客户端将邮政编码转换为纬度和经度。在 AJAX 请求中,我们将纬度、经度和距离发布到服务器代码。服务器使用纬度/经度和半径计算偏移量,计算纬度和经度的起点和终点,然后搜索匹配的记录。
if (!ValidaZipCode(zipCode)) {
$("#searchZiperror").show();
$(".driverZipcode").removeClass("wouterror");
$(".driverZipcode").addClass("driverZipcodeError");
return false;
}
else {
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': zipCode }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var currentlatlng = results[0].geometry.location;
if (currentlatlng) {
var lat = currentlatlng.lat();
var lng = currentlatlng.lng();
var model = {
address: zipCode,
latitude: lat,
longitude: lng,
radius: radiusValue
};
$.ajax({
url: '@Url.Action("UpdateRadius", "MyController")',
contentType: 'application/json; charset=utf-8',
type: 'POST',
dataType: 'html',
data: JSON.stringify(model)
})
.success(function (result) {
// Use the result for appropriate action
})
.error(function (xhr, status) {
// Use the status for appropriate action
});
}
}
关于c# - 按特定半径内的邮政编码和过滤器列表搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29791713/