我有一个lat longs列表,我需要找到最接近的位置(lat,long)。
我不想使用任何公式,因为它会花费大量时间进行计算。有什么方法我可以使用谷歌API或我上传我的列表到任何谷歌帐户我刚刚用我的当前位置点击它将返回最近的一个地方和距离?
答案 0 :(得分:1)
是。首先,您必须加载几何库
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry"></script><script>
然后你可以使用谷歌地图功能google.maps.geometry.spherical.computeDistanceBetween
您将结果放入for循环或数组中;然后你返回最小的结果。
以下是您的问题的网络示例
<style>
#map {
height: 400px;
}
</style>
<div id="map"></div>
<input type="button" value="Minimal Distance" onclick="displayMinimalDistance()">
<div id="log"></div>
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry"></script><script>
var myLocations = [
{lat: 50.0,lng: 4.5},
{lat: 50.1,lng: 4.7},
{lat: 50.4,lng: 4.8},
{lat: 50.7,lng: 4.9},
{lat: 50.2,lng: 4.4},
{lat: 50.5,lng: 4.0},
{lat: 50.8,lng: 4.6},
{lat: 50.3,lng: 4.1},
{lat: 50.6,lng: 4.2},
{lat: 50.9,lng: 4.3}
];
var myLocation = {lat: 50.5,lng: 4.5};
// Google maps stuff
function initialize() {
var markers = [];
var myMarker;
var mapCenter = new google.maps.LatLng(50.5, 4.5);
var myOptions = {
zoom: 8,
center: mapCenter,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map"), myOptions);
addMarkers();
// adding the markers
function addMarkers() {
for(i in myLocations) {
markers.push(new google.maps.Marker({
position: new google.maps.LatLng(myLocations[i].lat, myLocations[i].lng),
title: i,
map: map
})
);
}
var myMarker = new google.maps.Marker({
position: new google.maps.LatLng(myLocation.lat, myLocation.lng),
title: 'You are here',
icon: {
url: 'http://www.euroheat.co.uk/images/you-are-here-icon.png',
size: new google.maps.Size(48, 48),
origin: new google.maps.Point(0,0),
anchor: new google.maps.Point(24,42)
},
map: map
});
}
}
google.maps.event.addDomListener(window, 'load', initialize);
// distance stuff
// returns an object: {i: 'key of the location', dist: 'distance'}
function getMinimalDistance(location, locations) {
var minDistance = 20000000; // and now we will look for any shorter distance
var minDistanceKey = -1;
var dist;
for(i in locations) {
dist = getDistance(location, locations[i]);
if(dist < minDistance) {
minDistance = dist;
minDistanceKey = i;
}
}
return {i: minDistanceKey, dist: minDistance};
}
function getDistance(source, destination) {
return google.maps.geometry.spherical.computeDistanceBetween(
new google.maps.LatLng(source.lat, source.lng),
new google.maps.LatLng(destination.lat, destination.lng)
);
}
// writes down the result of getMinimalDistance to a log div
function displayMinimalDistance() {
var minDistance = getMinimalDistance(myLocation, myLocations);
document.getElementById('log').innerHTML =
'Key of the marker at minimal distance: ' + minDistance.i
+ ' - distance: ' + minDistance.dist;
}
</script>
答案 1 :(得分:0)
你也可以参考这个问题SQL Distance Query without Trigonometry 在那里我找到了这个功能的灵感
protected void findClosestResources(LatLng mCurrentLocation, GoogleMap map,Context context){
Double currentLatitude=mCurrentLocation.latitude;
Double currentLongitude=mCurrentLocation.longitude;
Double longitudeDifferenceCorrection=1/Math.cos(currentLatitude);
Location=Place.query(LOCATION_TABLE_NAME, M_COLUMNS, "MIN(("+M_COLUMNS[1]+" - "+currentLatitude+") * ("+M_COLUMNS[1]+" - "+currentLatitude+
") + (("+M_COLUMNS[2]+" - "+currentLongitude+") * "+longitudeDifferenceCorrection+
") * (("+M_COLUMNS[2]+" - "+currentLongitude+") * "+longitudeDifferenceCorrection+"))", null,null
, null, null);
if (Location.moveToFirst()) {//select the first row in the cursor object
getInformationAbout(Location);
PlaceMakerOn(map);
Location.close();//close the connection with database
}
此函数通过从保存在SQLite数据库中的lat lon列表中选择它们来找到最近的位置
使用Location=Place.query(LOCATION_TABLE_NAME, M_COLUMNS, "MIN(("+M_COLUMNS[1]+" - "+currentLatitude+") * ("+M_COLUMNS[1]+" - "+currentLatitude+
") + (("+M_COLUMNS[2]+" - "+currentLongitude+") * "+longitudeDifferenceCorrection+
") * (("+M_COLUMNS[2]+" - "+currentLongitude+") * "+longitudeDifferenceCorrection+"))", null,null
, null, null);
其中MIN(("+M_COLUMNS[1]+" - "+currentLatitude+") * ("+M_COLUMNS[1]+" - "+currentLatitude+
") + (("+M_COLUMNS[2]+" - "+currentLongitude+") * "+longitudeDifferenceCorrection+
") * (("+M_COLUMNS[2]+" - "+currentLongitude+") * "+longitudeDifferenceCorrection+"))"
是SQLite语句的WHERE子句,其中M_COLUMNS[1]
和M_COLUMNS[2]
是数据库中纬度和经度的列
通过平均db和当前位置的两个位置的纬度和经度之间的差异,减小了此方法产生的误差,同时将经度差的平方值乘以longitudeDifferenceCorrection
就像名称所暗示的那样/ p>