我在AS3中使用Adobe Flash Pro制作AIR应用。
我已将多个POI点与其坐标放在一起:(我使用MapQuest)
var myIcon2:MapIcon = new MapIcon();
myIcon2.setImage(new pizzaImg(),22,32);
embeddedIconPoi = new Poi(new LatLng(-50.231838, 194.023561));
embeddedIconPoi.rolloverAndInfoTitleText = "Pizza";
embeddedIconPoi.icon = myIcon2;
PoiColl.add(embeddedIconPoi);
我已经设置了地理位置功能,以便在用户所在的位置放置一个点:
if (Geolocation.isSupported){
my_geo.addEventListener(GeolocationEvent.UPDATE, onGeoUpdate);
}
function onGeoUpdate(e:GeolocationEvent):void{
embeddedIconPoi.latLng = new LatLng(e.latitude, e.longitude);
}
现在,有没有办法创建一个功能,当他点击按钮时显示与用户最近的点?
类似的东西:
buttonClosest.addEventListener(MouseEvent.CLICK, goClosest, false, 0, true);
function goClosest(event:MouseEvent):void{
// Don't know what should I put here....
}
感谢您的回答,
斯蒂芬
答案 0 :(得分:0)
半正公式(http://en.wikipedia.org/wiki/Haversine_formula)可用于确定由纬度和经度定义的两个点之间的距离。在Actionscript 3.0中实现,它看起来像我从http://snipplr.com/view/58443/获得的代码片段
private const RADIUS_OF_EARTH_IN_MILES:int = 3963;
private const RADIUS_OF_EARTH_IN_FEET:int =20925525;
private const RADIUS_OF_EARTH_IN_KM:int =6378;
private const RADIUS_OF_EARTH_IN_M:int =6378000;
private function distanceBetweenCoordinates(lat1:Number,lon1:Number,
lat2:Number,lon2:Number,
units:String="miles"):Number{
var R:int = RADIUS_OF_EARTH_IN_MILES;
if (units == "km"){
R = RADIUS_OF_EARTH_IN_KM;
}
if (units == "meters"){
R = RADIUS_OF_EARTH_IN_M;
}
if (units =="feet"){
R= RADIUS_OF_EARTH_IN_FEET;
}
var dLat:Number = (lat2-lat1) * Math.PI/180;
var dLon:Number = (lon2-lon1) * Math.PI/180;
var lat1inRadians:Number = lat1 * Math.PI/180;
var lat2inRadians:Number = lat2 * Math.PI/180;
var a:Number = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.sin(dLon/2) * Math.sin(dLon/2) *
Math.cos(lat1inRadians) * Math.cos(lat2inRadians);
var c:Number = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d:Number = R * c;
return d;
}
一旦你拥有了它,你可以简单地遍历你拥有的点,找到每个点与用户之间的距离,然后得到最小值。
这可能看起来像这样(我不熟悉适用于AS3的MapQuests API,但您应该能够正确调整此代码)
function goClosest(e:MouseEvent):void
{
var minDistance:Number = int.MAX_VALUE;
var poiWithSmallestDistanceFromUser:Poi = null;
for each(var poi:POI in pois)
{
var distance:Number = distanceBetweenCoordinates(poi.lat, poi.lon, userPoi.lat, userPoi.lon);
if (distance < minDistance)
{
minDistance = distance;
poiWithSmallestDistanceFromUser = poi;
}
}
}
执行for循环后,你将使用minDistance和poiWithSmallestDistanceFromUser。
希望这有帮助!