查找最近的已知位置:Google Reverse Geocoding [英] Find nearest known location: Google Reverse Geocoding
问题描述
我需要使用Google地图API获取给定坐标的格式化地址。我使用 Google反向地理编码来查找位置名称。在Google地图数据库中有一个可用于位置的名称时,此工作正常。
大多数时候,给定的坐标来自远离城市边界的位置(例如在高速公路上)。该函数返回 ZERO_RESULTS
,因为地图上没有定义名称。要求是找到最近的已知地址地址。
在功能上,这听起来很不错,但从技术上讲,如何去做呢?
目前我正在找到距离这个点很少(千米)的位置,检查该位置是否有名字,并且递归地直到我得到一个名字。
个人而言,并不喜欢这种方式,原因如下:
-
无法猜测哪个方向去找到有名字的位置
-
我可能会朝一个方向走,但一个已知的地方只是在相反的方向几米。
- 在增量过程中,我可能会做得太过分,因为在15美元的地方有一个名字。我搜索10公里外的名字,再次检查
20公里,因为增值标识是10公里。<!DOCTYPE html>下面是完整的代码,它正在工作,但有上述问题。
< html xmlns =http://www.w3.org/1999/xhtml>
< head>
< title>地图测试< /标题>
< script src =https://maps.googleapis.com/maps/api/js?v=3.exp>< / script>
< / head>
< body>
< span id =spanId>正在加载...< / span>
< script>
Number.prototype.toRad = function(){
return this * Math.PI / 180;
}
Number.prototype.toDeg = function(){
return this * 180 / Math.PI;
}
google.maps.LatLng.prototype.destinationPoint = function(brng,dist){
dist = dist / 6371;
brng = brng.toRad();
var lat1 = this.lat()。toRad(),lon1 = this.lng()。toRad();
var lat2 = Math.asin(Math.sin(lat1)* Math.cos(dist)+
Math.cos(lat1)* Math.sin(dist)* Math.cos (方位角));
var lon2 = lon1 + Math.atan2(Math.sin(brng)* Math.sin(dist)*
Math.cos(lat1),
Math.cos dist) - Math.sin(lat1)*
Math.sin(lat2));
if(isNaN(lat2)|| isNaN(lon2))返回null;
return new google.maps.LatLng(lat2.toDeg(),lon2.toDeg());
}
var pointA = new google.maps.LatLng(32.6811,74.8732);
getLocation(pointA);
var distance = 0;
函数getLocation(info){
var myCenter = info; // new google.maps.LatLng(info.split(,,3)[1],info.split(,,3)[2]);
var gc = new google.maps.Geocoder();
$ b $ gc.geocode({'location':myCenter},function(results,status){
if(status == google.maps.GeocoderStatus.OK){
if (results [1]){
document.getElementById('spanId')。innerHTML = results [1] .formatted_address +','+ distance +'kms away from original point';
} else {
window.alert('找不到结果');
}
} else {
if(status =='ZERO_RESULTS')
{
var innerInMm = 10;
distance + = radiusInKm;
document.getElementById('spanId')。innerHTML ='从+距离+'千米以外'获得结果;
var pointB = pointA。 destinationPoint(90,distance);
setTimeout(function(){
的getLocation(pointB);
},2000);
}
}
});
}
< / script>
< / body>
< / html>
会真的很感谢任何人有很好的解决方案。
JSFiddle链接: https://jsfiddle.net/hbybs68q/1/
谢谢。
解决方案您需要修改
getLocation
函数。检查结果[0]不是结果[1]。if(status == google。 maps.GeocoderStatus.OK){
if(results [0]){
document.getElementById('spanId')。innerHTML = results [0] .formatted_address +','+ distance +'kms away从原点';
} else {
window.alert('找不到结果');
} else {
window.alert('Geocoder因 - '+ status'失败)
}
I am required to get the formatted address of a given coordinates using Google Maps API. I use the Google Reverse Geo coding for finding the location name. This works fine when there is a name available for the location in the Google maps database.
Most of the times, the given coordinates are from a location far off from city boundaries (on a highway for example). The function returns
ZERO_RESULTS
as there is no name defined on the map. Requirement is to find the nearest known location address to be returned.Functionally this is a nice to hear, but technically, how to go about it?
Currently I am finding the location which is few (kilo)meters away from this point, checking if the location has a name, and recursively going till i get a name.
Personally, didn't like this approach because of the following reasons:
Can't guess which direction to go to find the location which has a name
I might go in a direction but a known place is just few meters in the reverse direction.
- I might go too far during the increment, as in a place which is 15 kms away has a name. I search for the name 10 kms away and check at 20 kms again as the increment identity is 10 kms.
The following is the complete code, which is working but with the above issues.
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Map Test</title> <script type="text/javascript" src="//code.jquery.com/jquery-1.11.1.min.js"></script> <script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script> </head> <body> <span id="spanId">Loading...</span> <script> Number.prototype.toRad = function () { return this * Math.PI / 180; } Number.prototype.toDeg = function () { return this * 180 / Math.PI; } google.maps.LatLng.prototype.destinationPoint = function (brng, dist) { dist = dist / 6371; brng = brng.toRad(); var lat1 = this.lat().toRad(), lon1 = this.lng().toRad(); var lat2 = Math.asin(Math.sin(lat1) * Math.cos(dist) + Math.cos(lat1) * Math.sin(dist) * Math.cos(brng)); var lon2 = lon1 + Math.atan2(Math.sin(brng) * Math.sin(dist) * Math.cos(lat1), Math.cos(dist) - Math.sin(lat1) * Math.sin(lat2)); if (isNaN(lat2) || isNaN(lon2)) return null; return new google.maps.LatLng(lat2.toDeg(), lon2.toDeg()); } var pointA = new google.maps.LatLng(32.6811,74.8732); getLocation(pointA); var distance = 0; function getLocation(info) { var myCenter = info; //new google.maps.LatLng(info.split(",", 3)[1], info.split(",", 3)[2]); var gc = new google.maps.Geocoder(); gc.geocode({ 'location': myCenter }, function (results, status) { if (status == google.maps.GeocoderStatus.OK) { if (results[1]) { document.getElementById('spanId').innerHTML = results[1].formatted_address + ', ' + distance + ' kms away from original point' ; } else { window.alert('No results found'); } } else { if (status == 'ZERO_RESULTS' ) { var radiusInKm = 10; distance += radiusInKm; document.getElementById('spanId').innerHTML = 'Getting results from ' + distance + ' kms away'; var pointB = pointA.destinationPoint(90, distance); setTimeout(function(){ getLocation(pointB); }, 2000); } } }); } </script> </body> </html>
would really appreciate if anyone has a good solution.
JSFiddle Link: https://jsfiddle.net/hbybs68q/1/
thanks.
解决方案You need to modify the if condition inside
getLocation
function. Check for results[0] not result[1].if (status == google.maps.GeocoderStatus.OK) { if (results[0]) { document.getElementById('spanId').innerHTML = results[0].formatted_address + ', ' + distance + ' kms away from original point' ; } else { window.alert('No results found'); } } else { window.alert('Geocoder failed due to - ' + status) }
这篇关于查找最近的已知位置:Google Reverse Geocoding的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!