javascript - 计算Google map 中其他用户视口(viewport)的相对位置

标签 javascript jquery html google-maps google-maps-api-3

我正在实现 Google map 协作器,其中其他用户的 View 端口表示为矩形。如果任何用户 View 端口发生变化,矩形位置就会更新。

现在我想实现如果用户视口(viewport)不在我的视口(viewport)上,我需要创建一个代表用户当前视口(viewport)方向的头(黑色圆圈)。我目前拥有其他用户 View 端口的边界(LatLng)位置。为了绘制头部,我需要计算当前用户位置的 Angular 。

您可以引用下图来了解我想说的内容。如果用户的视口(viewport)在我的视口(viewport)内,我可以在矩形上画一个头。但是如果用户 View 端口发生变化,我需要计算相对于用户 View 端口绘制头部(黑色圆圈)的距离和 Angular 。
您能给我一些建议来完成它吗?如果您已经做过与此相关的事情,请分享您的想法。

enter image description here

最佳答案

谷歌地图 Bounds() .. 获取视口(viewport)顶部、左侧、右侧、按钮的位置:)

var latlgnBound = map.getBounds();
var NorthEast = latlgnBound.getNorthEast();
var SouthWest = latlgnBound.getSouthWest();
var maxlatX = NorthEast.lat();
var minlatX = SouthWest.lat();
var maxlngY = NorthEast.lng();
var minlngY = SouthWest.lng();

if (userlocation.locationx < maxlatX && userlocation.locationx > minlatX) {
        if (userlocation.locationy < maxlngY && userlocation.locationy > minlngY) {
                // draw a head :) .
        }
}

抱歉,如果这不是您想要的;

外部用户;

**if (userlocation.locationx > maxlatX || userlocation.locationx < minlatX) 
{
// user is outside your view
}
if (userlocation.locationy > maxlngY || userlocation.locationy < minlngY) {
                // user is outside your view
        }
}**

更新

var angle = 0;
if(userlocation.locationx > maxlatX) {        
    angle += 1;
}
if(userlocation.locationy > maxlngY){
    angle += 3;
}
if(userlocation.locationx < minlatX){
    angle += 5;
}
if(userlocation.locationy < minlngY){
    angle += 7;
}
switch(angle){
    case 1:
          //User in top view port
    break;
    case 3:
          //User in left view port
    break;
    case 5:
          //User in bottom view port
    break;
    case 7:
          //User in right view port
    break;
    case 4:
          //User in top right view port
    break;
    case 6:
          //User in top left view port
    break;
    case 10:
          //User in right bottom view port
    break;
    case 12:
          //User in left bottm view port
    break;
};

使用 HTML5 获取用户位置:)

  // Try HTML5 geolocation
  if(navigator.geolocation) { //Nếu trình duyệt định vị được
    navigator.geolocation.getCurrentPosition(function(position) { 
    //Lấy vị trí người dùng
      //Get location of user
      userlocation.locationx = position.coords.latitude;
      userlocation.locationy = position.coords.longitude

    }, function() {
      handleNoGeolocation(true); //Không xác định được vị trí
    });
  } else {
    // Browser doesn't support Geolocation
    handleNoGeolocation(false);
  }
function handleNoGeolocation(errorFlag){
    if (errorFlag) {
          var content = "Can't get user location"; 
    } else {
          var content = "Browser doesn't support get location"; 
    }
}

或阅读更多:http://www.w3schools.com/html/html5_geolocation.asp

关于javascript - 计算Google map 中其他用户视口(viewport)的相对位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24800324/

相关文章:

html - YouTube Video API不遵守 'replay'参数值

javascript - 如何使 float 侧边栏在页脚之前停止 float 而不与其重叠

javascript - JavaScript 中的正则表达式 : table of positions instead of table of occurrences

javascript - 如何避免在需要 URI 更新时重新安装组件(例如过滤器按钮)?

javascript - 仅使用 JS 和 CSS 在主页上显示 div

php - 需要建议同时在mysql中插入两条记录

javascript - 垂直求和数组数组(矩阵)

jquery - 使用 jquery 删除拖放购物车的按钮

jquery - 使用 jquery 替换 <div> 中的 <p> 中的单词

html - Angular 中的可滚动路由器导出