Javascript 使用谷歌地图和跳跃运动选择标记
我一直试图在谷歌地图上选择一个最接近跳跃运动坐标的标记。我已尝试将leap motion坐标转换为google maps坐标,并通过对标记执行marker.lat和marker.lng,并与计算的纬度和经度进行比较,在google maps上找到距离leap motion设备最近的标记。但是,它不工作,而是返回屏幕底部或顶部的标记。这是我当前的javaScript代码Javascript 使用谷歌地图和跳跃运动选择标记,javascript,google-maps-api-3,leap-motion,Javascript,Google Maps Api 3,Leap Motion,我一直试图在谷歌地图上选择一个最接近跳跃运动坐标的标记。我已尝试将leap motion坐标转换为google maps坐标,并通过对标记执行marker.lat和marker.lng,并与计算的纬度和经度进行比较,在google maps上找到距离leap motion设备最近的标记。但是,它不工作,而是返回屏幕底部或顶部的标记。这是我当前的javaScript代码 var results = document.getElementById('resultsTable'); cons
var results = document.getElementById('resultsTable');
console.log("Key Tap Gesture at: " + keyTapGesture.position[0]);
//google.maps.event.dispatchEvent(ktEvent);
var closestMarkerDistance = 10000000000000000000000000; //big number to start, so any calculation will override this value
var closestMarker = null;
var distance = 10000000000000000000000000;
console.log("marker distances to keytap are: " + results.length);
var hand = frame.hands[0];
var stabilized = hand.stabilizedPalmPosition;
for (var i = 0; i < markers.length; i++) {
var markerPos = 0;
var keyTapX = 0;
var keyTapY = 0;
var newLatLngPt = 0;
var keyLng = 0;
var keyLat = 0;
markerPos = markers[i].position;
keyTapX = stabilized[0];
keyTapY = stabilized[1];
newLatLngPt = convertToLatLng(keyTapX, keyTapY);
var scaling = 4.0 / Math.pow(2, map.getZoom() - 1);
keyLng = keyTapX * scaling;
keyLat = keyTapY * scaling;
//var keyTapCoord = new google.maps.LatLng(keyLat, keyLng);
distance = getDistanceFromLatLonInKm(markerPos.lat(), markerPos.lng(), newLatLngPt.lat(), newLatLngPt.lng());
if (distance < closestMarkerDistance) {
closestMarkerDistance = distance;
closestMarker = markers[i];
}
console.log(" \n" + distance + markers[i].getTitle());
}
if(closestMarker != null) {
console.log("\nclosest marker is : " + closestMarker.name + " title: " + closestMarker.getTitle() + " at pos: " + closestMarker.getPosition());
infowindow.setContent(closestMarker.getTitle());
infowindow.open(map,closestMarker);
console.log("\n ALSO: --> " + stabilized[0] + " ::::::" + stabilized[1]);
console.log("\n ANNNNNND: --> " + keyLng + " ::::::" + keyLat + "and then real la/lo = " + markerPos.lng() + " ____ " + markerPos.lat());
//document.getElementById(choices).innerHTML = place.name + "<br/>" + place.vicinity;
}
else {
console.log("\nclosest marker Does Not Exist");
}
function getDistanceFromLatLonInKm(lat1, lon1, lat2, lon2) {
var R = 6371; // Radius of the earth in km
var dLat = deg2rad(lat2 - lat1); // deg2rad below
var dLon = deg2rad(lon2 - lon1);
var a =
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
Math.sin(dLon / 2) * Math.sin(dLon / 2)
;
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var d = R * c; // Distance in km
return d;
}
function convertToLatLng(x, y) {
// retrieve the lat lng for the far extremities of the (visible) map
var latLngBounds = map.getBounds();
var neBound = latLngBounds.getNorthEast();
var swBound = latLngBounds.getSouthWest();
// convert the bounds in pixels
var neBoundInPx = map.getProjection().fromLatLngToPoint(neBound);
var swBoundInPx = map.getProjection().fromLatLngToPoint(swBound);
// compute the percent of x and y coordinates related to the div containing the map; in my case the screen
var procX = x / window.innerWidth;
var procY = y / window.innerHeight;
// compute new coordinates in pixels for lat and lng;
// for lng : subtract from the right edge of the container the left edge,
// multiply it by the percentage where the x coordinate was on the screen
// related to the container in which the map is placed and add back the left boundary
// you should now have the Lng coordinate in pixels
// do the same for lat
var newLngInPx = (neBoundInPx.x - swBoundInPx.x) * procX + swBoundInPx.x;
var newLatInPx = (swBoundInPx.y - neBoundInPx.y) * procY + neBoundInPx.y;
// convert from google point in lat lng and have fun :)
var newLatLng = map.getProjection().fromPointToLatLng(new google.maps.Point(newLngInPx, newLatInPx));
return newLatLng;
}
function deg2rad(deg) {
return deg * (Math.PI / 180)
}
var results=document.getElementById('resultsTable');
console.log(“按键手势在:”+keytapsignate.position[0]);
//google.maps.event.dispatchEvent(ktEvent);
var closestMarkerDistance=100000000000000000//开始时的数值较大,因此任何计算都将覆盖此值
var closestMarker=null;
var距离=100000000000000000;
log(“到按键的标记距离为:“+results.length”);
var-hand=frame.hands[0];
var稳定=手部稳定位置;
对于(var i=0;i”+稳定的[0]+”:::::“+稳定的[1]);
console.log(“\n annnnd:-->”+keyLng+”:::“+keyLat+”然后是real la/lo=“+markerPos.lng()+”);
//document.getElementById(选项).innerHTML=place.name+“
”+place.neighborary;
}
否则{
console.log(“\nclosest标记不存在”);
}
函数getDistanceFromLatLonInKm(lat1、lon1、lat2、lon2){
var R=6371;//地球半径,单位为km
var dLat=deg2rad(lat2-lat1);//下面是deg2rad
var-dLon=deg2rad(lon2-lon1);
变量a=
数学单(dLat/2)*数学单(dLat/2)+
数学cos(deg2rad(lat1))*数学cos(deg2rad(lat2))*
数学单(dLon/2)*数学单(dLon/2)
;
var c=2*Math.atan2(Math.sqrt(a),Math.sqrt(1-a));
var d=R*c;//以公里为单位的距离
返回d;
}
函数转换映射(x,y){
//检索(可见)地图远端的lat lng
var latLngBounds=map.getBounds();
var neBound=latLngBounds.getNorthEast();
var swBound=latLngBounds.getsoutwest();
//以像素为单位转换边界
var neBoundInPx=map.getProjection().fromLatLngToPoint(neBound);
var swBoundInPx=map.getProjection().fromLatLngToPoint(swBound);
//计算与包含地图的div相关的x和y坐标的百分比;在我的例子中是屏幕
var procX=x/window.innerWidth;
var procY=y/window.innerHeight;
//以像素为单位计算lat和lng的新坐标;
//对于lng:从容器的右边缘减去左边缘,
//将其乘以x坐标在屏幕上的百分比
//与放置贴图的容器相关,并添加回左边界
//现在,您应该拥有以像素为单位的Lng坐标
//对lat也这样做
var newLngInPx=(neBoundInPx.x-swBoundInPx.x)*procX+swBoundInPx.x;
var newLatInPx=(swBoundInPx.y-neBoundInPx.y)*procY+neBoundInPx.y;
//从lat lng中的google point转换并享受乐趣:)
var newLatLng=map.getProjection();
返回newLatLng;
}
功能deg2rad(度){
返回度*(数学PI/180)
}
跳跃运动坐标系与浏览器窗口无关。X轴从约-300mm延伸至+300mm。Y轴从大约0延伸到+600mm——它也向上为正,而浏览器Y轴向下为正。如果我读对了你的代码,你正在尝试使用跳跃运动坐标,就好像它们是像素坐标一样。他们不是
您需要将跳跃运动坐标映射到对您的应用程序有意义的位置。文档中的这篇文章可能有助于:
特别是,请查看InteractionBox类,使用该类可以将跳跃运动坐标标准化为范围[0..1]。通常,将标准化坐标映射到应用程序坐标系更容易